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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?