On Sunday 08 May 2005 12:37, Matthew Macdonald-Wallace wrote:
> Hi all,
>
> Following on from my post about php checkboxes, this is what I'm trying
> to do:
>
> Recipe ingredients are added to the database along with the recipe book,
> recipe name and some other information.

Would be useful to know how you're storing this in the DB.

I'd imagine you have a table to store each recipe's 'summary' details, for 
example:
table recipes:
id | name                 | (any other useful fields)
---+----------------------+---------------------------------
 1 | Chicken and chips    | 
 2 | Beef and potato      |
 3 | Chicken Recipe two   |
------------------------------------------------------------

... and then have an 'ingredients' table which has one row for each ingredient 
held in the system, giving it an ID number and a 'unit' to record what the 
'amount' of the ingredient is stored in (you'll probably want grams for solid 
ingredients, and millilitres (sp?) for liquid ingredients).

table ingredients:
id | ingredient       | unit
---+------------------+------------
 1 | Chicken breast   | gram
 2 | Beef             | gram
 3 | Water            | ml
 4 | Potato chips     | gram
-----------------------------------


and finally, a table that has one row for each ingredient required by a 
recipe, something like:

table recipeingredients:

recipe_id | ingredient_id  | amount
----------+----------------+-------------------
   1      |    1           |  500
   1      |    4           |  250
(etc)
-----------------------------------------------


> The problem I have is that I can't get my head around the code to do the
> following:
>
> 1. Get the information about the recipe from the database
> 2. Add together the quantities needed of each ingredient for each
> recipie
> 3. Display the total amount of each ingredient needed next to the
> ingredient name
>
> for example, if I have two chicken dishes, one requiring 500g of chicken
> and another requiring 250g, the page should display:
>
> Chicken - 750g

So now, you can loop through each recipe that was chosen, run a SELECT against 
the recipeingredients table to find out what ingredients it requires and how 
much of each, and add them up.


Rudy will probably jump in and offer a much better database design than the 
one I've done above, but it should at least work.

I assume you should be OK doing the SQL queries to fetch the info the way you 
want it (probably easiest to do a join so that you get the amount of each 
ingredient along with the 'unit' in one go), post back and I'll see what I 
can do.

Cheers

Dave P



____ � The WDVL Discussion List from WDVL.COM � ____
To Join wdvltalk, Send An Email To: mailto:[EMAIL PROTECTED] or
use the web interface http://e-newsletters.internet.com/discussionlists.html/
       Send Your Posts To: [email protected]
To change subscription settings, add a password or view the web interface:
http://intm-dl.sparklist.com/read/?forum=wdvltalk

________________  http://www.wdvl.com  _______________________

You are currently subscribed to wdvltalk as: [email protected]
To unsubscribe send a blank email to [EMAIL PROTECTED]
To unsubscribe via postal mail, please contact us at:
Jupitermedia Corp.
Attn: Discussion List Management
475 Park Avenue South
New York, NY 10016

Please include the email address which you have been contacted with.

Reply via email to