Empty variable - PHP

1

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

Invalid argument supplied for foreach() 

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

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();
    
asked by anonymous 16.02.2018 / 21:54

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 records in the selectAllPessoa() method. The datetime pattern accepted by most databases is YYYY-MM-DD HH:II:SS (Example: 2018-02-16 16:06:20). Verify that the POST value is coming in this same format.

Now about the error:

Invalid argument supplied for foreach()

In the selectAllPessoa() method, start the variable $grupo as an Empty Array , like this:

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 causes the variable sent to the foreach to be an array anyway, avoiding this error.

    
16.02.2018 / 22:12