dpitqpax07626
2011-09-03 14:08
浏览 53
已采纳

使用mysql_data_seek进行分页的问题

I've got the problem with using the function mysql_data_seek() in my pagination. When clicking on links, it produces no problem, but when clicking on the last page of the pagination link, it produces an error like this:

Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 14 is invalid for MySQL result index 6 (or the query data is unbuffered)

My code is below:

      $rpp = 3; 
      $adjacents = 4;
      $page = (!empty($_GET["page"]) ? intval($_GET["page"]) : 1);
    if($page<=0) $page = 1;
        $reload = $_SERVER['PHP_SELF'];
        $sql = "SELECT * FROM ".TABLE_IMAGE." ORDER BY id ASC";
        $qry = mysql_query($sql, $con);
        $tcount = mysql_num_rows($qry);
        $tpages = ($tcount) ? ceil($tcount/$rpp) : 1; 
                $i = 1;
        $count = 0;
        $j = ($page-1)*$rpp;
        while(($result = mysql_fetch_array($qry)) && (($count<$rpp) && ($j<=$tcount))){                                                            mysql_data_seek($qry,$j);
                $id = $result['id'];
        $img = $result['path'];
        $title = $result['title'];
        $detail = $result['detail'];
        $priority = $result['priority'];
        $active = $result['isActive'];                      
?><div id="block-image-slider"  class="<?php echo(($i%2==0)?'even':'odd')?>">
     <h2><?php echo $title ?></h2><span class="operation">[<a href="?action=edit&section=slider&id=<?php echo $id ?>">កែប្រែ</a>|<a href="?action=delete&section=slider&id='<?php echo $id ?>'">លុប</a>]</span>
            <div class="block-slider-image-body">
                <div class="left">
                     <ul>
                         <li>លេខរៀងទី<span class="space"><?php echo $id ?></span></li>
                         <li>កំនត់អទិភាពទី<span class="space"><?php echo $priority ?></span></li>
                         <li>ត្រូវបានបង្ហាញអោយឃើញ<span class="space"><?php echo (($active==1)?'បង្ហាញ':'មិនបង្ហាញ')?></span></li>
                         <li>អត្ថបទពេញ<div class="detail"><?php echo $detail ?></div></li>
                     </ul>
                </div>
             <div class="right">
          <img src="<?php echo '../../image/Slider/'.$img ?>" alt="<?php echo $title ?>" width="170" height="100" />
  </div>
 <div style="clear:both;"></div>
 </div>
</div>
 <?php
    $j++;
    $count++;
    $i++;
}
  include("../include/paginate.php");
  echo paginate_three($reload, $page, $tpages, $adjacents);

and this is the paginate.php code

<?php 
     function paginate_three($reload, $page, $tpages, $adjacents) {
    $prevlabel = "&lsaquo; Prev";
    $nextlabel = "Next &rsaquo;";

    $out = "<div class=\"pagin\">
";

    // previous
    if($page==1) {
        $out.= "<span>" . $prevlabel . "</span>
";
    }
    elseif($page==2) {
        $out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>
";
    }
    else {
        $out.= "<a href=\"" . $reload . "?action=slider&page=" . ($page-1) . "\">" . $prevlabel . "</a>
";
    }

    // first
    if($page>($adjacents+1)) {
        $out.= "<a href=\"" . $reload . "\">1</a>
";
    }

    // interval
    if($page>($adjacents+2)) {
        $out.= "...
";
    }

    // pages
    $pmin = ($page>$adjacents) ? ($page-$adjacents) : 1;
    $pmax = ($page<($tpages-$adjacents)) ? ($page+$adjacents) : $tpages;
    for($i=$pmin; $i<=$pmax; $i++) {
        if($i==$page) {
            $out.= "<span class=\"current\">" . $i . "</span>
";
        }
        elseif($i==1) {
            $out.= "<a href=\"" . $reload . "?action=slider\">" . $i . "</a>
";
        }
        else {
            $out.= "<a href=\"" . $reload . "?action=slider&page=" . $i . "\">" . $i . "</a>
";
        }
    }

    // interval
    if($page<($tpages-$adjacents-1)) {
        $out.= "...
";
    }

    // last
    if($page<($tpages-$adjacents)) {
        $out.= "<a href=\"" . $reload . "?action=slider&page=" . $tpages . "\">" . $tpages . "</a>
";
    }

    // next
    if($page<$tpages) {
        $out.= "<a href=\"" . $reload . "?action=slider&page=" . ($page+1) . "\">" . $nextlabel . "</a>
";
    }
    else {
        $out.= "<span>" . $nextlabel . "</span>
";
    }

    $out.= "</div>";

    return $out;
}

?>

PS: when clicking on the last link of the pagination page button, it displays the error message like the above. Moreover, I have no idea why the first record from my database always show in each page. Any help would be appreciated. Thank you.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongrong9938 2011-09-03 14:10
    已采纳

    There is no problem. You're requesting row 14 when there are only 6.

    Also, I think it's more efficient to use LIMIT [from], [amount] for pagination.

    打赏 评论
  • duanlinzhen7235 2011-09-03 14:10

    if you're using mysql_data_seek() for the pagination, you're already in BIG trouble.

    Use LIMIT operator in the query instead

    打赏 评论

相关推荐 更多相似问题