我的建議是在你的數據庫中存儲一個服務器,然後簡單地將你想在頁面上輸入的人數乘以。因此,如果你的用戶選擇了他們想要爲4人做飯,你保留一個變量(會話,cookie,無論什麼) - 讓我們稱之爲$peeps
這個例子 - 當你輸出的數據你做得像這個:
Echo "This will make $peeps portions:";
Echo "Ingredients:<br>";
Echo ($peeps*Ingredient1Quantity)." - ".$ingredient1Name."<br>";
Echo ($peeps*Ingredient2Quantity)." - ".$ingredient2Name."<br>";
等等。
如果您希望您的網站也可以從英制轉換爲公制,您可以使用簡單的功能輕鬆完成。
如果您將所有數據存儲在數據庫中的公制(或英制,但所有相同類型的),你可以根據自己的喜好以類似的方式將其轉換爲用戶輸出很容易就夠了:
// 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>";
編輯:如果您將所有內容都存儲在數據庫中,則可以使用最佳英制測量功能將數據返回給您。
// 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;
}
我不確定如果我真的理解正確嗎?你想轉換爲不同的單位,還是隻是擴大現有的單位? – chameco
假設我有一個食譜需要2茶匙的東西。如果我三倍,我不希望它顯示6茶匙,但2湯匙。 – Caleb
tsp和tbsp在不同的國家意味着不同的事情,我會建議堅持SI單位。 – 2012-08-27 00:31:03