trouble printing out in loop
Posted: Mon Apr 23, 2007 2:14 pm
hi,
im having problems with the following php script:
what the script does is
*gets the maxid in the table so that a random number can never be higher than that
*adds all the ids that are in the database to an array
*gets a random number between 1 and the max id
*checks if the number is not it the valid id number array and whether it has already been suggested
* keeps getting a number until the conditions have been satisfied
*adds the number to the suggested id array
*prints the name that corresponds to this id,
does this 7 times
the problem is with the query that returns the name corresponding to the id. the wuery runs fine as the error message is not output but the names are not printed out. can anyone see why?
thanks!
im having problems with the following php script:
Code: Select all
if ($nodishes >6)
{
echo "<b><h2>Vegetarian Option</h2></b><br />";
//get max dishid value so random number will never be a number greater than the highest
//number of ids in databse
$maxid = @mysql_query("SELECT max(dishid) FROM dishes") or die (mysql_error());
//add the max id to a variable
while ($row = mysql_fetch_array($maxid))
{
$maxidno = $row['max(dishid)'];
}
$weekitno=0;
$namearray=array();
$suggestedids=array();
//make seven meal suggestions
while ($weekitno <7)
{
//select all dishid values from the dishes table that are vegetarian
$idexists = @mysql_query("SELECT dishid FROM dishes WHERE vegetarian = 'Yes'") or die(mysql_error());
//if a database entry has been deleted it's id is also deleted and never restored
//this condition makes sure the random number is not a number that doesnt exist due
//to a record being deleted
//array of all ids in the database
$idarray = array();
$iterationnumber = 0;
//add all returned ids to an array so the random number can be checked against them
while ($row = mysql_fetch_array($idexists))
{
$idarray[$iterationnumber] = $row['dishid'];
$iterationnumber++;
//all valid id numbers are now stored in an array
}
//if the random number generated is in the array it is a valid id. if not, keep generating until valid id found
$random = rand(1, $maxidno);
while(!in_array($random, $idarray) && in_array($random, $suggestedids))
{
$random = rand(1, $maxidno);
}
$suggestedids[$weekitno] = $random;
//returns the name of the dish
$getrandomname = @mysql_query("SELECT name FROM dishes WHERE dishid = '$suggestedids[$weekitno]'")or die (mysql_error());
while ($row = mysql_fetch_assoc($getrandomname))
{
$name = $row['name'];
echo"$name<br />";
}
$weekitno++;
}
var_dump($suggestedids);*gets the maxid in the table so that a random number can never be higher than that
*adds all the ids that are in the database to an array
*gets a random number between 1 and the max id
*checks if the number is not it the valid id number array and whether it has already been suggested
* keeps getting a number until the conditions have been satisfied
*adds the number to the suggested id array
*prints the name that corresponds to this id,
does this 7 times
the problem is with the query that returns the name corresponding to the id. the wuery runs fine as the error message is not output but the names are not printed out. can anyone see why?
thanks!