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çã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?
This way nothing is printed on the screen.
– manzetti.denis
@Manzetti.Nis does not print even the empty cells in the table?
– rray
no. I made some changes:
if($pratos>0){
 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>";
 }

} var_dump($pratos)
from what I understand, has no value except in$pratos
. The program prints: array (size=0) Empty– manzetti.denis
@Manzetti.Nis The definition of
$pratos
is equal to the answer?– rray
it only prints the
$total
, which is the number of records in the columnid_prato
– manzetti.denis
Updated code: http://pastebin.com/TxaKH1ML <-
– manzetti.denis
Change your if from
if($pratos > 0)
forif(count($pratos) > 0)
. @Manzetti.Denis.– rray
I did, I used
$pratos = $pdo->select("SELECT * FROM prato");
and it worked.– manzetti.denis
Then the name of some column was misspelled in the query to not have returned anything. @Manzetti.Denis
– rray
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.denis
@Manzetti of a look in that question maybe the problem is the file encoding or the html output
– rray