Handling Data Array PDO CRUD

0

Lista-prato.php:

  <?php 
    //inclui as bibliotecas
    require_once('conexao.class.php');
    //faz a canexão 
    $pdo = new Conexao();

    // determina o numero de registros que serão visualisados
    $maximo = 20;
    // armazenamos o valor da pagina atual
    $pagina = isset($_GET['pagina']) ? ($_GET['pagina']) : '1';
    // subtraimos 1, por que os registros sempre começam do 0
    $inicio = $pagina - 1;
    //multiplicamos a quantidade de registros pelo valor da pagina atual
    $inicio = $maximo * $inicio;


    $strCount = $pdo->select("SELECT COUNT(*) AS 'id_prato', 'titulo', 'descricao', 'preco' FROM prato");

    $total = 0;
    $pratoId = 0;

if(count($strCount)){
    foreach ($strCount as $row)  {
        // armazeno total de registros da tabela para fazer paginação
        $total = $row["id_prato"];

    }

    $pratoid = $pdo->select("SELECT  id_prato FROM prato");
    $titulo = $pdo->select("SELECT  titulo FROM prato");
    $descricao = $pdo->select("SELECT  descricao FROM prato");
    $preco = $pdo->select("SELECT  preco FROM prato");
    }
echo "<pre><p> Total de pratos cadastrados</p>" . print_r($total, 1) . "<pre>";
?>

        <table class="tabela1">
            <colgroup>
                <col class="coluna1"/>
                <col class="coluna2"/>
                <col class="coluna3"/>
            </colgroup>
            <caption>Pagina&ccedil;&atilde;o com PHP</caption>          
            <thead>
                <tr>
                    <th>Codigo</th>
                    <th>Prato</th>
                    <th>Descricao</th>
                    <th>Preco</th>
                </tr>
            </thead>
            <tbody>
            <?php
                //se a tabela nao estiver vazia, percorremos linha por linha pegando os valores
                if(count($strCount)){
                    foreach ($strCount as $res) {
                        echo "<tr>";
                        echo "  <td>".print_r($pratoid, 1)."</td>";
                        echo "  <td>".print_r($titulo, 1)."</td>";
                        echo "  <td>".print_r($descricao, 1)."</td>";
                        echo "  <td>".print_r($preco, 1)."</td>";
                        echo "</tr>";

                    }
                }
            ?>

RESULT:

         Total de pratos cadastrados
    8
                Paginação com PHP          

 Codigo         Prato           Descricao                       Preco

                                     Array
    (
    )
        Array
    (
        [0] => Array
            (
                [titulo] => xx
                [0] => xx
            )

        [1] => Array
            (
                [titulo] => f
                [0] => f
            )

        [2] => Array
            (
                [titulo] => adad
                [0] => adad
            )

        [3] => Array
            (
                [titulo] => aa
                [0] => aa
            )

        [4] => Array
            (
                [titulo] => f
                [0] => f
            )

        [5] => Array
            (
                [titulo] => czx
                [0] => czx
            )

        [6] => Array
            (
                [titulo] => zc
                [0] => zc
            )

        [7] => Array
            (
                [titulo] => ddd
                [0] => ddd
            )

    )
        Array
    (
        [0] => Array
            (
                [descricao] => xx
                [0] => xx
            )

        [1] => Array
            (
                [descricao] => f
                [0] => f
            )

        [2] => Array
            (
                [descricao] => dasda
                [0] => dasda
            )

        [3] => Array
            (
                [descricao] => aa
                [0] => aa
            )

        [4] => Array
            (
                [descricao] => f
                [0] => f
            )

        [5] => Array
            (
                [descricao] => zxc
                [0] => zxc
            )

        [6] => Array
            (
                [descricao] => zxc
                [0] => zxc
            )

        [7] => Array
            (
                [descricao] => dd
                [0] => dd
            )

How do I organize this data to print the correct information in the tables?

    
asked by anonymous 18.05.2015 / 05:16

1 answer

2

print_r() is a function to print the structure of a good array / object to perform debugging, in which case it is not necessary to use it. To display the values correctly, make a foreach and use the name of the matching keys.

Since the information is from the same table, it is not necessary to make a query for each column.

$pratoid = $pdo->select("SELECT  id_prato FROM prato");
$titulo = $pdo->select("SELECT  titulo FROM prato");
$descricao = $pdo->select("SELECT  descricao FROM prato");
$preco = $pdo->select("SELECT  preco FROM prato");

The above block should be replaced by the one that does the same thing using a query.

$pratos = $pdo->select("SELECT  id_prato, titulo, descricao, preco FROM prato");

//Caso queira exibir todos os campos troque os nomes por asterisco
//$pratos = $pdo->select("SELECT * FROM prato");

To display the information of the dishes simply call the correct keys whose names are defined in the sql query.

if(count($strCount)){
   foreach ($pratos as $res) {
      echo "<tr>";
      echo "  <td>". $res['id_prato'] ."</td>";
      echo "  <td>". $res['titulo'] ."</td>";
      echo "  <td>". $res['descricao'] ."</td>";
      echo "  <td>". $res['preco'] ."</td>";
      echo "</tr>";
   }
}
    
18.05.2015 / 05:40