分页链接不起作用

时间:2011-05-26 08:59:49

标签: php pagination

我在网上搜索过,无法真正找到我的代码有什么问题。 next和prev链接不起作用。它只获得第一个条目。

希望你们能帮忙解决这个问题!感谢。

<?php 
// ROWS DISPLAYED PER PAGE
$rows_per_page = 1;

// GET PAGE NUMBER
$page = $_GET['page'];
$offset = (!empty($page)) ? $page : $page = 1;

// URL CLEAN UP
$self = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
$self = str_replace("page={$offset}", "", $self); 

// GET LIST OF STATES
$offset = ($page) ? ($page - 1) * $rows_per_page : 0; 
$id = $_GET['id'];
$sql = "SELECT * FROM updates WHERE update_categoryID = '$id' ORDER BY update_date          DESC LIMIT {$offset},{$rows_per_page}";
$result = mysql_query($sql)or die('Error, query failed');

// GET NUMBER OF PAGES
$query1 = "SELECT * FROM updates WHERE update_categoryID = '$id'"; 
$result1 = mysql_query($query1)or die('Error, query failed');
$total = mysql_num_rows($result1);
$NumPgs = ceil($total/$rows_per_page);

while($row = mysql_fetch_assoc($result)) 
{
?>
<h2><?php echo $row['update_title']; ?></h2>
 <p class="datetime"><?php echo $row['update_date'];?></p>
 <br>
 <p class="post"><?php echo $row['update_content'];?></p> 
 <a href="blogcomment.php?id=<?php echo $row['update_id'];?>">Post a Comment</a>
 <?php
 }
 ?>
<span style="float:right">
<? if ($NumPgs > 0 && $page!=1) {
echo "<a href=\"{$self}page=".($page- 1)."\">&lt;&lt;Prev</a>&nbsp;&nbsp;"; } ?>
[Page <?php echo $page; ?>]
<? if ($page < $NumPgs) {
echo "<a href=\"{$self}page=".($page+1)."\">&nbsp;&nbsp;Next&gt;&gt;</a>"; } ?>
<b><? echo $offset+1;?> to <? echo $offset+$rows_per_page;?>, of <? echo $total; ?>  Entries</b>
</span>
</div>  

2 个答案:

答案 0 :(得分:1)

嗯,也许您的页面参数会附加到查询字符串中,请尝试:

 $self = $_SERVER['PHP_SELF']."?".
         preg_replace( $_SERVER[ 'QUERY_STRING' ], 'page=[0-9]+', '');

而不是:

 // URL CLEAN UP
 $self = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
 $self = str_replace("page={$offset}", "", $self);  

答案 1 :(得分:0)

短打开的标签在您的情况下不起作用。

http://us3.php.net/echo

http://us3.php.net/manual/en/ini.core.php#ini.short-open-tag

请尝试以下代码。

<?php 
    // ROWS DISPLAYED PER PAGE
    $rows_per_page = 1;

    // GET PAGE NUMBER
    $page = $_GET['page'];
    $offset = (!empty($page)) ? $page : $page = 1;

    // URL CLEAN UP
    $self = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
    $self = str_replace("page={$offset}", "", $self); 

    // GET LIST OF STATES
    $offset = ($page) ? ($page - 1) * $rows_per_page : 0; 
    $id = $_GET['id'];
    $sql = "SELECT * FROM updates WHERE update_categoryID = '$id' ORDER BY update_date          DESC LIMIT {$offset},{$rows_per_page}";
    $result = mysql_query($sql)or die('Error, query failed');

    // GET NUMBER OF PAGES
    $query1 = "SELECT * FROM updates WHERE update_categoryID = '$id'"; 
    $result1 = mysql_query($query1)or die('Error, query failed');
    $total = mysql_num_rows($result1);
    $NumPgs = ceil($total/$rows_per_page);

    while($row = mysql_fetch_assoc($result)) 
    {
    ?>
    <h2><?php echo $row['update_title']; ?></h2>
     <p class="datetime"><?php echo $row['update_date'];?></p>
     <br>
     <p class="post"><?php echo $row['update_content'];?></p> 
     <a href="blogcomment.php?id=<?php echo $row['update_id'];?>">Post a Comment</a>
 <?php
}
?>
    <span style="float:right">
   <? echo ($prev = ($NumPgs > 0 && $page!=1) ? "<a href=\"{$self}&page=".($page- 1)."\">Prev</a>&nbsp;&nbsp;": 
   "Prev&nbsp;&nbsp;"); ?>
    [Page <?php echo $page; ?>]
   <? echo ($next = ($page < $NumPgs) ? "<a href=\"{$self}&page=".     ($page+1)."\">Next</a>": 
    "Next");?>&nbsp;
    <b>&nbsp;<? echo $offset+1?> to <?=$offset+$rows_per_page?>, of <? echo $total?> Entries</b>
    </span>
    </div>