Executing 2 scripts based on different variables
I have an Sql database which has a table containing: Email, Company, Name. When I do a query on that database and compare E-mail addresses I also get trim($row['company'])
& trim($row['name'])
so that the Company and Name of the person associated with that Email address are also brought down.
My problem is this: I am using require_once based on the trim($row['company'])
& trim($row['name'])
that is associated with the E-mail address. Ex.
require_once "/var/www/vhosts/default/htdocs/" . trim($row['company']) . "/Users/" . trim($row['name']) . "/example.php";
So using the above example, the script is then pointed to the example.php script located in the directory using the company and name variables that it got from the database, based on the email.
If I have 2 emails say each are from the sa开发者_运维知识库me company so trim($row['company']) would stay the same, but the 2 emails are assigned to different users, so trim($row['name']) would be different. Is there a way that when I use require_once that I can have both example.php scripts in both directories executed? Thanks!
Don't use require_once, but only require (or include if you feel better).
This looks unsafe
This looks unsafe if you don't do proper escaping, as user could input arbitrary characters and harm your script. (i.e. lead to another script like when inputting name "../AnotherUser")
The answer
But yes, you can include all scripts for particular company by using glob to enumerate all directories under specific directory:
foreach(glob("/var/www/vhosts/default/htdocs/" . trim($row['company']) . "/Users/*", GLOB_ONLYDIR) as $userDir) {
require_once($userDir."/example.php");
}
In order to do this you need to retrieve more than 1 row from your database. So you would have an array of arrays as your result. For example:
$result = array(
0 => array('email' => 'alex@abc.com', 'name' => 'Alex', 'company' => 'ABC'),
1 => array('email' => 'beth@xyz.com', 'name' => 'Beth', 'company' => 'XYZ')
);
Once you retrieve the data like that you would use the following code to run the script for each of them:
foreach ($result as $row)
{
require('/var/www/vhosts/default/htdocs/' . trim($row['company']) . '/Users/' . trim($row['name']) . '/example.php');
}
As far as how to retrieve multiple rows for all the emails from your database that depends on your DB code so I can't really help there without seeing what you use.
精彩评论