Page 1 of 1

struggling with loop

Posted: Fri Apr 06, 2007 2:07 pm
by slash_gnr3k
hi,
i am on the very last hurdle of my project and i just dont seem to be able to do what im trying to do:

i have a page which selects seven random meals from the database, one for each day of the week. each meal has a number of ingredients each with a quantity. when the user clicks a link, it takes them to a shopping list page which will print out each ingredient and the total amount needed of this ingredient in all seven dishes.....

i am having REAL problems actually implementing this -

i have the following script (on the shopping list page):

Code: Select all

		//array of dishes suggested for the week		
		$idarray = array();


		//DISH ID NUMBERS PASSED FROM A PREVIOUS PAGE
		$idarray[0] = $id1;
		$idarray[1] = $id2;
		$idarray[2] = $id3;
		$idarray[3] = $id4;
		$idarray[4] = $id5;
		$idarray[5] = $id6;
		$idarray[6] = $id7;

		//WE NOW HAVE AN ARRAY OF ALL THE DISHIDS

		var_dump($idarray);

		$iteration = 0;
		$ingidarray = array();
		$it = 0;
		$quantarray = array();
		$totalquant =0;

		//DO FOR EACH DISH (7 TIMES)
		//THIS LOOP JUST OUTPUTS ALL INGREDIENTS AND QUANTITES
		while ($iteration <7)
		{	

			//gets the ids and quantities of ingredients from each dish suggested
			$getidquant = @mysql_query("SELECT ingid, quant FROM recipes WHERE dishid = $idarray[$iteration]")or die(mysql_error());

			//array of all ingredients
			while ($row = mysql_fetch_assoc($getidquant))
			{

				$ingid = $row[ingid];
				$quant = $row[quant];

				$ingidarray[$it] = $ingid;
				//HERE WE HAVE AN ARRAY OF INGREDIENT IDS - FOR SIMPLICITY PURPOSES I AM ONLY USING DISHES WITH TWO INGREDIENTS AT THE MOMENT - THERE COULD BE MORE OR LESS THAN THIS NUMBER
				//THIS MEANS THAT FOR THE PURPOSE OF TESTING $INGIDARRAY WILL ALWAYS BE OF SIZE 14

				echo "<p><b>Ing ID</b>$ingid quant:$quant</p> ";
				$it++;


		
			}



			$iteration++;

		}


		//HAVING PROBLEMS FROM HERE ONWARDS....

		$ITVAR = 0;
		$foo=0;
		
		$ingarraysize = count($ingidarray);
			

		//this loop is run once for every ingredient
		while ($ITVAR <$ingarraysize)
		{


				//RETURNS QUANTS FOR EVERY INGREDIENT IN THE 7 DISHES
				$getquants = mysql_query("SELECT quant FROM recipes WHERE ingid = $ingidarray[$ITVAR] AND dishid IN (". implode(",",$idarray) . ")");
	
				if (!$getquants)
				{

					echo 'quant failure' . mysql_error() . '';

				}
				echo"&nbsp;";				

				while ($row = mysql_fetch_assoc($getquants))
				{

					$result = $row[quant];
					echo "$result, ";
					//$totalquant = $totalquant+$result;
				}
		$ITVAR++;
		//TODO - do not check for this ingredient again
		//POSSIBLY NEED SOME WAY OF FLAGGING AN INGREDIENT AS CHECKED AND MAKING OF CONDITION OF IF INGREDIENT HAS NOT BEEN CHECKED DO THE ABOVE

		}
so far the script above retrieves every ingredient needed for each meal and displays it quantity, this shows repeated ingredients.

i have marked on the script where my problem is. from that point, basically i want it to check the first ingredient of the first dish and its quantity and add the quantity to a variable, search for that ingredient in the other 6 dishes and add values to that variable to get a total for this ingredient and then put it in a new array, then get the 2nd ingredient of the 1st dish and do the same and so on and so on.

the script i have above works until it comes across a repeated ingredient - that is for example if it checked for the quantity of the 1st ingredient in the first dish but then if the ingredient is in a dish later on it will do the check again so i will end up with lots of double checked ingredients

i hope this makes sense, i have commented the code to help with understanding,
i have tried many different methods for the last 3 days and cannot find a way to do it,
please cab someone give me some advice,
thanks
Steve

Posted: Fri Apr 06, 2007 3:01 pm
by RobertGonzalez
You can check uniqueness fairly easily by setting a var for the current ingredient and as you loop, check that ingredient against the loop value. If the ingredient is the same as the loop value, use the continue; construct to break out of that iteration of the loop and move to the next one. If it is not the same, add the current ingredient to an array and switch the current ingredient var to the current, different ingredient.

Posted: Mon Apr 09, 2007 3:30 am
by slash_gnr3k
i used the array unique function to make every element in the array unique. works now! thanks!