public function addAction() { $p = $_REQUEST; if ($_SERVER['REQUEST_METHOD'] == "POST") { $pFid = empty($p['fid']) ? Tool_Fnc::ajaxMsg('食物id不能为空') : intval($p['fid']); $pTitle = empty($p['title']) ? Tool_Fnc::ajaxMsg('食物名称不能为空') : Tool_Fnc::safe_string($p['title']); $pAmount = empty($p['amount']) ? Tool_Fnc::ajaxMsg('数量不能为空') : floatval($p['amount']); $pWeight = empty($p['weight']) ? Tool_Fnc::ajaxMsg('重量不能为空') : floatval($p['weight']); $pUnit = empty($p['unit']) ? Tool_Fnc::ajaxMsg('单位不能为空') : Tool_Fnc::safe_string($p['unit']); $pCalory = empty($p['calory']) ? Tool_Fnc::ajaxMsg('卡路里不能为空') : floatval($p['calory']); $tTime = time(); $tData = array('fid' => $pFid, 'title' => $pTitle, 'amount' => $pAmount, 'weight' => $pWeight, 'unit' => $pUnit, 'created' => $tTime, 'calory' => $pCalory); $tDRM = new FoodunitsModel(); if (!$tDRM->insert($tData)) { Tool_Fnc::ajaxMsg('添加失败'); } Tool_Fnc::ajaxMsg('添加成功', 1); } }
public function indexAction() { exit; #$db = new SQLite3('/home/zhangyueru/data/www/dakang_www/shell/foods.db'); #$res = $db->query('select * from foods'); #$data = $db->fetchArray($res); #print_r($data); # header('Content-type:text/html;charset=utf-8'); $tFMO = new FoodModel(); $tFUMO = new FoodunitsModel(); $tFTMO = new FoodtagModel(); $tSqlite = new Orm_Sqlite('/home/zhangyueru/data/www/dakang_www/shell/food.db'); $res = $tSqlite->query('select a.*,s.name ctitle from (select fd.thumb_image_url,fd.large_image_url,fd.ingredient,lights,cate_id,fd.name,fd.units from foods f, food_detail fd where f.id = fd.parent_id) a,categories s where a.cate_id = s.id'); $ii = 0; while ($tRow = $res->fetchArray()) { $tTime = time(); $ii++; $large_image_url = ''; $thumb_image_url = ''; if (!empty($tRow['thumb_image_url']) && $tRow['thumb_image_url'] != 'null') { $thumb_image_url = '/foodimg/small/' . $tTime . '_' . rand(1000, 99999) . '.jpg'; $this->saveimg($tRow['thumb_image_url'], $thumb_image_url); echo 'thumb'; } if (!empty($tRow['large_image_url']) && $tRow['large_image_url'] != 'null') { $large_image_url = '/foodimg/' . $tTime . '_' . rand(1000, 99999) . '.jpg'; $this->saveimg($tRow['large_image_url'], $large_image_url); echo 'large'; } $i = json_decode($tRow['ingredient'], true); $l = json_decode($tRow['lights'], true); $tTag = array(); foreach ($l as $tKey => $tVal) { if (empty($tVal)) { continue; } $tTag[$tKey] = $tVal; } $tTag = implode('|', $tTag); $i['calcium'] = empty($i['calcium']) ? 0 : $i['calcium']; $i['vitamin_a'] = empty($i['vitamin_a']) ? 0 : $i['vitamin_a']; $i['zinc'] = empty($i['zinc']) ? 0 : $i['zinc']; $i['magnesium'] = empty($i['magnesium']) ? 0 : $i['magnesium']; $i['vitamin_c'] = empty($i['vitamin_c']) ? 0 : $i['vitamin_c']; $i['fiber_dietary'] = empty($i['fiber_dietary']) ? 0 : $i['fiber_dietary']; $i['selenium'] = empty($i['selenium']) ? 0 : $i['selenium']; $i['copper'] = empty($i['copper']) ? 0 : $i['copper']; $i['carbohydrate'] = empty($i['carbohydrate']) ? 0 : $i['carbohydrate']; $i['phosphor'] = empty($i['phosphor']) ? 0 : $i['phosphor']; $i['fat'] = empty($i['fat']) ? 0 : $i['fat']; $i['niacin'] = empty($i['niacin']) ? 0 : $i['niacin']; $i['vitamin_e'] = empty($i['vitamin_e']) ? 0 : $i['vitamin_e']; $i['manganese'] = empty($i['manganese']) ? 0 : $i['manganese']; $i['cholesterol'] = empty($i['cholesterol']) ? 0 : $i['cholesterol']; $i['iron'] = empty($i['iron']) ? 0 : $i['iron']; $i['kalium'] = empty($i['kalium']) ? 0 : $i['kalium']; $i['natrium'] = empty($i['natrium']) ? 0 : $i['natrium']; $i['thiamine'] = empty($i['thiamine']) ? 0 : $i['thiamine']; $i['protein'] = empty($i['protein']) ? 0 : $i['protein']; $i['lactoflavin'] = empty($i['lactoflavin']) ? 0 : $i['lactoflavin']; $i['calory'] = empty($i['calory']) ? 0 : $i['calory']; $i['carotene'] = empty($i['carotene']) ? 0 : $i['carotene']; $tSql = 'insert into food values("%s","%s",%d,%d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,"%s","%s",%s,"%s","%s")'; $tSql = sprintf($tSql, '', $tRow['name'], $tTime, 0, $i['calcium'], $i['vitamin_a'], $i['zinc'], $i['magnesium'], $i['vitamin_c'], $i['fiber_dietary'], $i['selenium'], $i['copper'], $i['carbohydrate'], $i['phosphor'], $i['fat'], $i['niacin'], $i['vitamin_e'], $i['manganese'], $i['cholesterol'], $i['iron'], $i['kalium'], $i['natrium'], $i['thiamine'], $i['protein'], $i['lactoflavin'], $i['calory'], $i['carotene'], $tTag, $thumb_image_url, $tRow['cate_id'], $tRow['ctitle'], $large_image_url); $tFid = $tFMO->exec($tSql); foreach ($l as $tKey => $tVal) { if (empty($tVal)) { continue; } $tData = array('title' => $tKey, 'fid' => $tFid, 'flag' => $tVal); $tFTMO->insert($tData); } $tUnitsArr = json_decode($tRow['units'], true); foreach ($tUnitsArr as $tURow) { $tData = array('amount' => $tURow['amount'], 'unit' => $tURow['unit'], 'fid' => $tFid, 'calory' => $tURow['calory'], 'weight' => $tURow['weight']); $tFUMO->insert($tData); } echo $ii . "\r\n"; } die; }