Heim > Backend-Entwicklung > PHP-Tutorial > 一个简单php和mysql数据分页程序_PHP教程

一个简单php和mysql数据分页程序_PHP教程

WBOY
Freigeben: 2016-07-13 10:44:31
Original
929 Leute haben es durchsucht

一个简单php和mysql数据分页程序有需要的朋友可参考一下。

 代码如下 复制代码


// Adam's Custom PHP MySQL Pagination Tutorial and Script
// You have to put your mysql connection data and alter the SQL queries(both queries)
// This script is in tutorial form and is accompanied by the following video:
mysql_connect("DB_Host_Here","DB_Username_Here","DB_Password_Here") or die (mysql_error());
mysql_select_db("DB_Name_Here") or die (mysql_error());
//////////////  QUERY THE MEMBER DATA INITIALLY LIKE YOU NORMALLY WOULD
$sql = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC");
//////////////////////////////////// Adam's Pagination Logic ////////////////////////////////////////////////////////////////////////
$nr = mysql_num_rows($sql); // Get total of Num rows from the database query
if (isset($_GET['pn'])) { // Get pn from URL vars if it is present
    $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); // filter everything but numbers for security(new)
    //$pn = ereg_replace("[^0-9]", "", $_GET['pn']); // filter everything but numbers for security(deprecated)
} else { // If the pn URL variable is not present force it to be value of page number 1
    $pn = 1;
}
//This is where we set how many database items to show on each page
$itemsPerPage = 10;
// Get the value of the last page in the pagination result set
$lastPage = ceil($nr / $itemsPerPage);
// Be sure URL variable $pn(page number) is no lower than page 1 and no higher than $lastpage
if ($pn     $pn = 1; // force if to be 1
} else if ($pn > $lastPage) { // if it is greater than $lastpage
    $pn = $lastPage; // force it to be $lastpage's value
}
// This creates the numbers to click in between the next and back buttons
// This section is explained well in the video that accompanies this script
$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
    $centerPages .= '  ' . $pn . '  ';
    $centerPages .= '  ' . $add1 . '  ';
} else if ($pn == $lastPage) {
    $centerPages .= '  ' . $sub1 . '  ';
    $centerPages .= '  ' . $pn . '  ';
} else if ($pn > 2 && $pn     $centerPages .= '  ' . $sub2 . '  ';
    $centerPages .= '  ' . $sub1 . '  ';
    $centerPages .= '  ' . $pn . '  ';
    $centerPages .= '  ' . $add1 . '  ';
    $centerPages .= '  ' . $add2 . '  ';
} else if ($pn > 1 && $pn     $centerPages .= '  ' . $sub1 . '  ';
    $centerPages .= '  ' . $pn . '  ';
    $centerPages .= '  ' . $add1 . '  ';
}
// This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
// Now we are going to run the same query as above but this time add $limit onto the end of the SQL syntax
// $sql2 is what we will use to fuel our while loop statement below
$sql2 = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC $limit");
//////////////////////////////// END Adam's Pagination Logic ////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Adam's Pagination Display Setup /////////////////////////////////////////////////////////////////////
$paginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display
if ($lastPage != "1"){
    // This shows the user what page they are on, and the total number of pages
    $paginationDisplay .= 'Page ' . $pn . ' of ' . $lastPage. '        ';
    // If we are not on page 1 we can place the Back button
    if ($pn != 1) {
        $previous = $pn - 1;
        $paginationDisplay .=  '   Back ';
    }
    // Lay in the clickable numbers display here between the Back and Next links
    $paginationDisplay .= '' . $centerPages . '';
    // If we are not on the very last page we can place the Next button
    if ($pn != $lastPage) {
        $nextPage = $pn + 1;
        $paginationDisplay .=  '   Next ';
    }
}
///////////////////////////////////// END Adam's Pagination Display Setup ///////////////////////////////////////////////////////////////////////////
// Build the Output Section Here
$outputList = '';
while($row = mysql_fetch_array($sql2)){

    $id = $row["id"];
    $firstname = $row["firstname"];
    $country = $row["country"];

    $outputList .= '

' . $firstname . '

' . $country . '


';
   
} // close while loop
?>


Adam's Pagination



  

    

Total Items:


  

     

     

     


效果

Page 6 of 39          Back   4    5    6    7    8    Next

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/633091.htmlTechArticle一个简单php和mysql数据分页程序有需要的朋友可参考一下。 代码如下 复制代码 ?php // Adam's Custom PHP MySQL Pagination Tutorial and Script // You have to...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage