我在尝试从一些教程中进行一些简单的分页时遇到了这个问题,但显然它没有按应有的方式工作..让我发布一些注释很好的代码并预览我面临的问题:-
首先,我有一个名为 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 .= ' <span class="pagNumActive">'.$pn.'</span> ';
$centerPages .= ' <a href="' .$_SERVER['PHP_SELF'].'?pn='.$add1.'">'.$add1.'</a> ';
}else if ($pn == $lastPage)
{
$centerPages .= ' <a href="' .$_SERVER['PHP_SELF'].'?pn='.$sub1.'">'.$sub1.'</a> ';
$centerPages .= ' <span class="pagNumActive">'.$pn.'</span> ';
}else if ($pn > 2 && $pn < ($lastPage-1)) {
$centerPages .= ' <a href="' .$_SERVER['PHP_SELF'].'?pn='.$sub2.'">'.$sub2.'</a> ';
$centerPages .= ' <a href="' .$_SERVER['PHP_SELF'].'?pn='.$sub1.'">'.$sub1.'</a> ';
$centerPages .= ' <span class="pagNumActive">'.$pn.'</span> ';
$centerPages .= ' <a href="' .$_SERVER['PHP_SELF'].'?pn='.$add1.'">'.$add1.'</a> ';
$centerPages .= ' <a href="' .$_SERVER['PHP_SELF'].'?pn='.$add2.'">'.$add2.'</a> ';
}else if ($pn >1 && $pn < $lastPage) {
$centerPages .= ' <a href="' .$_SERVER['PHP_SELF'].'?pn='.$sub1.'">'.$sub1.'</a> ';
$centerPages .= ' <span class="pagNumActive">'.$pn.'</span> ';
$centerPages .= ' <a href="' .$_SERVER['PHP_SELF'].'?pn='.$add1.'">'.$add1.'</a> ';
}
//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 .= ' <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 .= ' <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 .. 标题取标题从数据库中获取详细信息,然后将数据库中的文本与其标题相关联..这就是我想做的一切..
我非常感谢您的帮助。如果您需要任何信息,除了我迄今为止提供的大量信息之外,请通知我。