Question

I am creating a website that stores recipes. I am wanting to make a recipe scaler-where you can enter the desired servings, and the script will convert the recipe ingredients to match it. I can change the amount easy enough, but I'm trying to figure out how to convert units. Now I know I can go and put in 1500 if/then statements, but I'm trying to make it a bit simpler. :)

I was looking at a site called Tasty Kitchen. They POST the servings via AJAX to this file (http://tastykitchen.com/recipes/wp-admin/admin-ajax.php), and that file returns the ingredients. Then they display them on the page. Go to http://tastykitchen.com/recipes/breads/plain-bagels/ for an example.

I would REALLY appreciate any help I can get.

Thanks!

Was it helpful?

Solution

My suggestion would be to store a single serve in your database, then simply multiply out how many people you want to feed on the page.

So, if your user has selected they want to make the meal for 4 people, you keep a variable (session, cookie, whatever) - lets call it $peeps for this example - when you output the data you do soemthing like this:

Echo "This will make $peeps portions:";
Echo "Ingredients:<br>";
Echo ($peeps*Ingredient1Quantity)." - ".$ingredient1Name."<br>";
Echo ($peeps*Ingredient2Quantity)." - ".$ingredient2Name."<br>";

and so on.

If you want your site to also convert from imperial to metric, you can do it easily enough with a simple function.

If you store all your data in metric in the database (or Imperial, but all the same type) you can output it easily enough by converting it in a similar manner to the user based on their preference:

// Assumes a function that converts to Imperial from Metric called convertToImperial()
Echo "This will make $peeps portions:";v
Echo "Ingredients:<br>";
Echo convertToImperial($peeps*Ingredient1Quantity)." - ".$ingredient1Name."<br>";
Echo convertToImperial($peeps*Ingredient2Quantity)." - ".$ingredient2Name."<br>";

Edit: If you store everything in the database in Metric, you can then have a function return the data to you in the best Imperial measurement.

// For example, input is passed as 
// $qty=30 (ml) 
// $serves is passed as 3 (ie, three people)
// $type is passed as liquid.

function convertToImperial($qty, $serves, $type)
{
    // Metric to Imperial will need a $type passed (Liquid, Weight, Other).
    // You can use a switch statement to pass between the different types.
    switch ($type)
    {
         case "Liquid":
             // Assumes 5ml is a teaspoon
             // Assumes 15ml is a tablespoon
             // Assumes 250ml is a cup.
             $CalMeasure=$qty*$serves; // Now at 90ml.
             // Here you can now choose to either pick the best match
             // ie, measurement with least remainder/exact measure
             // which in this case would be 6 tablespoons
             // or
             // switch measurement types after a certain quantity is reached.
             if ($CalMeasure>125) // Half a cup
             {
                 return (round($CalMeasure/250,2)." cups");
             }
             elseif ($CalMeasure>15) // tablespoons
             {
                 return (round($CalMeasure/15,2)." Tablespoons");
             }
             else
             {
                 return (round($CalMeasure/5,2)." Teaspoons");
             }
             break;
         case "Weight":
             // Similar approach to Weights, Convert Grams to Pounds and the like.
             return $WeightMeasured;
             break;
         default: // assumes Other (pinches, sprinkles etc
             // Similar approach again.
             break;
}

OTHER TIPS

@Caleb - Fluffeh is right on here with his strategy in regards to storing everything in one type of measurement and then having a series of functions to convert them to others. I built a web application https://Gredio.com that does recipe scaling among other things and was able to produce very flexible reports in this way. One additional difficulty is around liquid measures versus weight. Large scale food production is always done by weight, but the small guys sometimes misunderstand the difference. Doing liquid to weight conversions programmatically can be complex as you need to know the liquids weight which will vary greatly (think honey versus water...)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top