Php Mysql Pagination


Php Mysql Pagination

我遇到过这个问题,试图从一些教程中做一些简单和简单的分页,但显然它没有按照它应该的方式工作。让我张贴一些代码,很好地注释和预览我所面临的问题:-

首先,我有一个名为FAQ_content的文件,它有以下代码:-

    <?php 
include ('config/setup.php'); //database connection file!
##Database Retrieval Query: -
    ##FAQ_Retrieval_Query: -
        ##Pagination Logic
        //This Query is Only to get the number of rows.. we'll use a second part downstairs.
        $FAQ_query="SELECT * FROM FAQ_content ORDER BY id ASC ";
        $data1 = mysql_query($FAQ_query);
        $nr = mysql_num_rows($data1); // counts the number of entries within the database table.
        if (isset ($_GET['pn']))
            {
                $pn=preg_replace('#[^0-9]#i','',$_GET['pn']); //filters everything but numbers for security 
            }else {// if the URL variable has no $pn then it will be set to 1.
                $pn=1;
            }
        $itemsPerPage= 5; //How many items do we wanna view per page?
        $lastPage = ceil($nr/$itemsPerPage); //number of entries in the database divided by the number we wanna show per page.
        if ($pn <1) { 
                // if the pagenumber is less than 1, then it'll be forced to one,
                //and if it's larger than last page, it'll be forced to last page.
                $pn=1;}
                else if ($pn>$lastPage){
                    $pn = $lastPage;
                    }
        //Creating the Numbers to click between next and back buttons..
        $centerPages = ""; //just initiating the variable.
        $sub1 =  $pn - 1;
        $sub2 = $pn - 2;
        $add1 = $pn + 1;
        $add2 = $pn + 2;
        if ($pn == 1)
        {
            $centerPages .= '&nbsp; <span class="pagNumActive">'.$pn.'</span>&nbsp;';
            $centerPages .= '&nbsp; <a href="' .$_SERVER['PHP_SELF'].'?pn='.$add1.'">'.$add1.'</a> &nbsp;';
        }else if ($pn == $lastPage) 
        {
            $centerPages .= '&nbsp; <a href="' .$_SERVER['PHP_SELF'].'?pn='.$sub1.'">'.$sub1.'</a> &nbsp;';
            $centerPages .= '&nbsp; <span class="pagNumActive">'.$pn.'</span>&nbsp;';
        }else if ($pn > 2 && $pn < ($lastPage-1)) {
            $centerPages .= '&nbsp; <a href="' .$_SERVER['PHP_SELF'].'?pn='.$sub2.'">'.$sub2.'</a> &nbsp;';
            $centerPages .= '&nbsp; <a href="' .$_SERVER['PHP_SELF'].'?pn='.$sub1.'">'.$sub1.'</a> &nbsp;';
            $centerPages .= '&nbsp; <span class="pagNumActive">'.$pn.'</span>&nbsp;';
            $centerPages .= '&nbsp; <a href="' .$_SERVER['PHP_SELF'].'?pn='.$add1.'">'.$add1.'</a> &nbsp;';
            $centerPages .= '&nbsp; <a href="' .$_SERVER['PHP_SELF'].'?pn='.$add2.'">'.$add2.'</a> &nbsp;';
        }else if ($pn >1 && $pn < $lastPage) {
            $centerPages .= '&nbsp; <a href="' .$_SERVER['PHP_SELF'].'?pn='.$sub1.'">'.$sub1.'</a> &nbsp;';
            $centerPages .= '&nbsp; <span class="pagNumActive">'.$pn.'</span>&nbsp;';
            $centerPages .= '&nbsp; <a href="' .$_SERVER['PHP_SELF'].'?pn='.$add1.'">'.$add1.'</a> &nbsp;';
        }
        //Setting the limit range for the number of data retrieved and the items per page.
        $limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;   
        $FAQ_query2=mysql_query("SELECT * FROM FAQ_content ORDER BY id ASC $limit " , $dbc) or die(mysql_error());
        ##Pagination Logic End-- :(
        ##Pagination Display Options : -
        $paginationDisplay = "";// just initialising..
        //This condition runs only if the last page is not equal to 1, if it is equal to 1, then we don't require links.
        if ($lastPage != "1") {
            //showing the user what page he is on, and what number of pages are there..
            $paginationDisplay .= 'Page <strong>' .$pn. '</strong> of' .$lastPage. '<img src="images/clearimage.png" width="48px" height="1px" alt="Spacer"/>'; 
                //if we're not on page 1 we can place a back button ^_^
                if ($pn != 1) {
                    $previous = $pn - 1;
                    $paginationDisplay .= '&nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'"> Back </a>';  
                }
                //Lay in the clickable numbers display here between the back and next links ^_^.
                $paginationDisplay .='<span class="paginationNumbers">'.$centerPages.'</span>';
                //If we're not on the very last page, a next button is placed :-
                if ($pn != $lastPage) {
                    $nextPage = $pn +1;
                    $paginationDisplay .= '&nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$nextPage.'"> Next </a>';  
                }
        }       
?>

是第一个包含所有分页规则和逻辑的文件,除了分页显示的格式,它将如何显示。现在我们继续讨论问题本身。

我把上面提到的文件包含到我的主文件中,我想查看数据,(提示:-数据库在以前的查询有23个条目,我已经测试了numrows看到23个条目通过回显它..)数据库中的数据有23个条目,包含id、title和text三个字段。title应该是标题,text是要检索的每个FAQ的正文。可能出现问题的代码是:-

            <div class="FAQWrapper">
            <?php 
                include ('content/FAQ-content.php');
                $output1='';
                $output2='';
                while ($row = mysql_fetch_array($FAQ_query2)) {
                    $title = $row['title'];
                    $text = $row['text'];
                    $output ='<div class="FAQInstance">
                    <div class="FAQHeader"><h2>'.$title.'</h2></div><div class="FAQDetails"><p>'.$text.'</p></div></div>';
                    //$output2 ='<div class="FAQDetails"><p>'.$text.'</p></div>';
                }
             ?>
        <!--FAQ Instance Start-->
            <?php echo $output; ?>
            <!--FAQ Header Start-->
            <!--FAQ Header Ends-->
            <!--FAQ Details Start-->
            <!--FAQ Details Ends-->
        <!--FAQ Instance Ends-->
        <!--Pagination Display Start-->
        <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid; float:right;"><?php echo $paginationDisplay; ?></div>
        <!--Pagination Display Ends-->
    </div>
    <!-- FAQ Content Ends-->

我得到的是:- http://wassatproject.com/FAQ.php正如您所看到的,除了底部的分页按钮(工作得很好)之外,它是来自数据库的单个输出。

我想做的是每页查看5个项目。其中FAQ包装器是大型包装器,其中包含两个嵌套的div在同一级别,其中是headerdiv和detailsdiv..Header从数据库中获取标题,而details从数据库中获取文本,并将其与标题相关联。这就是我想做的…

我真的提前感谢你的帮助。除了我已经提供的大量信息外,如果您还需要任何信息,请通知我。

我尝试了一种新方法,自从我发布问题以来,我一直在尝试,我想尝试一些已经解决问题的东西,正如你可以看到这里的新代码。所有的问题都与主文件FAQ.php有关,而不是与FAQ-content.php有关…这是我修复后的代码…谢谢你的款待^_^

<!-- FAQ Content Start-->
<div class="FAQWrapper">
        <?php 
            include ('content/FAQ-content.php');
            $output1='';
            $output2='';
            while ($row = mysql_fetch_array($FAQ_query2)) {
                $title = $row['title'];
                $text = $row['text'];
                $output ='<div class="FAQInstance">
                <div class="FAQHeader"><h2>'.$title.'</h2></div><div class="FAQDetails"><p>'.$text.'</p></div></div>';
                echo $output;
            }
         ?>
    <!--FAQ Instance Start-->
        <!--FAQ Header Start-->
        <!--FAQ Header Ends-->
        <!--FAQ Details Start-->
        <!--FAQ Details Ends-->
    <!--FAQ Instance Ends-->
    <!--Pagination Display Start-->
    <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid; float:right;"><?php echo $paginationDisplay; ?></div>
    <!--Pagination Display Ends-->
</div>
<!-- FAQ Content Ends-->

显然,在youtube上制作著名教程的人亚当·库利在他的网站上犯了这个错误:- http://www.developphp.com/view_lesson.php?v=289但是很明显,我在你们的帮助下解决了这个问题^_^

可以使用mysqli,代码如下

$conn=mysqli_connect("localhost","root","","ui");

$start=0;
$limit=5;
  $t=mysqli_query($conn,"select * from form_table");
  $total=mysqli_num_rows($t);

   if(isset($_GET['id']))
   {
        $id=$_GET['id'] ; 
                        $start=($id-1)*$limit;
                          }
            else
            {
        $id=1;
 }
    $page=ceil($total/$limit);
   $query=mysqli_query($conn,"select * from form_table limit $start,$limit");
 ?>
 <!DOCTYPE html>
<html>
<head>
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <link rel="stylesheet"                    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script s        src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">     </script>
 <script                      src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">              </script>
  </head>         
 <body>
<div class="container">
 <h2>Table</h2>
    <table class="table table-bordered">
    <thead>
      <tr>
       <th>Id</th>
         <th>Name</th>
       <th>Gender</th>

<th>Hobbies</th>
<th>Course</th>
 </tr>
 </thead>
 <tbody>
 <?php
    while($ft=mysqli_fetch_array($query))
 {?>
 <tr>
 <td><?= $ft['0']?></td>
 <td><?= $ft['1']?></td>
 <td><?= $ft['2']?></td>
 <td><?= $ft['3']?></td>
 <td><?= $ft['4']?></td>
 </tr>
 <?php
 }
  ? >

    </tbody>
    </table>
    <ul class="pagination">
       <?php if($id > 1) {?> <li><a href="?id=<?php echo ($id-1) ?              >">Previous</a></li><?php }?>
     <?php
     for($i=1;$i <= $page;$i++){
    ?>
    <li><a href="?id=<?php echo $i ?>"><?php echo $i;?></a></li>
   <?php
   }
   ?>
 <?php if($id!=$page)
  {?>