簡體   English   中英

如何將此代碼合並在一起以獲得工作分頁?

[英]How can I merge this code together to get working pagination?

我有兩套博客類型網站的代碼。 一個顯示文章列表,另一個用於分頁。 我不知道如何讓分頁和文章列表代碼一起工作。 文章列表代碼用於從 SQL 數據庫為我的網站生成所需的信息。 但是,我不知道需要更改什么才能響應分頁代碼。 盡管當前頁面不顯示為按鈕,但將生成分頁。

文章列表:

<?php
   include ('database-connect.php');
   include ('database-query-topic2.php');
                                            
                                            
// this runs the query
// needs to have the mysql connection info passed as $con
// the second argument is the number of articles to return
   $articles = list_articles($con, 5); //
                                                                                      

   // if articles are found 
   if ($articles) {
       while ($row = mysqli_fetch_row($articles)){
                                                    
       ?>

        <!-- loop here for articles -->

        <div class="article-list-item-info">
            <div class="a-l-i-i-left">
                <h2 class="article-title">
                    <a href="#" title="Article Title"> <?php print( $row[1]); ?> </a>
                </h2>
                <div class="article-list-preview">
                    <a href="#"> <?php print($row[3]); ?> </a>
                </div>
                <div class="article-list-author-date">
                    <address class="article-list-author">
                        <a href="#"> <?php print($row[4]); ?> </a>
                    </address>
                    <time class="article-list-date"> <?php print($row[8]); ?> </time>
                </div>
            </div>
            <img class="article-image" src=<?php print($row[6]); ?> >
        </div>
                                                                                                                                        
      <!-- end loop for articles -->                                              
                                                   

   <?php
   }
}

// Closing the connection
mysqli_close($con); 
                                            
?> 

<div class="a-l-l-m-b-container">
    <?php include ("article-pagination.php"); ?>
<!--<button class="a-l-load-more-button">Load More</button>-->
</div>

數據庫連接.php:

<?php
    $servername = "ip address";
    $username = "user";
    $password = "password";
    $dbname = "database name";
                
    // Create Connection
    $con = mysqli_connect('ip address', 'user', 'password', 'database name');                       
?>

數據庫查詢主題2.php:

<?php


//gets list of articles
// $con is the sql connection
// $limit is the number of articles to return

function list_articles($con, $limit){

    // Executing the multi query
    
    $query =
    "SELECT * FROM table
    WHERE article_category='category'
    ORDER BY article_id DESC LIMIT $limit";


    $result = mysqli_query($con, $query);

    return $result;

}

文章分頁.php:

<?php

include("database-connect.php");
 
$query=mysqli_query($con,"select count(article_id) from `table`");
$row = mysqli_fetch_row($query);
  
$rows = $row[0];
 
$page_rows = 5; //
 
$last = ceil($rows/$page_rows);

if($last < 1){
  $last = 1;
}

$pagenum = 1;

if(isset($_GET['pn'])){
  $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}

if ($pagenum < 1) { 
  $pagenum = 1; 
} 
else if ($pagenum > $last) { 
  $pagenum = $last; 
}

//
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$nquery=mysqli_query($con,"SELECT * FROM table
WHERE article_category='category'
ORDER BY article_id DESC LIMIT $limit");


//
  $paginationCtrls = '';
 
  $paginationCtrls .= '<nav aria-label="Page navigation example">';
  $paginationCtrls .= '<ul class="pagination">';

    if($last != 1){
 
    if ($pagenum > 1) {
        $previous = $pagenum - 1;
        $paginationCtrls .= '<li class="page-item"><a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'" class="page-link">Previous</a></li> &nbsp; &nbsp; ';
 
        for($i = $pagenum-4; $i < $pagenum; $i++){
            if($i > 0){
                $paginationCtrls .= '<li class="page-item"><a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'" class="page-link">'.$i.'</a></li> &nbsp; ';
            }
        }
    }
 
    $paginationCtrls .= ''.$pagenum.' &nbsp; ';
 
    for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= ' <li class="page-item"><a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'" class="page-link">'.$i.'</a></li> &nbsp; ';
        if($i >= $pagenum+4){
            break;
        }
    }
 
    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= ' &nbsp; &nbsp; <li class="page-item"><a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'" class="page-link">Next</a></li> ';
    }
    }
  echo $paginationCtrls;
  '</ul>';
  '</nav>';
 
?>

看起來這里是錯誤的

$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$nquery=mysqli_query($con,"SELECT * FROM table  
WHERE article_category='category'
ORDER BY article_id DESC LIMIT $limit");

您正在使用 LIMIT 兩次

首先在$limit = 'LIMIT....'

第二個DESC LIMIT $limit"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM