-2

I'm receiving the following error "Parse error: syntax error, unexpected 'while' (T_WHILE), expecting ')' in /Applications/XAMPP/xamppfiles/htdocs/website-proiecte.dataserv.ro/includes/printAnexa2.inc.php on line 46".

Cannot figure where the error is. Here is also the code:


<?php

require_once ('../vendor/autoload.php');
require_once ('dbh.inc.php');

$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '../tmp']);

if(isset($_GET['report-id']))
    {
        $id = $_GET['report-id'];
        $sql = "SELECT * FROM planning
                inner join classes on classes.classesClass = planning.planningClass
                WHERE planningId = '$id'
                ";
        $result = mysqli_query($conn, $sql);

// raport anexa 2
$mpdf->AddPage('P');
$mpdf->WriteHTML('

<img src="../img/ue-logo.jpg" height="60" width="auto" />
<img style="padding-left: 32%;" src="../img/gr-logo.jpg" height="60" width="auto" />
<img style="padding-left: 32%;" src="../img/is-logo.jpg" height="60" width="auto" />

<br /><br />

<p style="text-align: right; padding-right: 5%;"><strong>ANEXA 2</strong></p>

<br />

<h3 style="text-align: center;">GRAFIC INSTRUIRE PRACTICĂ</h3>

<br /><br /><br /><br /><br />

<table style="border:1px solid black; width:100%;">
  <tr>
    <th>Clasa</th>
    <th>Saptamana</th>
    <th>Data Start</th>
    <th>Data Finalizare</th>
    <th>Ora incepere</th>
    <th>Ora terminare</th>
  </tr>
  <tr>'

  while($fetch = mysqli_fetch_array($result)) {'

    <td>' . $fetch['planningClass'] . '</td>
    <td>' . $fetch['planningWeek'] . '</td>
    <td>' . $fetch['planningDateStart'] .'</td>
    <td>' . $fetch['planningDateEnd'] . '</td>
    <td>' . $fetch['planningHourStart'] .'</td>
    <td>' . $fetch['planningHourEnd'] . '</td>
  </tr>
</table> ' } '

        ');
    }

$mpdf->Output();

  • 2
    Does this answer your question? [PHP parse/syntax errors; and how to solve them](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – El_Vanja May 07 '21 at 07:48
  • You can't put a `while` (or any other control structure) in the middle of a string. – El_Vanja May 07 '21 at 07:49
  • See [how to concatenate strings](https://stackoverflow.com/questions/8336858/how-to-combine-two-strings-together-in-php). It shows an example of adding to a variable with `.=`, which is what you should do here (put your string in a variable, add to it in the loop and then pass the variable to `writeHTML`). – El_Vanja May 07 '21 at 07:51
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman May 07 '21 at 09:24

1 Answers1

1

You cannot put the while loop inside a string.

put it outside and assign the result to string then concatenate that in the inside of the WriteHTML. see this code this should work for you.

    require_once ('../vendor/autoload.php');
    require_once ('dbh.inc.php');
    
    $mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '../tmp']);
    
    if(isset($_GET['report-id']))
        {
            $id = $_GET['report-id'];
            $sql = "SELECT * FROM planning
                    inner join classes on classes.classesClass = planning.planningClass
                    WHERE planningId = '$id'
                    ";
            $result = mysqli_query($conn, $sql);
    
    // raport anexa 2
    $row='';
    while($fetch = mysqli_fetch_array($result)) {
       $row.='
            <tr>
            <td>' . $fetch['planningClass'] . '</td>
            <td>' . $fetch['planningWeek'] . '</td>
            <td>' . $fetch['planningDateStart'] .'</td>
            <td>' . $fetch['planningDateEnd'] . '</td>
            <td>' . $fetch['planningHourStart'] .'</td>
            <td>' . $fetch['planningHourEnd'] . '</td>
            </tr>';
        }
    $mpdf->AddPage('P');
    $mpdf->WriteHTML('
        <img src="../img/ue-logo.jpg" height="60" width="auto" />
        <img style="padding-left: 32%;" src="../img/gr-logo.jpg" height="60" width="auto" />
        <img style="padding-left: 32%;" src="../img/is-logo.jpg" height="60" width="auto" />
        
        <br /><br />
        
        <p style="text-align: right; padding-right: 5%;"><strong>ANEXA 2</strong></p>
        
        <br />
        
        <h3 style="text-align: center;">GRAFIC INSTRUIRE PRACTICĂ</h3>
        
        <br /><br /><br /><br /><br />
        
        <table style="border:1px solid black; width:100%;">
        <tr>
            <th>Clasa</th>
            <th>Saptamana</th>
            <th>Data Start</th>
            <th>Data Finalizare</th>
            <th>Ora incepere</th>
            <th>Ora terminare</th>
        </tr>
        '.$row.'
        </table>
        
    
  
    
            ');
        }
    
    $mpdf->Output();
jacob Denis
  • 51
  • 11