Code:make-nutrition0.9-url-from-sql-results.html

From the change wiki

The main use-case of this code, is to show the global food supply in the Nutrition Calculator.

This code will take the results of certain SQL queries (see Code:food1.sql) and convert them into URLs. The input (SQL query results) must be formatted like the following example:

50|Molasses
150|Onions, raw

For each row, there must be the number of grams, followed by the | character, followed by the name of the food (string must be identical to the name in the calculator).

Code

<html><head><script>
 function generate() {
  var url = 'https://olam.wiki/nutrition.html?targets=averaged&timescale=1';
  var title = document.getElementById("title").value;
  if (title) url += '&title='+encodeURIComponent(title);
  var lines = document.getElementById("input").value.split("\n");
  for (var i=0; i<lines.length; i++) {
   var cols = lines[i].split("|");
   if (cols.length==2) {
    url += '&amount'+i+'='+encodeURIComponent(cols[0]);
    url += '&name' +i+'='+encodeURIComponent(cols[1]);
   }
  }
  document.getElementById("output").value = url;
 }
</script></head><body>
 <input id='title' placeholder='title (optional)'></input><br />
 <textarea id='input' placeholder='paste sql results here' cols='80' rows='24'></textarea><br />
 <button onclick='generate()'>Generate url:</button>
 <input id='output' placeholder='url' readonly></input>
</body></html>