开发者

Issue using mysql_data_seek for pagination

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开发者_如何学Python&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\">\n";

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

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

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

    // 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>\n";
        }
        elseif($i==1) {
            $out.= "<a href=\"" . $reload . "?action=slider\">" . $i . "</a>\n";
        }
        else {
            $out.= "<a href=\"" . $reload . "?action=slider&page=" . $i . "\">" . $i . "</a>\n";
        }
    }

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

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

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

    $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.


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.


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

Use LIMIT operator in the query instead

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜