trouble printing out in loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slash_gnr3k
Forum Commoner
Posts: 43
Joined: Tue Nov 28, 2006 6:41 pm

trouble printing out in loop

Post by slash_gnr3k »

hi,
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);
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!
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

I think there is much easier way to do it

Code: Select all

$result = @mysql_query("SELECT name FROM dishes WHERE vegetarian = 'Yes' order by rand() limit 7")or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
    echo $row['name']."<br />";
}
slash_gnr3k
Forum Commoner
Posts: 43
Joined: Tue Nov 28, 2006 6:41 pm

Post by slash_gnr3k »

sorted - thanks!
Post Reply