0
 for ($i=0; $i<$total_questions_for_exam; $i++) {

        $question_id= $question_and_answers[$i]['question_id'];
        $numberofanswersperquestion = count_answers_belongToOne_questionNew($question_id);
        //die(var_dump($question_id)); 
        $student_answer_per_question = retrieve_student_result ($_SESSION['user_id'], $_GET['quiz_id'], $question_id);
         echo '   <table class="table table-bordered table-condensed table-datatable table-hover">
                    <tbody>

                        <tr>
                            <td style="text-align: left;" width="100%"><strong>Question '. ($i+1) .'</strong></td> 
                        </tr>
                        <tr>
                            <td style="text-align: left;" width="100%">' . $question_and_answers[$i]['question_name'] .'</td>
                        </tr>



                        <tr>
                            <td style="text-align: left;" width="100%" class="warning"><em>Question Not attempted</em><br><strong>Correct Answer is</strong><br>' . $numberofanswersperquestion[0]['answer_name'] . '</td>
                        </tr>

                        <tr>
                            <td style="height: 5px;" width="100%">&nbsp;</td>
                        </tr>

                        <tr>
                        <td style="height: 5px;" width="100%">&nbsp;</td>
                        </tr>

                    </tbody>
                    </table>';

 }

Hi guys I have this for loop, what I am trying to do is, only print this

<tr>
    <td style="text-align: left;" width="100%" class="warning"><em>Question Not attempted</em><br><strong>Correct Answer is</strong><br>' . $numberofanswersperquestion[0]['answer_name'] . '</td>
</tr>

based on an if statement. But how can put that if statement inside that echo ??

I have tried this way, by

' . if() {} . '

but I couldn't get it to work this way.

this is what I want inside the if statement

if($student_answer_per_question === '' || '') {
 <tr>
    <td style="text-align: left;" width="100%" class="warning"><em>Question Not attempted</em><br><strong>Correct Answer is</strong><br>' . $numberofanswersperquestion[0]['answer_name'] . '</td>
</tr>
}

I hope I have explained the problem in detail and I appreciate any help from you guys. Thanks

dark_illusion_909099
  • 1,067
  • 2
  • 21
  • 41

2 Answers2

1

You can temporarily define a variable to store your output.

$output = '<table class="table table-bordered table-condensed table-datatable table-hover">
                <tbody>

                    <tr>
                        <td style="text-align: left;" width="100%"><strong>Question '. ($i+1) .'</strong></td> 
                    </tr>
                    <tr>
                        <td style="text-align: left;" width="100%">' . $question_and_answers[$i]['question_name'] .'</td>
                    </tr>';
if(condition)
{
$output .= '             <tr>
                        <td style="text-align: left;" width="100%" class="warning"><em>Question Not attempted</em><br><strong>Correct Answer is</strong><br>' . $numberofanswersperquestion[0]['answer_name'] . '</td>
                    </tr>';
}
$output .=              '<tr>
                        <td style="height: 5px;" width="100%">&nbsp;</td>
                    </tr>

                    <tr>
                    <td style="height: 5px;" width="100%">&nbsp;</td>
                    </tr>

                </tbody>
                </table>';
Jacky Chong
  • 172
  • 1
  • 10
-1

Create a variable, before the echo statement for example $statement and use PHP Shorthand If/Else, something like this

$statement = ($student_answer_per_question == '')? "Option 1" : "Option 2";

Now you can just print the $statement within your echo statement, as

echo "blah blah $statement blah blah";
Vaibhav Sidapara
  • 159
  • 2
  • 12