简单实用的分页函数(Php+mysql)

function rs($sql,$conn){
 $rs=mysql_query($sql,$conn);
 $str=array();
 $ct=0; 
 if($row=mysql_fetch_assoc($rs)){
  do{
   foreach($row as $k=>$v){
    $str[$ct][$k]=$v;
   }
   $ct++;
  }while($row=mysql_fetch_assoc($rs));
 } else {
  //echo $sql . mysql_error();
 }
 return $str;
}
function dpage($sql,$conn,$npp,$keep=array(),$pagestr='page',$link=''){
 global $_GET;
 $totalrs=mysql_num_rows(
        mysql_query($sql,$conn)
       );
 $totalpage=ceil($totalrs/$npp)-1; 
 $page=abs(intval($_GET[$pagestr]));
 if($page>$totalpage){
  $page=$totalpage;
 } 
 $sql.=' limit '.($npp*$page).','.$npp;
 $qstr=$link.'?';
 $connquote='';
 foreach($keep as $k){
  $connquote='&';
  $qstr.=$k.'='.$_GET[$k];
 }
 $prev=false;
 $next=false;
 if($page>0){
  $prev=$qstr.$connquote.$pagestr.'='.($page-1);
 }
 if($page<$totalpage){
  $next=$qstr.$connquote.$pagestr.'='.($page+1);
 }
 return array(rs($sql,$conn),$prev,$next,($page+1).'/'.($totalpage+1));
}

 

usage: 

 

 list($pac,$prev,$next,$info) = dpage("SELECT o.id,t.namevalue,t.value,t.point pp,o.point rp,o.status,day(now())-day(order_date) wt from orders o,trade_price t where o.user_id=".$_SESSION["uid"]."  and o.tradetype=t.name order by o.status,o.point",$conn,2,array('pagead'),'pagepac');

<?php if ($prev){echo '<a href="'.$prev.'">&lt;&lt;</a>';}else{echo '&lt;&lt;';} ?>&nbsp;<?php echo $info; ?>&nbsp;
   <?php if ($next){echo '<a href="'.$next.'">&gt;&gt;</a>';}else{echo '&gt;&gt;';} ?>

posted @ 2007-06-30 17:07  MultiThread-PHP  阅读(182)  评论(0编辑  收藏  举报