Handling CRUD Array PDO data

Asked

Viewed 324 times

0

list-dish.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 I organize this data to print the correct information in the tables?

1 answer

2


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

As 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 top block should be replaced by the one that does the same thing but 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 just call the correct keys their names are set 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>";
   }
}
  • This way nothing is printed on the screen.

  • @Manzetti.Nis does not print even the empty cells in the table?

  • no. I made some changes: if($pratos>0){&#xA; foreach ($pratos as $res) {&#xA; echo "<tr>";&#xA; echo " <td>". $res['id_prato'] ."</td>";&#xA; echo " <td>". $res['titulo'] ."</td>";&#xA; echo " <td>". $res['descricao'] ."</td>";&#xA; echo " <td>". $res['preco'] ."</td>";&#xA; echo "</tr>";&#xA; }&#xA;&#xA;} var_dump($pratos) from what I understand, has no value except in $pratos. The program prints: array (size=0) Empty

  • @Manzetti.Nis The definition of $pratos is equal to the answer?

  • it only prints the $total, which is the number of records in the column id_prato

  • Updated code: http://pastebin.com/TxaKH1ML <-

  • Change your if from if($pratos > 0) for if(count($pratos) > 0). @Manzetti.Denis.

  • I did, I used $pratos = $pdo->select("SELECT * FROM prato"); and it worked.

  • Then the name of some column was misspelled in the query to not have returned anything. @Manzetti.Denis

  • Just one more thing, it’s not printing characters like "c". In the database assigns the columns as utf8-bin. has something to do with?

  • @Manzetti of a look in that question maybe the problem is the file encoding or the html output

Show 6 more comments

Browser other questions tagged

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