PHP类使用静态DB表/列名。如何使动态?(最佳做法)

PHP类使用静态DB表/列名。如何使动态?(最佳做法),php,class,methodology,object-oriented-database,Php,Class,Methodology,Object Oriented Database,我找到了一个基于PHP的食谱和膳食计划脚本,我想编辑它 有一个MealPlan类,允许所有用户通过从数据库中找到的所有食谱列表中选择来创建自己的每日菜单/MealPlan 我想根据用户级别切换insert语句中使用的表/列名 最初的插入方法是: // Insert Meal Plan function insert($date, $meal, $recipe, $servings, $owner) { global $DB_LINK, $db_table_mealplans, $

我找到了一个基于PHP的食谱和膳食计划脚本,我想编辑它

有一个MealPlan类,允许所有用户通过从数据库中找到的所有食谱列表中选择来创建自己的每日菜单/MealPlan

我想根据用户级别切换insert语句中使用的表/列名

最初的插入方法是:

// Insert Meal Plan
function insert($date, $meal, $recipe, $servings, $owner) {
        global $DB_LINK, $db_table_mealplans, $LangUI;

        $date = $DB_LINK->addq($date, get_magic_quotes_gpc());
        $meal = $DB_LINK->addq($meal, get_magic_quotes_gpc());
        $recipe = $DB_LINK->addq($recipe, get_magic_quotes_gpc());
        $servings = $DB_LINK->addq($servings, get_magic_quotes_gpc());
        $owner = $DB_LINK->addq($owner, get_magic_quotes_gpc());

        $sql = "INSERT INTO $db_table_mealplans (
          mplan_date,
          mplan_meal,
          mplan_recipe,
          mplan_servings,
          mplan_owner)
          VALUES (
          '$date',
          $meal,
          $recipe,
          $servings,
          '$owner')";
        $rc = $DB_LINK->Execute($sql);
        DBUtils::checkResult($rc, NULL, $LangUI->_('Recipe already added on:'.$date), $sql);
}
我的想法是改变:

mplan_date,
mplan_meal,
mplan_recipe,
mplan_servings,
mplan_owner
致:

并使用switch语句更改表/列名。但有人告诉我,这种事情在OOP中是不受欢迎的。最好的办法是什么


(注意:如果需要,我可以发布整个课程,但我想这已经足够了)

我没有任何代码给你,但最近我做了类似的事情。基本上,我通过根据用户输入的内容对字符串进行浓缩来动态生成SQL字符串。请耐心听我说,我正在努力打字

$columnsArray = array();
$sqlCmd = "INSERT INTO";

If (someCondition) {
    array_push($columnsArray, "mplan_date");
}//Could try turning this into a loop and just push all 
 //applicable columns into the array

$counter = 0;

Foreach ($columnsArray as $columnName) {
    $sqlCmd .= " $columnName";
    $counter++
    If ($counter < count($columnsArray)){
        $sqlCmd .= ",";
    }//This will put a comma after each column with the 
     //exception of the last one.
}
$columnsArray=array();
$sqlCmd=“插入”;
如果(某些条件){
数组推送($columnsArray,“mplan\u日期”);
}//可以试着把它变成一个循环,然后把所有的
//数组中的适用列
$counter=0;
Foreach($columnsArray作为$columnName){
$sqlCmd.=“$columnName”;
美元柜台++
如果($counter
所以我只使用一个用户定义的select语句来检索数据。基本上,继续使用逻辑,直到您构建了一个自定义字符串,该字符串将是用户定义的SQL语句,然后执行它。这包括执行类似于上述脚本的操作,将值合并到字符串中


希望这能给你一些想法。

db\u table\u mealplan
中还有哪些列是你想切换到的?这听起来不像是编程问题,更像是应用程序设计/数据设计问题。如果您必须尝试完成类似的操作,那么您可能完全错了。此外,许多测试来自html中的
$\u POST
表单,并检查是否设置了其他变量以用于构建SQL语句。
$columnsArray = array();
$sqlCmd = "INSERT INTO";

If (someCondition) {
    array_push($columnsArray, "mplan_date");
}//Could try turning this into a loop and just push all 
 //applicable columns into the array

$counter = 0;

Foreach ($columnsArray as $columnName) {
    $sqlCmd .= " $columnName";
    $counter++
    If ($counter < count($columnsArray)){
        $sqlCmd .= ",";
    }//This will put a comma after each column with the 
     //exception of the last one.
}