-1

While loop doesn't work which is inside another while loop. Parse error: syntax error, unexpected '.' after while { it says.

$sql = "SELECT * FROM question where question_id={$update_id}";

$result = mysqli_query($conn, $sql) or die("query failed");

$output = '';

if(mysqli_num_rows($result) > 0){
while($question = mysqli_fetch_assoc($result)){
    $output .=" <form id='edit-question-form'>
        <div class='form-panel'>
            <label for='question-faculty'>Faculty:</label>
            <select name='faculty' id='question-faculty'>
            ".
            $sql2 = 'SELECT faculty_id,faculty_name FROM faculty;';
            $result2 = mysqli_query($conn, $sql2) or die('query failed');
            if(mysqli_num_rows($result2)){
            while($faculty = mysqli_fetch_assoc($result2)){
            ."<option value='{$faculty['faculty_id']}' ".(($question['faculty']==$faculty['faculty_id'])?'selected':'').">{$faculty['faculty_name']}</option>
            ".
            }
            }
            ."
            </select>
        </div>
    </form>
";
Dark Crow
  • 3
  • 2

1 Answers1

1

Try this revised edition:

$select = "";
$sql2 = 'SELECT faculty_id,faculty_name FROM faculty;';
         $result2 = mysqli_query($conn, $sql2);
         if(mysqli_num_rows($result2)){
              while($faculty = mysqli_fetch_assoc($result2)){
                   $select.= "<option value='{$faculty['faculty_id']}'";
                   $select.=  $question['faculty']==$faculty['faculty_id'] ? 'selected':'';
                   $select.= ">{$faculty['faculty_name']}</option>";"
              }
         }

$output = <<<EOT
    <div class='form-panel'>
         <label for='question-faculty'>Faculty:</label>
         <select name='faculty' id='question-faculty'>
         $select
         </select>
    </div>
EOT;

IMPORTANT - that last line EOT; must not have any spaces before it. It's indented here to be read as code, but in real PHP it cannot be indented.

Kinglish
  • 23,358
  • 3
  • 22
  • 43