0

I have a mcqs website, the problem is that when i try to load a quiz page with multiple questions and their answers, it loads fine.

Working code:

@foreach ($question->answers as $answer)
                  <span style="display:inline-block">
                      {{$answer->content}}
                   </span>
@endforeach

Not Working Code:

@foreach ($question->answers as $answer)
              <span style="display:inline-block">
                  {!! $answer->content !!}
               </span>
@endforeach


{!! $answer->content !!} is the same as echo $answer->content

however for the same code when i try to render the answers with HTML formatting using <?php echo , the page stops rendering midway. forexample if say the page was to load 30 questions , it will only load 10 and stops rendering . Even when i tried using {!!$answer->content!!} , the problem remains the same.

Note: i am returning set of questions using the below code:

 $questions = Question::all()->where('exam_id',$exam_id)->random($questionsCount)->shuffle();
 return view('quiz',compact('exam','questions','questionsCount','t'));

I have one to many relationship setup so i can extract the answers of the question using $question->answers it works fine when i don't echo any answer content.

Anyhelp will be greatly appreciated. Thanks!

Ali Waqas
  • 306
  • 2
  • 12

1 Answers1

1

Looks like an issue with unescaped text. When you use the blade template's escape method ({{ }}) it is successful. When you use the unescaped method, ({!! !!}) it fails. This is the same when using the unescaped php echo.

There is likely some type of character in one of your questions that is causing a break of the loop or php echo.

To fix, use the blade escape {{ }} or php escape (htmlentities(), htmlspecialchars() etc) in the echo.

htmlentities-vs-htmlspecialchars is worth checking out as well.

Watercayman
  • 7,970
  • 10
  • 31
  • 49
  • thanks a lot, using `echo htmlspecialchars($answer->content)` solved my problem. – Ali Waqas Jan 12 '20 at 18:33
  • However this doesn't echo HTML instead it only echoes the html as string! – Ali Waqas Jan 12 '20 at 18:37
  • 1
    Yes, this solves the problem - it was clearly some character that was stopping the loop. As you noticed, you are now producing *escaped* text instead of the HTML. This solves the original problem, but now you will have to *print* the characters as you want them on the screen. You'll either need to clean the input before (as it hits the DB), or un-escape it within display. If it is something that is breaking your loop - you are likely better off cleaning your data. But, if you want to show the original tags, research php escape and how to display those strings :) – Watercayman Jan 13 '20 at 01:45
  • in one answer it was `<>` that was breaking the loop. Where can i find list of characters that break code, also what could be done to them to display them as string and not html when required. – Ali Waqas Jan 13 '20 at 06:26
  • 1
    To start - look at whatever creates a tag or an escape (like `<>` or `@` or `;` for example) - just characters that signal a browser that something is coming, or a language, like PHP that it is time to end a line. Interestingly, you can't type an unescaped `<>` on this site's comments without it being ignored! For display - research how to translate the strings back - it will depend on how you encode / escape them, but it is a good lesson to learn as you will have to make decisions on how you want to do it from both sides (encode and decode). – Watercayman Jan 13 '20 at 20:08