0

我试图通过从数据库中获取数据来使这个脚本更加自动化,而不是逐个输入每个循环。

这就是我得到的:

我希望变量“$test”每次都不同,所以例如我的结果如下:

$test1 = "entry1;entry2;entry3;";
$test2 = "entry1;entry2;entry3;";

这是我得到的脚本,它适用于一个变量测试,但我需要很多不同变量的结果。

$query = "SELECT * FROM course";
$result = mysql_query($query);

while($row = mysql_fetch_array($result))
{
$course_menuname = $row['course_menuname']; #E.G breakfast_cereal, breakfast_toast, lunch_main
$course_item = $row['course_jsname']; #E.G cereal, toast, jacketpotato
 if(!empty($_POST[''.$course_menuname.''])) {
   foreach($_POST[''.$course_menuname.''] as $course_item) {
    $course_item = trim($course_item);
    if(!empty($course_item)) {
      $test .= "$course_item;";
    }
   }
 }
}

echo $test;

这是我之前多次使用的,但我希望它更加自动化。

 if(!empty($_POST['lunchmain_selection'])) {
   foreach($_POST['lunchmain_selection'] as $lunchmain) {
    $lunchmain = trim($lunchmain);
    if(!empty($lunchmain)) {
      $lunchmainchoices .= "$lunchmain;";
    }
   }
  }
 if(!empty($_POST['jacketpotato_selection'])) {
   foreach($_POST['jacketpotato_selection'] as $lunchjacketpotato) {
    $lunchjacketpotato = trim($lunchjacketpotato);
    if(!empty($lunchjacketpotato)) {
      $lunchjacketpotatochoices .= "$lunchjacketpotato;";
    }

} }

4

1 回答 1

0

听起来你可以使用函数:这是一个例子

function thisLoopDoesSomething($loop_array) {
    // Don't bother doing anything if it's an empty array
    if (!empty($loop_array)) {
        return;
    }

    // Your loop code here, using the passed in variable
}

thisLoopDoesSomething($_POST['lunchmain_selection']);
thisLoopDoesSomething($_POST['jacketpotato_selection']);
...
于 2013-10-29T17:46:19.627 回答