Question

Im new to php functions and I am currently trying to construct an if statement that looks into a database table to look at a column called tableformatting. This table is where the formatting options for my quiz questions are stored, e.g if I want the answers to my question displayed as a dropdown menu I would select that option when adding the quiz question, and then when it is pulled out of the database it will be formatted like that.

At present the if statement looks like this :

    <?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
            {}

     ?> 

and the formatting functions look like this :

<?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 //
    ?>

The problem I am having is the function is creating the dropdown list menu just fine, but the data isnt being filled in for some reason and the dropdown menu is just blank, if I take the code out of the function and run it normally though it all works as expected and the menu is populated with the answers.

Was it helpful?

Solution

You're trying to access the $questionID variable out of scope. Try passing it to the function, eg:

// 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>';
}

Then call the function from your first loop like so:

dropdownmenu($questionID)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top