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

From the change wiki
(Created page with "The main use-case of this code, is to show the global food supply in the [//olam.wiki/nutrition.html 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: <pre> 50|Molasses 150|Onions, raw </pre> For each row, there must be the ''number of grams'', followed by the {{!}} character, followed by the ''name of the food'...")
 
(new domain name)
 
Line 1: Line 1:
The main use-case of this code, is to show the global [[food]] supply in the [//olam.wiki/nutrition.html Nutrition Calculator].
The main use-case of this code, is to show the global food supply in the [{{SERVER}}/nutrition.html 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:
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:
Line 12: Line 12:
<html><head><script>
<html><head><script>
  function generate() {
  function generate() {
   var url = 'https://olam.wiki/nutrition.html?targets=averaged&timescale=1';
   var url = 'https://thechange.wiki/nutrition.html?targets=averaged&timescale=1';
   var title = document.getElementById("title").value;
   var title = document.getElementById("title").value;
   if (title) url += '&title='+encodeURIComponent(title);
   if (title) url += '&title='+encodeURIComponent(title);

Latest revision as of 04:03, 10 November 2024

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://thechange.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>