x
首页 前端[Javascript] 数据库类

数据库类

发表于 2010-12-23 - 浏览:2066 评论:0 收藏 0
<?php
/*Need param: hostname username password dbname charset*/
class db{
 private $hostname='';private $username='';private $password='';private $dbname='';private $charset='utf8';private $link;private $result;
 function __construct($db){
  foreach($db as $k=>$v)$this->{$k}=$v;
  $this->link = @mysqli_connect($this->hostname,$this->username,$this->password,$this->dbname) or die('Access Deny!(DB)');
  $this->query("SET NAMES ".$this->charset);
 }
 function __destruct(){/*mysqli_free_result($this->result);*/!empty($this->link) && mysqli_close($this->link);}
 function query($sql){return $this->result = mysqli_query($this->link,$sql);}
 function get_row($sql){return mysqli_fetch_assoc($this->query($sql));}
 function num_rows(){return mysqli_num_rows($this->result);}
 function insert_id(){return mysqli_insert_id($this->link);}
 function fetch_row(){return mysqli_fetch_row($this->result);}
 function fetch_assoc(){return mysqli_fetch_assoc($this->result);}
 function fetch_array(){return mysqli_fetch_array($this->result);}
 function affected_rows(){return mysqli_affected_rows($this->link);}
 
 public $pages = array(
  'total'=>0,#int#总记录数
  'count'=>0,#int#总页数
  'next'=>0,#int#下一页
  'prev'=>0,#int#上一页
  'info'=>'当前:0/0 总共:0条记录',#string#分页基本信息
  'code'=>'当前无记录',#string
  'limit'=>' LIMIT 0,15',#string
  'links'=>array()#array#分页中间链接
 );
 function page($sql,$nonce=1,$show=15,$url=false){//分页
  $this->pages['total']= mysqli_num_rows(mysqli_query($this->link,$sql));
  $this->pages['count']= (int)ceil($this->pages['total'] / $show);
  $this->pages['nonce']= ($nonce>$this->pages['count']&&$this->pages['count']!==0)?$this->pages['count']:$nonce;
  $this->pages['limit']= ' LIMIT '.($this->pages['nonce']-1)*$show.','.$show;
  $this->pages['next'] = $this->pages['nonce']>=$this->pages['count']?$this->pages['count']:$this->pages['nonce']+1;
  $this->pages['prev'] = $this->pages['nonce']<=1?1:$this->pages['nonce']-1;
  $this->pages['info'] = '当前:<b style="color:#d00;">'.$this->pages['nonce'].'</b>/'.$this->pages['count'].' 总共:'.$this->pages['total'].'条记录';
  for($i=-3;$i<4;$i++)if(($this->pages['nonce']+$i)>=1 && ($this->pages['nonce']+$i)<=$this->pages['count']){array_push($this->pages['links'],$this->pages['nonce']+$i);}
  
  if($url!==false && $this->pages['total']>0){//分页代码
   $this->pages['code'] = '<div id="pages"><div id="pagesinfo">'.$this->pages['info'].'</div><div style="float:right;">';
   $this->pages['code'].= $this->pages['nonce']==1?'':('<a '.str_replace('{$num}',1,$url).' title="首页">|<</a>'//IndexPage
           .'<a '.str_replace('{$num}',$this->pages['prev'],$url).' title="上一页"><=</a>');//PrevPage
   foreach($this->pages['links'] as $k=>$v)$this->pages['code'].= '<a '.($v==$this->pages['nonce']?'id="present"':str_replace('{$num}',$v,$url)).' title="第'.$v.'页">'.$v.'</a>';
   $this->pages['code'].= $this->pages['nonce']==$this->pages['count']?'':('<a '.str_replace('{$num}',$this->pages['next'],$url).' title="下一页">=></a>'//NextPage
           .'<a '.str_replace('{$num}',$this->pages['count'],$url).' title="尾页">>|</a>');//LastPage
   $this->pages['code'].= '</div><div class="clear"></div></div>';
  }
  return $sql.$this->pages['limit'];
 }
 
 function uporder($order,$table){//更新排序
  $s='';$ids=array();
  foreach($order as $k=>$v){$s.=" WHEN $k THEN $v";$ids[]=$k;}
  $ids=implode(',', $ids);
  $this->query("UPDATE `".$table."` SET `order`= CASE `id` $s END WHERE `id` IN ($ids)");
 }

文章评论。。。