summaryrefslogtreecommitdiff
path: root/food_quantity.php
diff options
context:
space:
mode:
authorNathan Kinkade <nath@nkinka.de>2008-02-03 23:23:24 +0000
committerNathan Kinkade <nath@nkinka.de>2008-02-03 23:23:24 +0000
commitd895b852a6e160496ffc760d46d3719a3d62ff86 (patch)
tree52230bb04148197e8312e09b5c5273417e7a3be9 /food_quantity.php
Initial checkin of nutridb.org and basic subversion directory structure
Diffstat (limited to 'food_quantity.php')
-rw-r--r--food_quantity.php70
1 files changed, 70 insertions, 0 deletions
diff --git a/food_quantity.php b/food_quantity.php
new file mode 100644
index 0000000..d86c6b3
--- /dev/null
+++ b/food_quantity.php
@@ -0,0 +1,70 @@
+<?php
+
+# include the main site config where various global variables
+# and libraries are included
+require("config.php");
+
+# if ndb_no wasn't passed to this script then just go to the
+# index page, as we can't do anything without it
+if ( isset($_REQUEST['food']) ) {
+ $food = $_REQUEST['food'];
+ $smarty->assign("food", $food);
+} else {
+ $_SESSION['systemMsg'] = "<span class='msgError'>You must specify a food.</span>";
+ header("Location: {$config->_rootUri}/");
+ exit;
+}
+
+# get the selected food and quantities from the database
+$sql = sprintf ("
+ SELECT foodDescs.sciname, %s AS foodDesc, weights.*
+ FROM foodDescs LEFT JOIN weights
+ ON foodDescs.ndb_no = weights.ndb_no
+ WHERE foodDescs.ndb_no = '%s'
+ ",
+ $db->_dbConn->Concat("foodDescs.long_desc", "', '", "foodDescs.comname"),
+ $food
+);
+$db->Select($sql);
+# if for some reason the ndb_no doesn't exist, then drop them where they
+# came from with an appropriate error message
+if ( $db->_rowCount > 0 ) {
+ $smarty->assign("foodQuantities", $db->_rows);
+} else {
+ $_SESSION['systemMsg'] = "<span class='msgError'>The food you specified doesn't seem to exist.</span>";
+ header("Location: {$config->_previousUri}");
+ exit;
+}
+
+# increment the counter for this food. this counter could be used for all
+# sorts of things, for example it is the basis of the "sort by popularity" option.
+# the more people that select this item, the higher in the sort list it
+# will appear.
+incrementPopularityCounter($food, "foodDescs");
+
+# some data housekeeping
+for ( $idx = 0; $idx < count($db->_rows); $idx++ ) {
+ # the data from the USDA frequently has low order, unnecessary zeros to the
+ # right of the decimal. here we strip off any superfluous zeros and/or
+ # decimal points
+ $db->_rows[$idx]['amount'] = trim($db->_rows[$idx]['amount'], ".0");
+
+ # trim any extra commas from the end of foodDesc, as may appear
+ # due to the concatenation of long_desc and comname with a comma
+ # where comname has no value, which it frequently doesnt'
+ $db->_rows[$idx]['foodDesc'] = trim($db->_rows[$idx]['foodDesc'], ", ");
+}
+
+$smarty->assign("foodQuantities", $db->_rows);
+
+# grab the various parts. these sections are not printed to the screen
+# but rather dumped into smarty variables that will simply be printed
+# in the template, so the order doesn't matter here at the moment
+require("header.php");
+require("sidebar_left.php");
+require("sidebar_right.php");
+require("footer.php");
+
+$smarty->display("food_quantity.tpl");
+
+?>