Blog pagination, post limitation

Asked

Viewed 151 times

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

  • But what’s going on? It’s looking for all the posts, or none? Or are you making a mistake? Explain better...

  • Tip: You can delete ?> at the end, as a space after it can generate headaches.

  • 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.

  • seeing will be easier: 1st page : http://gyazo.com/04f3929eeedcf38bc5bf248c13830bf5 2nd page : http://gyazo.com/99287e8a247e25ded3db17fa2971dc66

  • 1

    in your loop it is interesting to know whether it is using $limit or $total

  • @cloud you have to put the code that "prints" the data in the browser.

  • 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

  • exact was what I said it has two variables with the $limit query and $total it must be using the $total

Show 4 more comments

2 answers

2

as I had said you are thus mounting your query:

$result = mysql_query("select * from blog "); 

And it should be so:

$result = mysql_query("$busca LIMIT $inicio,$total_reg");
  • now I do not appear any post

  • 1

    I’ll tuck you in to put the full code of the page .... but still you already know that this query is without any filter... will show everything even... still I think you should study a little more because the answer was given, you just do not know how to put into practice properly

2

Note the structure well. In your code, you cannot use a variable before you have declared:

///Ler posts do blog da página atual
$result = mysql_query("$busca LIMIT $inicio,$total_reg");

Like you did there at the beginning. The variables $busca, $inicio, $total_reg had not yet been declared.

To search for the limited records in your code and paginate you needed the number of records, which also hadn’t been done yet. So try to leave all your search logic and calculations first, and finally do the loop for displaying records and links from next page and previous page.

$busca = "SELECT * FROM blog";
$total_reg = 4; // número de registos por página


// Verifica se a variável $_GET['pagina'] foi informada na URL, 
// Caso sim, atribui o valor dela, caso contrário atribui 1
$pc = (isset($_GET['pagina']) ? $_GET['pagina'] : 1); 


$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 

///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"];
    /**
     * AQUI VAI O LOOP PARA EXIBIÇÃO DOS POSTS DO BLOG
    **/
}

$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>"; 
} 

Search to validate variables $_GET as I did there, before using them, so that no error is generated.

I suggest taking a look at logic and also starting for Object Orientation. Stop by the blog Devcia, I’m publishing some posts for beginners there.

  • Very good @Carlos

  • Thanks @Otto, when we are starting it is complicated, I wish that when I was learning, had someone to help me in these "silly", rsrs.

  • 1

    I just think you need a little more effort ... like help me and don’t do it for me

  • It is true... If the guy does not run after, then has no future in programming. Because the profession can be summarized in "solve problems", rs.

  • I know, I’m sorry about that :S I’ll try to take a look at that ;) and thank you to 2

  • Good @cloud, managed to solve with the model I presented there?

  • yes gave thanks :)

  • Fine, then mark as solved. ;)

  • I even marked but I see here nothing to mark :D

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.