Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jan 2008
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Help with "recipe" script that updates ingredients based on servings

    I have been asked to "throw together" a script that essentially shows a recipe drawn from mysql. The twist is that they want the visitor to be able to change the number of servings (default=4) and that will then update the measurements on the site.

    We have a similar file in cgi that runs from two flat files (one with the title, image location, method and the second txt file, named the id number for the individual recipe, and each line has two fields, the number and the measurement/ingredient). I'm not a CGI person, and they are planning to do everything using mySQL from now on.

    I need to be able to do something quick to show it can be done, so I was trying to just use the flat files from the earlier project, and code the php to make it do what I want, but I don't know if it's possible to include two flat files in one document, and if so, how to script that. Anyone know?

    I will be putting the final project in a database eventually, and I have no idea how that could be structured so it works, but if anyone has any ideas/advice, I'm more than willing to hear it!

  • #2
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,008
    Thanks
    59
    Thanked 10 Times in 10 Posts
    What do you mean by flat files?


  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    It sounds like you need more help with PHP than just the formula to change ingredients. That formula is 3rd grade math.

    So... what is your real question?

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Flat file as opposed to database. Also called sequential file, text file, data file.

  • #5
    New Coder
    Join Date
    Jan 2008
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts
    nikos, flat files are txt files that php draws data from. they are used when there's no access to a database.

    fumigator, the "real" question is this:
    I have no idea how that could be structured so it works

    The equation is not the issue. The issue is that I can't sort out, in my head, how to set up the database so that I can pull the information the way I need it.

    Setting up the recipe is not a problem, it's how to put in the ingredients. The original script has two files. One is the recipe without the ingredients, and the second is the individual recipe's ingredients, named for the recipe's id number, with each line containing two columns...one for the number and one for the measurement and ingredient.

    Is there a more logical way to do it than that?

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    EDIT: Not sure if you're asking how to put it in the database, or in the flat files. This is easy enough to put in a database. There isn't that much going on.

    --------------------------------

    Table: Recipe

    id, name, description, cookingtime (etc.)

    Table: Ingredients

    id, name, description (optionally: weight, nutritional values, etc.)

    Table: Unit

    id, name, shortname

    Table Recipe_Ingredients

    recipe_id, ingredient_id, amount, unit_id

    --------------------------------

    All amounts in recipe_ingredients are for single (1!) servings. This makes it easy to multiply.

    Join the tables together, and in the column clause of the select statement, multiply Recipe_Ingredients.amount times the number of servings. The query just needs 2 inputs: Recipe ID and Servings.
    Last edited by aedrin; 01-10-2008 at 05:45 PM.

  • Users who have thanked aedrin for this post:

    lilbadger25 (01-10-2008)

  • #7
    New Coder
    Join Date
    Jan 2008
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks so much Aedrin. I have been staring at another project so long that my brain won't think of anything beyond.

    I appreciate your time.

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    No problem, I've tried to throw together a recipe database before so I had some knowledge left (though that never got finished)

  • #9
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    I was presented with a similar problem before. Would you recommend storing the quantities as fractions? Let's say the recipe is for 3 servings and requires 1 teaspoon of vanilla. Do you store it in the database as '1/3' teaspoons of vanilla or .333333 teaspoons? You obviously have to worry about rounding if you store it as some sort of decimal number.

    I just stored the default recipe in the database and calculated the quantities based on the requested_serving_size/default_serving_size ratio.

    There are also some interesting conversions. For example, do you tell the user to add 6 teaspoons or 2 tablespoons. Do you convert pints, cups, etc. or just use the most "base" unit?

  • #10
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You'd store the actual number (.333)

    One can always convert to 'human readable' format afterwards. But storing it in 'machine readable' format allows you to do easy calculations with it (in the database, not php).

  • #11
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    This was posed to me as a learning exercise a few years back. The big frustration I recall having was when you store the value as decimal (.333) you end up with questionable rounding issues. For example, if you multiply .333 * 3 servings, you get .999. and you need to round it up to 1. But when you round something like 1/8 = .125 you get .13 which isn't what you want. I'm just noticing that they have added a precision parameter to round() in the years since I had this issue which is basically what I had to mimic for reasonable results.

  • #12
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    This is why the metric system makes so much more sense. No fractions. I'm sure there is a decimal to fraction algorithm out there though.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •