I’ll summarize a technique I use using the OFFSET
mysql.
<?php
$limite = 15; // Limite por página
// Pega página atual, se houver e for válido (maior que zero!)
if( isset( $_GET['pagina'] ) && (int)$_GET['pagina'] >= 0){
$pagina = (int)$_GET['pagina'];
}else{
$pagina = 0;
}
// Calcula o offset
$offset = $limite * $pagina;
// Se for 0 será 15*0 que será 0, começando do inicio
// Se for 1 será 15*1 que irá começar do 15 ignorando os 15 anteriores. ;)
$postagem = $mysqli->query('SELECT * FROM `post` ORDER BY id DESC LIMIT '.$limite.' OFFSET '.$offset);
?>
After this just display as you want, for example:
<?php
while($info = $postagem->fetch_array()){
// Loop finito para repetir para cada linha existente
?>
<!-- HTML PARA EXIBIÇÃO -->
<h1><?= $info['titulo'] ?></h1>
<div class="data"><?= $info['dataHumano'] ?></div>
<div class="descricao"><?= $info['desc'] ?></div>
<!-- HTML PARA EXIBIÇÃO -->
<?php
}
?>
To paginate use something similar to:
This will trigger the $_GET['pagina']
mentioned in the first part.
<?php
if($pagina !== 0){ // Sem isto irá exibir "Página Anterior" na primeira página.
?>
<a href="meulink.com?pagina=<?php echo $pagina-1; ?>">Página Anterior</a>
<?php
}
?>
<a href="meulink.com?pagina=<?php echo $pagina+1; ?>">Página Posterior</a>
I believe this is simple and will suffice, I have tried to comment as much as possible.
Note:
The link to next page will be displayed even if it is the last possible page! To solve this I think the best solution is to select all lines and divide by 15 and check that the user is not on the last page.
I didn’t use the bind_param
because they are only whole numbers and only in LIMIT and OFFSET, but use the bind_param
if there are other parameters in the WHERE
, for example categories.
Welcome to Stack Overflow in Portuguese! Do the post’s come from BD? Before showing you know how many posts are?
– Jorge B.
yes the post’s come from the comic
– cloud
Show the code you have to be easier to answer the question.
– Jorge B.
the code to show you the posts are not yet made I wanted a basis
– cloud
Hello, welcome to [en.so]. What you are asking involves various problems, could you [dit] the question indicating in which part you are struggling? Would it be about how to take the posts from the bank by blocks? Or about how to display this on the page?
– bfavaretto
@Cloud I don’t think anyone quite understood your question as already commented above. Are you blogging from scratch? So probably your question is actually how to query a database and should describe what technologies are being used. Or would it be a Wordpress template? In this case there are specific wordpress commands to make the pagination for you. Anyway, you need to further detail what you want, otherwise no one will simply create an example blog just to answer this question.
– utluiz
sorry for my explanation, yes I started a blog from scratch but I already have all the Divs all http://gyazo.com/817d5deec09f32476250502fe61cc6f2 I am already adding from the administrative panel with link to the database, what will happen is that the site user will post’s and more post’s and blog page will get very big, full of posts like this http://gyazo.com/44a9a885fc5761e7a6173b1b943f2802
– cloud
what I want is to limit the post imagine when the blog page has 100 posts so it will show the 20 last posts added recently and then I will have the "Older posts" button that will then show me the rest of the older posts
– cloud
http://gyazo.com/5a7fa312a7114e8f2fd9f210d3c77429 this is what I want a button to show old posts by clicking on it will update me and show me old posts
– cloud
This limit is also known as paging, when making the query it is necessary to use a
limit/off set
and aorder by <campo> desc
for example by id or creation date this will bring the last entered records– rray
As you’ve already said, your problem is in doing paging. Rephrase your question to make it about that subject, for example: "How do PHP and Mysql pagination?". Then there will probably be answers for you.
– utluiz
cloud, put these details of your comments in the question.
– ptkato
I believe that is exactly what @Lost commented, what you seek is the command LIMIT mysql.
– Adriano Leal
As @bfavaretto said, would it just be a set of results from a single category ? Or would it be multiple results from different categories ? Explain it better, if possible.
– Edilson