2012-08-27 307 views
-2

我正在創建一個存儲食譜的網站。我想製作一個配方縮放器 - 在那裏你可以輸入所需的份量,而腳本將轉換配方成分以匹配它。我可以很容易地改變金額,但我想弄清楚如何轉換單位。現在我知道我可以放入1500條if/then語句,但我試圖讓它更簡單一些。 :)食譜計算器

我正在看一個名爲美味廚房的網站。他們通過AJAX發送這份文件到這個文件(http://tastykitchen.com/recipes/wp-admin/admin-ajax.php),並且該文件返回成分。然後他們在頁面上顯示它們。以http://tastykitchen.com/recipes/breads/plain-bagels/爲例。

我會真的感謝任何幫助,我可以得到。

謝謝!

+0

我不確定如果我真的理解正確嗎?你想轉換爲不同的單位,還是隻是擴大現有的單位? – chameco

+0

假設我有一個食譜需要2茶匙的東西。如果我三倍,我不希望它顯示6茶匙,但2湯匙。 – Caleb

+0

tsp和tbsp在不同的國家意味着不同的事情,我會建議堅持SI單位。 – 2012-08-27 00:31:03

回答

1

我的建議是在你的數據庫中存儲一個服務器,然後簡單地將你想在頁面上輸入的人數乘以。因此,如果你的用戶選擇了他們想要爲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; 
} 
+0

我的問題不是乘法,但我需要知道如何轉換單位,如果他們需要轉換。(請參閱我對chameco的評論) – Caleb

+0

@Caleb我剛剛看到評論,並在回答時添加了一個修改。將數據庫中的所有數據以Metric(最簡單的方式)存儲在數據庫中,然後按照服務將其相乘,並且具有從公制轉換爲相應英制測量的功能。 – Fluffeh

+0

問題是我想要它轉換單位,所以它會顯示1/4杯vs 4 TB。此外,用戶將進入選擇他們自己的單位的食譜。 – Caleb

1

@Caleb - Fluffeh是他的策略吧就在這裏其對在一個類型的測量存儲一切,然後有一系列的功能,將它們轉換爲其他。我建立了一個Web應用程序https://Gredio.com,可以在其他方面進行配方縮放,並能夠以這種方式生成非常靈活的報告。另一個困難是圍繞液體量度和體重。大規模的食品生產總是以重量來完成,但小傢伙有時會誤解差異。以編程方式進行液體重量轉換可能很複雜,因爲您需要知道液體重量會有很大差異(請考慮蜂蜜與水...)