-
-
$pagesize=10; //Set the number of records displayed on each page
- $conn=mysql_connect("localhost","root",""); //Connect to the database
- $rs =mysql_query("select count(*) from tb_product",$conn); //Get the total number of records $rs
- $myrow = mysql_fetch_array($rs);
- $numrows=$myrow[0];
-
//Calculate the total number of pages
- $pages=intval($numrows/$pagesize);
//Judge the page number setting
- if (isset($_GET['page' ])){
- $page=intval($_GET['page']);
- }
- else{
- $page=1; // Otherwise, set to the first page
- }
-
Copy the code
3. Create the use case table myTable
-
- create table myTable(id int NOT NULL auto_increment,
- news_title varchar(50),
- news_cont text,
- add_time datetime,
- PRIMARY KEY(id))
Copy code
4. Complete code
-
-
- php paging example_bbs.it-home.org
-
- $conn=mysql_connect("localhost","root","");
- //Set the number of records displayed on each page
- $pagesize=1;
- mysql_select_db("mydata",$conn);
- //Get the total number of records $rs, and calculate the total number of pages using
- $rs=mysql_query(" select count(*) from tb_product",$conn);
- $myrow = mysql_fetch_array($rs);
- $numrows=$myrow[0];
- //Calculate the total number of pages
- $pages=intval($numrows/$ pagesize);
- if ($numrows%$pagesize)
- $pages++;
- //Set the number of pages
- if (isset($_GET['page'])){
- $page=intval($_GET['page'] ; Specify the number of records
- $rs=mysql_query("select * from myTable order by id desc limit $offset,$pagesize",$conn);
- if ($myrow = mysql_fetch_array($rs))
- {
- $i=0;
- ?>
-
-
-
-
- do {
- $i++;
- ?>
-
-
=$myrow["news_title"]?> |
-
=$myrow["news_cont"]?> |
-
- }
- while ($myrow = mysql_fetch_array($rs));
- echo "
| ";
- }
- echo "
There are ".$pages." pages (".$page ."/".$pages.")";
- for ($i=1;$i< $page;$i++)
- echo "[".$i ."] ";
- Echo "[".$page."]";
- for ($i=$page+1;$i<=$pages;$ i++)
- echo "[".$i ."] ";
- echo "
" ;
- ?>
5. Summary
- This example code runs normally on windows2000 server+php4.4.0+mysql5.0.16.
- The displayed paging format is [1][2][3]... this way.
- If you want to display it as "Home Page Previous Page Next Page Last Page", please add the code:
- $first=1;
- $prev=$page-1;
- $next=$page+1;
- $last= $pages;
if ($page > 1)
- {
- echo "Homepage< /a> ";
- echo "Previous page ";
- }
{ - echo "next page
- echo "Last page ";
- }
-
-
-
- Copy code
-
-
- In fact, the code for writing paging display is It's very simple, just understand how it works
-
-
-
- .
-
|