what would be the easiest way to switch a mySQL query condition within PHP after all results are listed.
Let’s assume I have a table with the fields “fruit” and “color”.
What I am looking for is a loop where in the first round all fruits with red color will be selected and in the second round all as green fruits.
$fruit_selection = mysql_query(“SELECT fruit FROM table WHERE color LIKE ‘$fruit_color’”, $dbi);
while(list($fruit) = mysql_fetch_row($fruit_selection)){
echo “$fruit”;
}
Between these two rounds I would like change some parameters for other variables.
Thanks, but I am looking for something different. (My fault)
Let me ask the question a little differnt to show the problem more precise:
I want to send some users a mail. Each mail shall be in the preferred user’s language. The language is done with constants.
To avoid redefining constants for each user (unnecessary SQL query and time), I would like to first define the English constants and than select all users that use English. After the mail has been sent to all of them, I would like to define the German constants and send the same mail in German.
Thus I need some loop that lets me redefine constants after all users of one language have been selected. So this would need more some kind of PHP solution rather than the SQL type.
So this is what I want to do:
Define language
Define Constants based on language
Select all users which use defined language
Switch language (go back to 1.) after all users of earlier defined language have been processed.
Somehow a while loop or a for each seems to be not the right type - or am I mistaken?
What i would do in this type of situation and im sure someone has a better way but i would use a function and just simply have it do all the work.
function sendMail($lang='english'){
// Grab all the language
include_once('lang/'.$lang.'.php');
// Grab all the users matching the $lang
$res = mysql_query("SELECT someone FROM something WHERE language='$lang'");
$userList = array();
while($row = mysql_fetch_array($res)){
$userList[$row['username']] = $row['email'];
}
mysql_free_result($res);
// Make sure the array has a length of at least 1
if (sizeof($userList) == 0){
exit('Invalid user array given or no array given at all.');
}
// Setup the message
// Would be replaced by the language constants
$message = "Hello %s,\
\
";
$message .= "We would like to share something with you.";
foreach($userList as $user => $email){
// Parse the message
$message = sprintf($message, $user);
if (!mail($email, 'Subject title', $message, 'From <you@something.com>')){
exit('Error sending email to '.$email);
}
}
}
sendMail();
sendMail('german');