我正在尝试使用“上一个”和“下一个”按钮按ID排序的时间返回一条记录。我遇到了一些代码问题。
<?php
include ('header.php');
include ('connection.php');
//pagination starts here
if(isset($_GET['page'])){
$page = preg_replace('#[^0-9]#','',$_GET['page']);
}else{
$page = 1;
}
$perPage = 1;
$lastPage = cell($count / $perPage);
if($page < 1){
$page = 1;
}else if($page > $lastPage){
$page = $lastPage;
}
$limit = 'LIMIT ' .($page -1)*$perPage .',$perPage';
$query = mysql_query('SELECT LastName FROM residents ORDER BY ID DESC $limit');
if($lastPage != 1){
if($page != $lastPage){
$next = $page + 1;
$pagination.='<a href="AutoIncrement.php?page='.$next.">Next</a>';
}
//error occurs here
if($page != 1){
$prev = $page - 1;
$pagination.='<a href="AutoIncrement.php?page='.$prev.">Previous</a>';
}
}
?>
这一切似乎都没问题,除了我不断收到最后一个IF语句的错误。有什么想法吗?
答案 0 :(得分:1)
我终于弄明白了问题所在。以下是我提出的建议:
<?php
//browsing feature of database
include ('connection.php');
include ('header.php');
$count_query = mysql_query("SELECT NULL FROM residents");
$count = mysql_num_rows($count_query);
//pagination starts here
if(isset($_GET['page'])){
//$page = preg_replace("##[^0-9]","",$_GET['page']);
$page = $_GET['page'];
//echo "Page is ".$page;
}else{
$page = 1;
//echo "Page is one";
}
$perPage = 1;
$lastPage = ceil($count/$perPage);
if($page <1){
$page = 1;
}else if($page > $lastPage){
$page = $lastPage;
}
$limit = "LIMIT " .($page-1) * $perPage .", $perPage";
$query = mysql_query("SELECT * FROM residents ORDER BY ID ASC $limit");
if($lastPage != 1){
if($page != $lastPage){
$next = $page + 1;
$pagination.='<nav><ul class="pager"><li><a href=Browse.php?page='.$next.'>Next</a> </li></ul><nav>';
}
if($page != 1){
$prev = $page - 1;
$pagination.='<nav><ul class="pager"><li><a href=Browse.php? page='.$prev.'>Previous</a></li></ul></nav>';
}
}
while($row = mysql_fetch_array($query)){
$output.='<p><img src="upload/' . $row['Picture'].'" width="70" height="70"/></p>';
$output.= $row['FirstName']. '<br />';
$output.= $row['LastName']. '<br />';
$output.= $row['Address']. '<br />';
$output.= $row['Birthday']. '<br />';
$output.= $row['FormerResidence']. '<br />';
$output.= $row['Career']. '<br />';
$output.= $row['Education']. '<br />';
$output.= $row['SpecialInterests']. '<br />';
}
?>
<?php
/*while($person = mysql_fetch_array($result)) {
echo "<h3>" . $person['FirstName'] . "</h3>";
echo "<p>" . $person['LastName'] . "</p>";
echo "<p>" . $person['Address'] . "</p>";
echo "<p>" . $person['Birthday'] . "</p>";
echo "<p>" . $person['FormerResidence'] . "</p>";
echo "<p>" . $person['Career'] . "</p>";
echo "<p>" . $person['Education'] . "</p>";
echo "<p>" . $person['SpecialInterests'] . "</p>";
echo '<p><img src="upload/' . $person['Picture'].'" width="70" height="70"/> </p>';
}*/
?>
<h1>Browse Residents</h1>
<!-- Comment start
<a href="nextandprevious.php?page='.$previous.">Previous</a>
<a href="nextandprevious.php?page='.$next.">Next</a>
Comment end -->
<?php echo $output;?>
<?php echo $pagination;?>
答案 1 :(得分:0)
Limit
和offset
可用于获取特定记录,但这会产生过多的开销。而是在变量中获取整个表数据,将其传递给javascript并在javascript中迭代。
编辑1:我故意没有提供任何代码示例,因此您可以自己尝试一下。
答案 2 :(得分:0)
然后您可以使用SELECT * FROM table ORDER BY LastName ASC Limit 1,1
之类的查询。 (对于下一个)然后在将限制改为2,1之后等等。