Page 1 of 1

trouble with totals

Posted: Wed Apr 04, 2007 4:36 pm
by slash_gnr3k
hi,
i am having the following problem,

i have a page which suggests seven meals(one for each day of the week) each meal has one or more ingredients and each ingredient has a quantity
at the click of a link, i want a page that shows ONLY the ingredients and their quantities without recpeated ingredients eg if spaghetti is used in 2 dishes - one with 1 unit and anothe with 4 i want it to say "spaghetti - 5" etc...

so far i have this.....

Code: Select all

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


		//adds passed parameters to array
		//an array of DISHID numbers

		$idarray[0] = $id1;
		$idarray[1] = $id2;
		$idarray[2] = $id3;
		$idarray[3] = $id4;
		$idarray[4] = $id5;
		$idarray[5] = $id6;
		$idarray[6] = $id7;

		$iteration = 0;
		$ingidarray = array();
		$it = 0;
		
		while ($iteration <7)
		{	

			//gets the ids and quantities of ingredients from each dish suggested
			$getid = @mysql_query("SELECT ingid FROM recipes WHERE dishid = $idarray[$iteration]");
			
			if (!$getidquant)
			{

				echo 'fail ' . mysql_error() . '';

			}

			//array of all ingredients


			while ($row = mysql_fetch_array($getid))
			{

				$ingid = $row[ingid];

				//add ingredient id only if it is not already in the array
				while(!in_array($ingid, $ingidarray))
				{

					$ingidarray[$it] = $ingid;
				}			

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

		
			}

		//at this point we hav an array of all ingredients used (each ingredient is in array only once)

			
			$iteration++;

		}

		echo"<b>ingredientids:</b>";
		var_dump($ingidarray);

i am not sure where to go from here. i think the following would be good:

-query that gets quantities for the 1st ingredient
-adds to variable
-query executed again - if the quant for this ingid has already been added to a variable then add this amount to that variable
-if not, add to another variable
- so on

i am unsure of how to do this, anyone have any ideas?
thanks

EDIT - just realised that to do it the way i said above my array of unique number would need to be an array of ALL the ingredients

Posted: Wed Apr 04, 2007 8:41 pm
by jwalsh
You should be able to do it completely in SQL.

Code: Select all

SELECT SUM(ingrediantquantity) FROM ingrediants GROUP BY ingrediantID
Is that what your looking for?