繁体   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