PHP Development of PHP paging program implementation Code

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags .url clear code content development echo example function

In Php+mysql to achieve a relatively simple pagination, as long as you get page and x each page a few records and then use the limit n,m can be the perfect implementation of pagination, this example is very clear to the needs of the students reference.

Database Connection Code

The code is as follows
<?php
$conn = @ mysql_connect ("localhost", "root", "") or Die ("Database link error"); mysql_select_db ("form", $conn);
mysql_query ("Set names ' GBK '"); Use GBK Chinese code;
Replace space, enter
function Htmtocode ($content)
{
$content = Str_replace ("n", "<br>", Str_replace ("", "&nbsp;", $content));
return $content;
}
?>

This is a very important shared core function.

  code is as follows  
<?php
 
     function _pageft ($totle, $displaypg = $url = ') {
 
         Global $page, $firstcount, $pagenav, $_server
&NBSP
         $GLOBALS ["displaypg"] = $DISPLAYPG;
 
        if (! $page)
              $page = 1;
         if (! $url) {
              $url = $_server["Request_uri"];
        }

        //url Analysis:
          $parse _url = Parse_url ($url);        if ($url _query) {
             $url _query = ereg_replace ("(^|&) page= $page", "", $url _query);
            $url = str_replace ($parse _url["Query"], $ Url_query, $url);
            if ($url _query)
                  $url. = "&page";
             Else
                 $url. = "page";
        } else {
              $url. = "? page";
        }
         $LASTPG = ceil ($totle/$DISPLAYPG);//Last page, total pages
          $page = min ($lastpg, $page);
        $PREPG = $page-1;//prev
         $NEXTPG = ($page = = $LASTPG 0: $page + 1); Next page
        $firstcount = ($page-1) * $DISPLAYPG;

        //Start Pager bar code:
          $pagenav = "Show Cap <B>". ($totle? ($firstcount + 1): 0). "</B>-<B>". Min ($firstcount + $DISPLAYPG, $totle). "</B> Records, a total of $totle records";
 
      //If only one page jumps out of the function:
        if ($LASTPG <= 1)
            return false;

         $pagenav. = "<a href= ' $url =1 ' > Home </a>";
         if ($PREPG)
             $pagenav. = "<a href= ' $url = $prepg ' > front page </a>";
         Else
             $pagenav. = "front page";
         if ($NEXTPG)
              $pagenav. = "<a href= ' $url = $nextpg ' > Back page </a>";
        Else
              $pagenav. = "Back Page";
         $pagenav. = "<a href= ' $url = $lastpg ' > Last </a>";
 
       /Drop-down Jump List, looping all page numbers:
&NBSP;&Nbsp;       $pagenav. = "to <select name= ' topage ' size= ' 1 ' onchange= ' window.location= "$url =" +this.value ' >n ";
         for ($i = 1; $i <= $lastpg; $i + +) {
              if ($i = = $page)
                 $pagenav. = "<option value= ' $i ' selected> $i </option>n" ;
             Else
                 $pagenav. = "<option value= ' $i ' > $i </ Option>n ";
       }
        $pagenav. = "</select> page, $lastpg page";
    }


Include ("conn.php");

$result =mysql_query ("SELECT * from ' Test '");
$total =mysql_num_rows ($result);
Call Pageft (), display 10 messages per page (using the default 20 o'clock, you can omit this parameter), use this page URL (default, so omitted).
_pageft ($total, 5);
Echo $pagenav;

$result =mysql_query ("SELECT * from ' Test ' limit $firstcount, $displaypg");
while ($row =mysql_fetch_array ($result)) {

echo "<hr><b>" $row [name]. "| ". $row [Sex];

}
?>

list.php

Database query record and generate SQL query statement

The code is as follows
<?php
Include ("conn.php");
$pagesize = 5; 5 $url =$_server["Request_uri"];
$url =parse_url ($url);
$url = $url [path];
$NUMQ =mysql_query ("SELECT * from ' Test '");
$num = mysql_num_rows ($NUMQ);
if ($_get[page]) {
$pageval =$_get[page];
$page = ($pageval-1) * $pagesize;
$page. = ', ';
}
if ($num > $pagesize) {
if ($pageval <=1) $pageval = 1;
echo "Altogether $num bar". "<a href= $url page=". ($pageval-1). " > prev </a> <a href= $url page= ". ($pageval + 1). " > Next </a> ";
}
$SQL = "SELECT * from ' Test ' limit $page $pagesize";
$query =mysql_query ($SQL);
while ($row =mysql_fetch_array ($query)) {
echo "<hr><b>" $row [name]. "| ". $row [Sex];
}
?>

Page-Numbering formula: (current page-1) * Number of pages per page, per page

The code is as follows
SQL statement: SELECT * from test_table limit ($page-1) * $pageSize, $pageSize;

Summary:

No matter what program development is a separate is the original from X to start to take N, so that only a few, and in MySQL has limit function to operate, such as limit 1,5 is from the first article to take 5.

To introduce the core code, here to get the page number, and then xpagesize code as follows

The code is as follows
if ($_get[page]) {
$pageval =$_get[page];
$page = ($pageval-1) * $pagesize;
$page. = ', ';
}
if ($num > $pagesize) {
if ($pageval <=1) $pageval = 1;


It's a lot simpler to make a split in mysql+php because of the limit

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.