2

我在尝试从一些教程中进行一些简单的分页时遇到了这个问题,但显然它没有按应有的方式工作..让我发布一些注释很好的代码并预览我面临的问题:-

首先,我有一个名为 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 是要检索的每个常见问题解答的正文。可能有问题的代码是: -

            <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 个项目.. 被划分为它们的 div,其中常见问题包装器是一个大包装器,在同一级别上保存两个嵌套的 div,其中是标题 div 和详细信息 div .. 标题取标题从数据库中获取详细信息,然后将数据库中的文本与其标题相关联..这就是我想做的一切..

我非常感谢您的帮助。如果您需要任何信息,除了我迄今为止提供的大量信息之外,请通知我。

4

2 回答 2

0

我尝试了一种新方法,因为自从我发布问题以来我一直在尝试,我想尝试一些解决问题的方法,你可以看到这里是新代码..所有问题都出在主文件上命名为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 上制作著名教程的人名叫 Adam Khoury 在他的网站上犯了这个错误:- http://www.developphp.com/view_lesson.php?v=289 但显然我设法解决了在你们的帮助下 ^_^

于 2013-05-30T01:41:25.930 回答
-1

你可以使用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)

  {?> 
于 2017-04-10T12:05:09.833 回答