Jump to content

Como fazer pagina seguinte e anterior e os produtos alterarem


Recommended Posts

Posted (edited)

Este código abaixo é para passar para a pagina 2 e assim sucessivamente dependendo do numero de produtos tiver na bd, sendo que so quero apresentar 8 em cada pagina.

Este código tem algo certo. Mas não troca para a seguinte quando chega aos 8 e existe mais produtos. será que me pode ajudar?

$n=0;
$pagina=0;
$pagina = array_key_exists('pagina', $_REQUEST) ? (int) $_REQUEST['pagina'] : 1;
$pagina = array_key_exists('pagina', $_REQUEST) ? (int) $_REQUEST['pagina'] : 2;
$registosPorPagina = array_key_exists('produto_id', $_REQUEST) ? (int) $_REQUEST['produto_id'] : $n;

$offset = $pagina > 1 ? ($pagina * $registosPorPagina - $registosPorPagina) : 0;
$limit = $registosPorPagina;
$offset = $pagina > 2 ? ($pagina * $registosPorPagina - $registosPorPagina) : 0;
$limit = $registosPorPagina;

$sql = "SELECT * FROM produtos ORDER BY produto_id LIMIT $offset, $limit";
$sql = "SELECT COUNT(*) AS n FROM produtos";

$anterior = $pagina > 1 ? $pagina - 1 : 1;
$seguinte = $pagina + 1;

print "<a href='" . $_SERVER['PHP_SELF'] . "?NumRegistos=4&pagina=" . $anterior . "'>Página Anterior</a><BR/>";
print "<a href='" . $_SERVER['PHP_SELF'] . "?NumRegistos=4&pagina=" . $seguinte . "'>Página Seguinte</a>";
?>
Edited by Angela_Santos
Posted

onde e' que estas a executar as querys?

Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender.

A beleza de um código está em decompor problemas complexos em pequenos blocos simples.

"learn how to do it manually first, then use the wizzy tool to save time."

"Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."

Posted

tens que executar o sql, com por exemplo com a funcao mysqli_query

Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender.

A beleza de um código está em decompor problemas complexos em pequenos blocos simples.

"learn how to do it manually first, then use the wizzy tool to save time."

"Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."

Posted (edited)
<?php

$pagina = array_key_exists('pagina', $_REQUEST) ? (int) $_REQUEST['pagina'] : 1;
$registosPorPagina = 8;

$offset = $pagina > 1 ? ($pagina * $registosPorPagina - $registosPorPagina) : 0;
$limit = $registosPorPagina;

$sql = "SELECT * FROM produtos ORDER BY produto_id LIMIT $offset, $limit";
$sql2 = "SELECT COUNT(*) AS n FROM produtos";

$anterior = $pagina > 1 ? $pagina - 1 : 1;
$seguinte = $pagina + 1;

print "<a href='" . $_SERVER['PHP_SELF'] . "?NumRegistos=4&pagina=" . $anterior . "'>Página Anterior</a><BR/>";
print "<a href='" . $_SERVER['PHP_SELF'] . "?NumRegistos=4&pagina=" . $seguinte . "'>Página Seguinte</a>";
Edited by yoda
  • Vote 1

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
×
×
  • Create New...

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.