Paging problem

3

I'm doing a pagination. So far so good, but when I'm on the homepage, it shows the results, everything ok. But when I click to go to the page 2 , it only changes the last result of the page.

example products.php

id 45,46,47,48,49 on this page everything is ok. But on page produtos.php?pagina=1 , it shows 46,47,48,49,50 .

    session_start();
    require '../conectaBanco.php';
    $itens_pagina = 5;
    if (isset($_GET['pagina'])) {
    $pagina = $_GET['pagina'];
    } else {
    $pagina = 0;
    }
    $sql_code = "SELECT * FROM produtos LIMIT $pagina, $itens_pagina";
    $execute = $conecta->query($sql_code) or die($$conecta->error);
    $produto = $execute->fetch_assoc();
    $num = $execute->num_rows;
    $num_total = $conecta->query("SELECT * FROM produtos")->num_rows;
    $num_paginas = ceil($num_total / $itens_pagina);
    
asked by anonymous 06.01.2017 / 15:11

1 answer

1

Try this:

session_start();
require '../conectaBanco.php';
$itens_pagina = 5;
if (isset($_GET['pagina']))
    $pagina = $_GET['pagina'] * $itens_pagina ; // mudei aqui
else
    $pagina = 0;

$sql_code = "SELECT * FROM produtos LIMIT $pagina, $itens_pagina";
$execute = $conecta->query($sql_code) or die($$conecta->error);
$produto = $execute->fetch_assoc();
$num = $execute->num_rows;
$num_total = $conecta->query("SELECT * FROM produtos")->num_rows;
$num_paginas = ceil($num_total / $itens_pagina);

When there is a page in the URL, you must multiply that number by the number of items you want on this page, for example:

  • Page 1 - > $pagina = 1 * 5 = 05, then list from 05, to 10
  • Page 2 - > $pagina = 2 * 5 = 10, then list 10, to 15
  • Page 3 - > $pagina = 3 * 5 = 15, then list 15, to 20
06.01.2017 / 15:16