3
I have a blog embedded on the site and I want to limit the number of posts per page, but the problem you’re giving me is that it doesn’t limit my posts.
Code
///Ler posts do blog da página atual
$result = mysql_query("$busca LIMIT $inicio,$total_reg");
$num_registos = mysql_affected_rows();
for ($i = 1; $i <= $num_registos; $i++){
$linha = mysql_fetch_array($result);
//declarar variaveis
$id=$linha["id"];
$busca = "SELECT * FROM blog";
$total_reg = "4"; // número de registos por página
$pagina=$_GET['pagina'];
if (!$pagina) {
$pc = "1";
} else {
$pc = $pagina;
}
$inicio = $pc - 1;
$inicio = $inicio * $total_reg;
$limite = mysql_query("$busca LIMIT $inicio,$total_reg");
$todos = mysql_query("$busca");
$tr = mysql_num_rows($todos); // verifica o número total de registros
$tp = $tr / $total_reg; // verifica o número total de páginas
$anterior = $pc -1;
$proximo = $pc +1;
if ($pc>1) {
echo " <a class='blog-page-link' href='?pagina=$anterior'><i class='fa fa-arrow- left'></i> Anterior</a> ";
}
if ($pc<$tp) {
echo " <a class='blog-page-link' href='?pagina=$proximo'>Seguinte <i class='fa fa-arrow-right'></i></a>";
}
that code is doing everything right, I really need to get you to limit posts that I can’t :c
– cloud
But what’s going on? It’s looking for all the posts, or none? Or are you making a mistake? Explain better...
– Franchesco
Tip: You can delete
?>
at the end, as a space after it can generate headaches.– Franchesco
i currently have 4 posts in the comics, yes it will get me the 4, I in the code limited to 3 and yet on the first page appears me the 4, and if I click the next button appears me the 4 posts in the same. What happened was: 1st page - 3 posts; 2nd page - 1post.
– cloud
seeing will be easier: 1st page : http://gyazo.com/04f3929eeedcf38bc5bf248c13830bf5 2nd page : http://gyazo.com/99287e8a247e25ded3db17fa2971dc66
– cloud
in your loop it is interesting to know whether it is using $limit or $total
– Otto
@cloud you have to put the code that "prints" the data in the browser.
– Jorge B.
Dude I took the test here and it worked, I think you’re doing the loop with the variable that brings all the records and not with the one that brings the limited ones. And a hint, tries to switch to the library mysqli, for the mysql will be discontinued soon. Another tip, try using a library to manage DB type a Connectionmsi
– KaduAmaral
exact was what I said it has two variables with the $limit query and $total it must be using the $total
– Otto