Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要帮助重构我的冗余代码(使用MySQL/PHP填充/构建3个选择列表)_Php_Mysql_Sql_Database_Refactoring - Fatal编程技术网

需要帮助重构我的冗余代码(使用MySQL/PHP填充/构建3个选择列表)

需要帮助重构我的冗余代码(使用MySQL/PHP填充/构建3个选择列表),php,mysql,sql,database,refactoring,Php,Mysql,Sql,Database,Refactoring,我有一个膳食/食谱数据库,用于创建每日膳食计划。我需要创建3个不同的选择列表(早餐、午餐、晚餐),以显示每个餐名的可用食谱选项 到目前为止,我对上述每一项都使用单独的查询,并使用单独的构建来显示每个列表的结果 午餐查询: // Lunch options $sql = "SELECT plan_date, plan_meal, plan_recipe, recipe_name, recipe_serving_size FROM recipe_pla

我有一个膳食/食谱数据库,用于创建每日膳食计划。我需要创建3个不同的选择列表(早餐、午餐、晚餐),以显示每个餐名的可用食谱选项

到目前为止,我对上述每一项都使用单独的查询,并使用单独的构建来显示每个列表的结果

午餐查询:

  // Lunch options
  $sql = "SELECT plan_date,
    plan_meal,
    plan_recipe,
    recipe_name,
    recipe_serving_size
    FROM recipe_plans
    LEFT JOIN $table_meals ON meal_id = plan_meal    
    LEFT JOIN $table_recipes ON recipe_id = plan_recipe
    WHERE plan_date = '".$date."'
    AND meal_name = 'Lunch'
    AND plan_owner = '".$user_name."'
    ORDER BY recipe_name";
    $rc = $DB_LINK->Execute($sql);
    DBUtils::checkResult($rc, NULL, NULL, $sql);

  // Scan the rows of the SQL result
  while (!$rc->EOF) {
    $recipeList[($rc->fields['plan_recipe'])] = $rc->fields['recipe_name'] . " (" . $rc->fields['recipe_serving_size'] . ")";
    $rc->MoveNext();
  }
// Scan the fields in the SQL result row
// Print all existing Meals, and some new ones
$minShow = 1;
$maxShow = 1;  // only build 1 while testing
for ($i = 0; $i < (isset($MealPlanObj->mealplanItems[$date]) && ($i < $maxShow) ? count($MealPlanObj->mealplanItems[$date]) : 0) + $minShow; $i++) {
  if ($i < (isset($MealPlanObj->mealplanItems[$date]) ? count($MealPlanObj->mealplanItems[$date]) : 0)) {
    // If it is an existing meal item, then set it
    $meal = $MealPlanObj->mealplanItems[$date][$i]['meal']; // meal_id
    $servings = $MealPlanObj->mealplanItems[$date][$i]['servings'];
    $recipe = $MealPlanObj->mealplanItems[$date][$i]['id']; // recipe_id
  } else {
    // It is a new one, give it blank values
    $meal = NULL;
    $servings = $defaultServings;
    $recipe = NULL;
  }
  // The HTML Code to build the select list for 'Lunch'
}
和构建:

  // Lunch options
  $sql = "SELECT plan_date,
    plan_meal,
    plan_recipe,
    recipe_name,
    recipe_serving_size
    FROM recipe_plans
    LEFT JOIN $table_meals ON meal_id = plan_meal    
    LEFT JOIN $table_recipes ON recipe_id = plan_recipe
    WHERE plan_date = '".$date."'
    AND meal_name = 'Lunch'
    AND plan_owner = '".$user_name."'
    ORDER BY recipe_name";
    $rc = $DB_LINK->Execute($sql);
    DBUtils::checkResult($rc, NULL, NULL, $sql);

  // Scan the rows of the SQL result
  while (!$rc->EOF) {
    $recipeList[($rc->fields['plan_recipe'])] = $rc->fields['recipe_name'] . " (" . $rc->fields['recipe_serving_size'] . ")";
    $rc->MoveNext();
  }
// Scan the fields in the SQL result row
// Print all existing Meals, and some new ones
$minShow = 1;
$maxShow = 1;  // only build 1 while testing
for ($i = 0; $i < (isset($MealPlanObj->mealplanItems[$date]) && ($i < $maxShow) ? count($MealPlanObj->mealplanItems[$date]) : 0) + $minShow; $i++) {
  if ($i < (isset($MealPlanObj->mealplanItems[$date]) ? count($MealPlanObj->mealplanItems[$date]) : 0)) {
    // If it is an existing meal item, then set it
    $meal = $MealPlanObj->mealplanItems[$date][$i]['meal']; // meal_id
    $servings = $MealPlanObj->mealplanItems[$date][$i]['servings'];
    $recipe = $MealPlanObj->mealplanItems[$date][$i]['id']; // recipe_id
  } else {
    // It is a new one, give it blank values
    $meal = NULL;
    $servings = $defaultServings;
    $recipe = NULL;
  }
  // The HTML Code to build the select list for 'Lunch'
}
//扫描SQL结果行中的字段
//打印所有现有膳食和一些新膳食
$minShow=1;
$maxShow=1;//测试时仅构建1
对于($i=0;$i<(isset($MealPlanObj->mealplanItems[$date])&($i<$maxShow)?计数($MealPlanObj->mealplanItems[$date]):0)+$minShow;$i++){
如果($i<(设置($MealPlanObj->mealplanItems[$date])?计数($MealPlanObj->mealplanItems[$date]):0){
//如果它是现有的膳食项目,则设置它
$MEIN=$MealPlanObj->mealplanItems[$date][$i]['MEIN']];//膳食id
$servings=$MealPlanObj->mealplanItems[$date][$i]['servings'];
$recipe=$MealPlanObj->mealplanItems[$date][$i]['id'];//recipe\u id
}否则{
//这是一个新的,给它空白值
$MEIN=NULL;
$servings=$defaultServings;
$recipe=NULL;
}
//用于构建“午餐”选择列表的HTML代码
}
上面的代码对每顿饭的名字都是重复的,因为这就是我有限的技能留给我的地方,哈哈


问题是:与其为3个条件(早餐、午餐、晚餐)中的每一个编写单独的select和build语句,我怎样才能只编写1,将它们全部输出?

您已经使用了变量来构建SQL查询,因此您可以只引入另一个变量,如
$MEINE\u name
。此变量可应用于SQL语句。而不是:

$sql = "SELECT mplan_date,
  ...
  AND meal_name = 'Lunch'
  ...
然后你会写:

$meal_name = 'Lunch';
...
$sql = "SELECT mplan_date,
  ...
  AND meal_name = '" . mysqli_real_escape_string($meal_name) . "'
  ...
请注意函数
mysqli\u real\u escape\u string()
的使用,尽管在本例中并非绝对必要,但如果您不能绝对确定变量内部的内容,则必须对添加到SQL语句中的所有变量进行转义。您的示例代码易受SQL注入攻击

之后,您可以进一步将代码打包到函数中:

function buildSqlQuery($meal_name, $date, $user_name)
{
  $sql = "SELECT mplan_date,
    ...
    ORDER BY recipe_name";
  return $sql;
}

$sqlForBreakfast = buildSqlQuery('Breakfast', '2000-01-01', 'teddy');
$sqlForLunch = buildSqlQuery('Lunch', '2000-01-01', 'teddy');
$sqlForDinner = buildSqlQuery('Dinner', '2000-01-01', 'teddy');

我不明白。php代码中没有对餐名的引用。问题是什么?此外,我认为您可能可以制作一些循环
foreach
,以使代码更易于阅读。因此,这有助于我解决问题的前半部分……但我如何为3个列表中的每一个实现实际构建的自动化?同样,我们将用于构建sql select语句的代码放入函数中,我们可以将执行查询和写入HTML的代码放入函数中。只需将变量
$sqlforfast
传递给查询函数,然后将
$recipeList
传递给编写函数。这三种情况我们都可以重复。^^^^这应该是显而易见的。我想我是想得太多而导致“分析瘫痪”,哈哈,又是thx!