ホームページ  >  記事  >  バックエンド開発  >  シンプルな php および mysql データ ページング プログラム_PHP チュートリアル

シンプルな php および mysql データ ページング プログラム_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-13 10:44:31867ブラウズ

必要な友人が参照できる簡単な php および mysql データ プログラムを次に示します。

代码如下复制幣

// Adam のカスタム PHP MySQL ページネーション チュートリアルとスクリプト
// mysql 接続データを配置し、SQL クエリ (両方のクエリ) を変更する必要があります
// このスクリプトはチュートリアル形式であり、次のビデオが付属しています:
mysql_connect("DB_Host_Here","DB_Username_Here","DB_Password_Here") または die (mysql_error());
mysql_select_db("DB_Name_Here") または die (mysql_error());
////////////// 通常と同じように、最初にメンバー データをクエリします
$sql = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC");
/////////////////////////////////// アダムのページネーション ロジック /////////// //////////////////////////////////////////////// ///////////
$nr = mysql_num_rows($sql); // データベースクエリから合計 Num 行を取得します
if (isset($_GET['pn'])) { // URL 変数が存在する場合は、URL vars から pn を取得します
    $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); // セキュリティのために数値以外のすべてをフィルタリングします(new)
    //$pn = ereg_replace("[^0-9]", "", $_GET['pn']); // セキュリティのために数値以外のすべてをフィルタリングします (非推奨)
} else { // pn URL 変数が存在しない場合は、強制的にページ番号 1 の値になります
    $pn = 1;
}
//ここで、各ページに表示するデータベース項目の数を設定します
$itemsPage = 10;
// ページネーション結果セットの最後のページの値を取得します
$lastPage = ceil($nr / $itemsPerPage);
// URL 変数 $pn(ページ番号) がページ 1 より小さくなく、$lastpage より大きくないことを確認してください
if ($pn < 1) { // 1 未満の場合
    $pn = 1; // 1 になる場合は強制します
} else if ($pn > $lastPage) { // $lastpage より大きい場合
    $pn = $lastPage; // $lastpage の値に強制します
}
// これにより、「次へ」ボタンと「戻る」ボタンの間にクリックする数字が作成されます
// このセクションは、このスクリプトに付属するビデオで詳しく説明されています
$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:


  

     

     

     


效果

ページ 6 /39 戻る 4 5 6 7 8 次へ

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/633091.html技術記事代案次のように?php // Adam のカスタム PHP MySQL ページネーション チュートリアルとスクリプト // あなたがしなければならないのは...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。