Php Mysql分页

标签 php mysql sql phpmyadmin 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 个项目.. 分为它们的 div,其中 FAQ 包装器是一个大包装器,在同一级别包含两个嵌套的 div,其中是标题 div 和详细信息 div..标题从数据库中获取标题,而详细信息从数据库中获取文本并将其与其标题相关联..这就是我想要做的..

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

最佳答案

我已经尝试了一种新方法,因为自从我发布问题以来我一直在尝试,我想尝试一些已经解决了问题的方法,正如你可以看到这里的新代码..所有的问题都是主文件名为 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 但显然我在你们的帮助下设法解决了这个问题 ^_^

关于Php Mysql分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16826306/

相关文章:

javascript - 尝试根据选择选项从数据库自动填充输入字段时出现错误

MySQL 错误 : Duplicate entry 'xxx' for Primary Key

php - 在 php 中放置一个表格,第一行

python - 用于具有多个左连接的 SQL 查询的 Django ORM

php - 如何防止 PHP 中的 SQL 注入(inject)?

sql - 月末时如何按月分组

php - 通过预加载从 Eloquent 获取原始 SQL 查询

javascript - 服务器端或其他方式结束vidyo io视频通话

php - preg 替换 twitter 状态和 http 或 https PHP

mysql - 创建一个 cron 来执行每日 mysql 转储