简单实用的分页函数(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');

<<';}else{echo '<<';} ?>  
   >>';}else{echo '>>';} ?>

转载于:https://www.cnblogs.com/Iamlein/archive/2007/06/30/2375957.html

你可能感兴趣的:(简单实用的分页函数(Php+mysql))