Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

N3lson

[Resolvido] Paginação (erro)

Recommended Posts

N3lson

boas tenho este código para fazer a paginação mas algo está mal, poderiam-me ajudar?


//paginação
       $records_per_page = 20;
       $row = mysqli_query($conexao, "SELECT COUNT(*) FROM susp");
       $total = mysqli_num_rows($row);

       $page_count = ceil($total / $records_per_page);

       $page = 1;
       if (isset($_GET['page']) && $_GET['page'] >= 1 && $_GET['page'] <= $page_count) {
           $page = (int)$_GET['page'];
       }
       $skip = ($page - 1) * $records_per_page;

       $resultados = mysqli_query($conexao, "SELECT * FROM susp ORDER BY data DESC LIMIT $skip, $records_per_page");

dá apenas uma página e deveria dar pelo menos 3


You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

Share this post


Link to post
Share on other sites
HappyHippyHippo
$row = mysqli_query($conexao, "SELECT COUNT(*) FROM susp"); // o que retorna este SQL ?
$total = mysqli_num_rows($row);                             // quantas linhas tem o resultado ?


IRC : sim, é algo que ainda existe >> #p@p

Share this post


Link to post
Share on other sites
N3lson

Quantas linhas tem ou deveria ter ?

Apresenta 1

Isto da paginação, não encaixei ainda :)

Edited by N3lson

You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

Share this post


Link to post
Share on other sites
N3lson

Código completo

echo $table;
$i=0;

//paginação
       $records_per_page = 20;
       $row = mysqli_query($conexao, "SELECT COUNT(*) FROM susp");
       $total = mysqli_num_rows($row);

       echo ($total);

       $page_count = ceil($total / $records_per_page);

       $page = 1;
       if (isset($_GET['page']) && $_GET['page'] >= 1 && $_GET['page'] <= $page_count) {
           $page = (int)$_GET['page'];
       }
       $skip = ($page - 1) * $records_per_page;

       $resultados = mysqli_query($conexao, "SELECT * FROM susp ORDER BY data DESC LIMIT $skip, $records_per_page");


while($tabela =  mysqli_fetch_assoc($resultados))
 {
    //print_r(strlen($tabela['soda_kg'])); // debug de variáveis
   echo ($i % 2)?'<tr class="odd">':'<tr class="even">';// cor alternada
   echo"<td>". $tabela['data']."</a></td>";
   echo"<td>". $tabela['hora']."</td>";
   echo"<td>". number_format($tabela['zinco'],1)."</td>";
   echo"<td>". number_format($tabela['soda'],1)."</td>";
   echo"<td>". number_format($tabela['minutos'],1)."</td>";
   if (strlen($tabela['soda_kg']<=1)){
   echo"<td>". number_format($tabela['soda_kg'],2)."</td>";
   }
   else{
   echo"<td>". number_format($tabela['soda_kg'],1)."</td>";
   }
   echo"<td>". $tabela['abrilh']."</td>";
   echo"<td>". $tabela['correct']."</td>";
   echo"<td>". $tabela['lcd']."</td>";
   echo"<td>". number_format($tabela['form'],1)."</td>";
   echo"<td>". $tabela['resp']."</td>";
   echo"</tr>";
   $i++;    // cor alternada
 }
echo '</table>';
echo 'Registos: ';
for ($x = 1; $x <= $page_count; ++$x) {
           echo '<a href="' . $_SERVER['PHP_SELF'] . '?page=' . $x . '"><span>'. $x . '</span></a> ';
       }


You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

Share this post


Link to post
Share on other sites
HappyHippyHippo
$row = mysqli_query($conexao, "SELECT COUNT(*) FROM susp"); // este SQL resulta num único registo com um número
$total = mysqli_num_rows($row);                             // $total será sempre 1

// o que queres é
$record = mysqli_fetch_array($row):
$total = $record[0];

  • Vote 1

IRC : sim, é algo que ainda existe >> #p@p

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.