Home >Backend Development >PHP Tutorial >Detailed explanation of how to use PHP to create simple paging (read records from the database)_PHP Tutorial

Detailed explanation of how to use PHP to create simple paging (read records from the database)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:10:471402browse

                                                                                                                                                                                                                                            PHP I couldn’t understand some of the writing and didn’t read much. Finally found a simpler one.

The general idea is:

 1. Set the maximum number of records to be displayed on each page.

2. Calculate the total number of pages

3. Compare the current page with the total number of pages to change the status of the connection

4. Use limit control to read records from the database

The following is the code:

Copy the code The code is as follows:

$conn = mysql_connect('localhost','root','');
mysql_select_db('db_BookStore',$conn);
mysql_query("set names utf8");
ISSET ($ _ Get ['Page'])) // Determine whether there is a page parameter, get the face value, otherwise 1
{
$ page = intval ($ _ get ['page']);
                                                                                                                                                                             2; //Maximum number of records
$sql = "SELECT count(*) as amount FROM tb_BookInfo";
 $amount = $row['amount '];
if($amount)
if($amount % $page_size){$page_count = 1;} page_count = ( int)($amount / $page_size) + 1; }
else{$page_count = $amount / $page_size;}
}
else
              $page_count = 0;
                                                                                                                                                               $page_string .= "Homepage | Previous page";   }
   $sql = "select * from table order by id desc limit ". ($page-1)*$page_size .", $page_size";
   $result = mysql_query($sql);
   while ($row = mysql_fetch_row($result) ){
     $rowset[] = $row;
   }
?>

This is just a very simple method, everyone If you have any other methods, please let me know.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327039.htmlTechArticleNew to PHP, I always wanted to do some paging but forgot about it. I was lucky enough to be reminded today, so I searched online. I couldn’t understand some of the writing and didn’t read much. Finally found a simpler one. Roughly...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn