Paging blog, limiting posts

3

I have a blog embedded in the site and I want to limit the number of posts per page, but the problem you are giving me is that it does not 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>"; 
} 
    
asked by anonymous 16.09.2014 / 12:28

2 answers

2

As I said, you are setting up your query:

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

And it should look like this:

$result = mysql_query("$busca LIMIT $inicio,$total_reg");
    
16.09.2014 / 13:45
2

Notice the structure well. In your code, you can not use a variable before you have declared:

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

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

To get the limited records in your code and make the paging you needed the number of records, which had not been done yet. Then try to leave all of your search logic and calculations first, and finally loop to display the records and links on the next page and the 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>"; 
} 

Try to validate the variables $_GET as I did there, before using them, so that an error is not generated.

I suggest taking a study in logic and also starting to go to Object Orientation. Check out Devcia , I'm posting a few posts for beginners there.

    
16.09.2014 / 14:27