Jump to content


Photo

Grouping Like Items


  • Please log in to reply
2 replies to this topic

#1 hadem

hadem
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 22 May 2006 - 04:16 PM

So i wanted to write a simple PHP script that would keep track of my recipes in a MySQL database so that I could keep track of the ingredients, any notes, and ratings, etc. I really wanted a feature where i could select a bunch of recipes and have the script generate a shopping list for me. I know how to do that, but then i want it to group like items. So, if two different recipes call for one apple, the shopping list would tell me to buy 2 apples instead of listing 1 apple twice. Does anyone have any suggestions? Thanks in advance.

#2 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 22 May 2006 - 04:20 PM

you could probably use "GROUP BY" in your query.

SELECT * FROM recipies WHERE cluause goes here GROUP BY ingrediantsTable

OR

in your PHP script that makes a list, as you go through your database results and add each item to the list, put them into an array. As you add each item to the array, do a check to see if the item is already inarray(). if so, don't add it again.
"Confidence in the face of risk."

#3 hadem

hadem
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 23 May 2006 - 12:21 AM

So i got it to group. i used

$result = mysql_query("SELECT item, SUM(qty) FROM recipes GROUP BY item");





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users