Empty variable - PHP

Asked

Viewed 65 times

1

I have two pages (index.php) and (report.php). index.php has a modal that takes the data entered by the user (start date and end date). Through this imputed information, it is directed to the report.php. The problem that variables are not being fed into the.php report, generating the error below:

Invalid argument supplied for foreach() 

If I access the.php report and put a fixed value in $sql, the report is generated without problems, however if it depends on the values imputed by the user, the report is generated blank.

relatorio.php




 <?php

#include("conexao.php");
include("mpdf.php");

$grupo = selectAllPessoa();
$datainicio = $_POST['starts_at'];
$datafim = $_POST['ends_at'];

function abrirBanco(){
    $conexao = new mysqli("localhost", "xxx", "xxxx", "xxx");
    return $conexao;
}


function selectAllPessoa(){
    $banco = abrirBanco();
    $sql = ("select * FROM xxxx WHERE resolution BETWEEN ('$datainicio') AND ('$datafim')");
    $resultado = $banco->query($sql);
    $banco->close();
   while ($row = mysqli_fetch_array($resultado)) {

      $grupo[] = $row;
    }
   return $grupo;
}

$mpdf = new mPDF();
$mpdf->SetDisplayMode("fullpage");
$mpdf->WriteHTML("<h1>Relatorio - Denuncia</h1><hr/>");

$html = "<table>
            <thead>
                <tr>
                    <th>Nickname</th>
                    <th>Sala</th>
                    <th>Data </th>

                </tr>
            </thead>
            <tbody>";
               foreach ($grupo as $pessoa) {
$html = $html ."    <tr>
                    <td>{$pessoa["nickname"]}</td>
                    <td>{$pessoa["sala"]}</td>
                    <td>{$pessoa["resolution"]}</td>
                     </tr>";
}
          $html = $html ."  </tbody>
        </table>";

$mpdf->WriteHTML($html);
$mpdf->Output();
exit();

1 answer

1

Probably the start and end dates are not coming in a format accepted by the database, so it does not return any record in the method selectAllPessoa(). The datetime pattern accepted by most databases is YYYY-MM-DD HH:II:SS (Example: 2018-02-16 16:06:20). Check if the POST value is coming in this same format.

Now about the mistake:

Invalid argument supplied for foreach()

In the method selectAllPessoa(), start the variable $grupo as a Empty array, thus:

function selectAllPessoa(){
    $banco = abrirBanco();
    $sql = ("select * FROM xxxx WHERE resolution BETWEEN ('$datainicio') AND ('$datafim')");
    $resultado = $banco->query($sql);
    $banco->close();
    $grupo = array(); // <- Adicione esta linha
    while ($row = mysqli_fetch_array($resultado)) {

      $grupo[] = $row;
    }
   return $grupo;
}

This makes the variable sent to foreach an array anyway, avoiding this error.

  • It includes the suggested line and the error no longer occurs, but the report still remains blank. The form is sending in the right format 0000-00-00.I believe it is not receiving the post values.

  • That’s right, I inserted the variables $datainicio = $_POST['starts_at']; $datafim = $_POST['ends_at']; within the function selectAllPessoa () and solved. Thank you for your attention.

Browser other questions tagged

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