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!
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
$peepsfor this example – when you output the data you do soemthing like this: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:
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.