MPDF mounting with php foreach , bootstrap and html

Asked

Viewed 101 times

2

I am trying to generate the MPDF, but this error appears:

PHP Parse error: syntax error, Unexpected 'foreach' (T_FOREACH)

This error is accusing in the second line that is the first foreach.

$prova = unserialize($row['prova']);

$html = " <div class='row'>
            ".foreach($prova[0] as $idPergunta => $rowPergunta) {."
              <div class='col-sm-6'>
                  <div class='row'>
                      <div class=''>
                          <div class='form-group'>
                              <h4><b>".echo $rowPergunta;."</b></h4>
                          </div>
                      </div>
                  </div>

                  <div class='row'>
                      <div class=''>
                          <div class='form-group'>
                              ".foreach($prova[1][$idPergunta] as $rowRespostas) {."
                                ".echo $rowRespostas;."
                              "}"
                          </div>
                      </div>
                  </div>

              </div>

                ".$rowCount++;
                if($rowCount % $numOfCols == 0) echo '</div><div class='row'>';
                $i++; }."

            ".}."
          </div>
        ";

1 answer

3


You are concatenating a foreach, and that’s a syntax error. foreach is not an expression or method to be concatenated. The ideal in this scenario is to dispose the contents of the foreach directly in the string $html:

$prova = unserialize($row['prova']);

$html = " <div class='row'>
            <?php foreach($prova[0] as $idPergunta => $rowPergunta) { ?>
              <div class='col-sm-6'>
                  <div class='row'>
                      <div class=''>
                          <div class='form-group'>
                              <h4><b><?php echo $rowPergunta . '<br/>'; ?></b></h4>
                          </div>
                      </div>
                  </div>

                  <div class='row'>
                      <div class=''>
                          <div class='form-group'>
                              <?php foreach($prova[1][$idPergunta] as $rowRespostas)
                                 echo $rowRespostas . <br/>;
                              } ?>
                          </div>
                      </div>
                  </div>

              </div>

                <?php
                $rowCount++;
                if($rowCount % $numOfCols == 0) 
                      echo '</div><div class=\'row\'>';
                $i++;

            } ?>
          </div>
        ";

This code will make a post-processing so that echo $html; is performed. This way, the code will execute after the procedure.

Scenario update

The method WriteHTML of lib MPDF prints an HTML code only, and PHP does not run. Therefore, post-processing does not work on this occasion.

You can also do with preprocessing, thus, HTML already comes pure and ready, without PHP built in:

$html = "<div class='row'>";

foreach($prova[0] as $idPergunta => $rowPergunta) {
    $html .= "<div class='col-sm-6'>
                  <div class='row'>
                      <div class=''>
                          <div class='form-group'>
                              <h4><b>".echo $rowPergunta;."</b></h4>
                          </div>
                      </div>
                  </div>

                  <div class='row'>
                      <div class=''>
                          <div class='form-group'>";

    foreach($prova[1][$idPergunta] as $rowRespostas)
        $html .= $rowRespostas . "<br/>";
    }

    $html .= "</div> </div> </div> </div>";

    $rowCount++;
    if($rowCount % $numOfCols == 0) 
         $html .= "</div><div class='row'>";
    $i++;
}

echo $html;
  • 1

    Thank you so much for your help. Using the first example, this error appears PHP Parse error: syntax error, unexpected '$rowPergunta'.

  • I opted for the first, because the indentation is visually better.

  • I didn’t pay attention and had a line that was being processed in the code before moving to the text. I fixed it in the edit.

  • See how it appears https://prnt.sc/oz933u

  • The way out of this $html goes to an interpreter that supports PHP? Apparently not. In this case, you will have to use preprocessing.

  • I am using https://github.com/mpdf/mpdf

  • 1

    The Second worked out!

  • 1

    Thank you very much.

  • The only thing that is not happening is the formatting in two columns. You know to inform the reason?

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.