Dynamic site with multiple pages of results

Asked

Viewed 576 times

0

I am doing a project of a dynamic website in college and wanted to know how to assemble those result pages with first, second pages and so on without creating new files .php .

For example:

I have a news site, when the user clicks on a theme, the site will be redirected to a page with news related to the subject and are shown at most 10 news in each page.

As I do for when user click the button on the second page, it be redirected to a page with the news of 11-20, and so on, on the 3rd page the news of 21-30...; without creating a php file for subsequent pages as it can become very large.

  • 4

    Search by pagination on the site, have a diversity of questions and answers on the subject.

  • People, I am reopening the question because it is possible to answer it without extending too much, as evidenced by Hiago’s answer below. Leandro, even so it is recommended to be more specific in your question. If you already have the site, it would be nice to have included in the question as you already do the complete listing of the news. So the staff has a starting point, understands what you already know, etc. Remembering yet that at any time you can [Edit] your questions to improve them. Thank you.

1 answer

7


You search for PHP paging, before that you need to understand what paging is. Result paging is something very simple.

We do a search in a certain table of the Database, and with the search result, we divide the number of records, by a specific number to display per page.

For example a total of 200 records, and we want to display 20 per page, so we have 200/20 = 10 pages. Simple, no? Well let’s go to the code then.

First connect to Mysql:

<?php
 $conn = mysql_connect("host","usuario","senha");
 $db = mysql_select_db("bancodedados");
?>

Now let’s create the SQL clause that must be executed:

<?php
 $busca = "SELECT * FROM tabelax";
?>

Let’s get to work... Specify the total records to be displayed per page:

<?php
  $total_reg = "10"; // número de registros por página
?>

If the page is not specified the "page" variable will be set to 1, this avoids displaying the beginning page 0:

<?php
 $pagina=$_GET['pagina'];
 if (!$pagina) {
  $pc = "1";
 } else {
  $pc = $pagina;
 }
?>

Let’s determine the initial value of the limited searches:

<?php
  $inicio = $pc - 1;
  $inicio = $inicio * $total_reg;
?>

Let’s select the data and display the pagination:

<?php
  $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

  // vamos criar a visualização
  while ($dados = mysql_fetch_array($limite)) {
    $nome = $dados["nome"];
    echo "Nome: $nome<br>";
  }

  // agora vamos criar os botões "Anterior e próximo"
  $anterior = $pc -1;
  $proximo = $pc +1;
  if ($pc>1) {
    echo " <a href='?pagina=$anterior'><- Anterior</a> ";
  }
  echo "|";
  if ($pc<$tp) {
    echo " <a href='?pagina=$proximo'>Próxima -></a>";
  }
 ?>

Ready, our PHP pagination is created!

Read more in: PHP Pagination http://www.devmedia.com.br/paginacao-em-php/21972#ixzz3EL04G7wL

Source: http://www.devmedia.com.br/paginacao-em-php/21972

Browser other questions tagged

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