1

我是 php 函数的新手,我目前正在尝试构建一个 if 语句来查看数据库表以查看名为 tableformatting 的列。此表是存储我的测验问题的格式选项的位置,例如,如果我希望我的问题的答案显示为下拉菜单,我会在添加测验问题时选择该选项,然后当它从数据库中拉出时将被格式化。

目前 if 语句如下所示:

    <?php
    // Lookup Question Format & Format Using Function //
    $questionformat="SELECT * FROM itsnb_chronoforms_data_createquestions WHERE questionID='$questionID'";
    $questionformatresult =mysql_query($questionformat);
    while ($formatrow = mysql_fetch_array($questionformatresult)){
    $format = $formatrow['questionformat']; }

            if ($format == 'questionformatdropmenu')
            {dropdownmenu ();
            }
            else
            {}

     ?> 

格式化函数如下所示:

<?php
    // Question formatting functions //

    // Dropdown Menu
    function dropdownmenu(){

    echo '<select id="quizselectanswer" name="quizselectanswer" title="quizselectanswer">';

    $sql="SELECT * FROM itsnb_chronoforms_data_createquestions WHERE questionID='$questionID'";
    $result2 =mysql_query($sql);
    while ($data = mysql_fetch_array($result2)){

      echo '<option value ="' . $data['quizanswer1'] . '" >' . $data['quizanswer1'] . '</option>';
      echo '<option value ="' . $data['quizanswer2'] . '" >' . $data['quizanswer2'] . '</option>';
      echo '<option value ="' . $data['quizanswer3'] . '" >' . $data['quizanswer3'] . '</option>';
      echo '<option value ="' . $data['quizanswer4'] . '" >' . $data['quizanswer4'] . '</option>';
        }
      echo '</select>';
      }
    // Question formatting functions END //
    ?>

我遇到的问题是该函数可以很好地创建下拉列表菜单,但是由于某种原因没有填写数据并且下拉菜单只是空白,如果我将代码从函数中取出并正常运行它虽然它一切都按预期工作,菜单中填满了答案。

4

1 回答 1

1

您正在尝试访问$questionID超出范围的变量。尝试将其传递给函数,例如:

// Dropdown Menu
function dropdownmenu ( $questionID ) {
  echo '<select id="quizselectanswer" name="quizselectanswer" title="quizselectanswer">';

  $sql="SELECT * FROM itsnb_chronoforms_data_createquestions WHERE questionID='$questionID'";
  $result2 =mysql_query($sql);

  while ( $data = mysql_fetch_array( $result2 ) ) {
    echo '<option value ="' . $data['quizanswer1'] . '" >' . $data['quizanswer1'] . '</option>';
    echo '<option value ="' . $data['quizanswer2'] . '" >' . $data['quizanswer2'] . '</option>';
    echo '<option value ="' . $data['quizanswer3'] . '" >' . $data['quizanswer3'] . '</option>';
    echo '<option value ="' . $data['quizanswer4'] . '" >' . $data['quizanswer4'] . '</option>';
  }

  echo '</select>';
}

然后从你的第一个循环调用函数,如下所示:

dropdownmenu($questionID)
于 2012-11-30T15:34:37.853 回答