PHP实用类

来源:互联网 发布:smarty二维数组遍历 编辑:程序博客网 时间:2024/04/30 19:50

1.分页类

<?php

/////////////////////////////分页类/////////////////////////////
/*
* 程序原做:北极星
* e_mail:superstarfu1985@163.com
* QQ:327553822
* 时间:2009.2.2
*调用方式:
*   $p  = new page($rows,12,6);
*   $p->page_set($_GET['page']);
*   $limit = $p->limit();
*   $p->page_show();
*显示:
*   << < 1 2 3 4 5 6 7 > >>
*
*$page_gap_length-->当前页到左右两边页码之间的间距
*/
class page
{
 private $row_per_page;   //每页取出的记录条数
 private $current_page;  //当前页
 private $page_gap_length; //当前页到左右两边页码之间的间距
 private $total_pages;  //总页数
 private $pre_page;   //上一页
 private $next_page;   //下一页
 private $link_head;   //得到"page="之前的字符串
 
 public function page($total,$row_per_page=5,$page_gap_length=2)
 {
  $this->row_per_page  = $row_per_page;
  $this->page_gap_length = $page_gap_length;
  $this->total_pages  = ceil($total/$row_per_page);
  $this->parse_query();
 }
 
 public function parse_query()
 {  
  $arr  = explode("page=",$_SERVER['QUERY_STRING']); //分割
  $arr_left = $arr[0];
  if(empty($arr_left)){
   $this->link_head = $_SERVER['PHP_SELF']."?";
  } else {
   $arr_left = (substr($arr_left,-1) == "&") ? $arr_left: $arr_left."&";
   $this->link_head = $_SERVER['PHP_SELF']."?".$arr_left;
  }
 }
 /////////////////下拉列表方式,分页跳转//////////////
 public function page_sel()
 {
  if($this->total_pages <= 1){
   $sel = "";
  } else {
   $sel = "<select onchange='window.location=this.value'>";
   for($i=1; $i<=$this->total_pages; $i++){
    $sel .= ($i == $this->current_page) ? "<option value='".$this->link_head."page=".$i."' selected>".$i."</option>" : "<option value='".$this->link_head."page=".$i."'>".$i."</option>";
   }
   $sel .= "</select>";
  }
  return $sel;
 }
 ////////////////////////由GET传值$_GET['page'],确定当前页、上一页、下一页////////////////////////
 public function page_set($current_page)
 {
  if(!is_numeric($current_page) || empty($current_page) || $current_page == 0){
   $this->current_page = 1;
  } else {
   $this->current_page = $current_page;
  }
   $this->next_page = ($this->total_pages <= ($this->current_page+1)) ? $this->total_pages : ($this->current_page+1);
   $this->pre_page  = ($this->current_page-1) < 1 ? 1 : ($this->current_page-1);
 }
 
 public function limit()
 {
  return ($this->current_page-1)*$this->row_per_page.",".$this->row_per_page;
 }
 public function page_show()
 {
   if(!empty($this->total_pages)){
  $page_list = "<a href='".$this->link_head."page=1' title='首页'><<</a>&nbsp;";
  $page_list .= "<a href='".$this->link_head."page=".$this->pre_page."' title='上一页'><</a>&nbsp;";
  $m = $this->current_page-$this->page_gap_length;
  $n = $this->current_page+$this->page_gap_length;
  $p = ($m <= 0) ? 1 : $m;
  $q = ($n >= $this->total_pages) ? $this->total_pages : $n;
  for( $i = $p; $i<=$q; $i++){
   $page_list .= "<a href='".$this->link_head."page=".$i."' title='第".$i."页'>".$i."</a>&nbsp;";
  }

  $page_list .= "<a href='".$this->link_head."page=".$this->next_page."' title='下一页'>></a>&nbsp;";
  $page_list .= "<a href='".$this->link_head."page=".$this->total_pages."' title='尾页'>>></a>";
  $page_list  .= "&nbsp;&nbsp;&nbsp;".$this->page_sel();
  echo $page_list;
   }
 }
}
?>

 

2.字符串截取类

 

<?php

   

   //字符串处理函数
 /*
 *$str:要截取的字符串
 *$start:从哪里开始截取
 *$len:截取长度
 */
function msubstr($str,$start,$len)//字符位置从0开始
{
 $strlen=$start+$len;
 for($i=0;$i<$strlen;$i++)
 {
  //如果读取的是双字节字符,则一次要读取两个字节的长度,否则,会出现意外情况
  if(ord(substr($str,$i,1))>0xa0)
  {
   $tmpstr.=substr($str,$i,2);
   $i++;
  }
  else{
 
   $tmpstr.=substr($str,$i,1);
  }

 }
 $tmpstr = (strlen($str) <= $len) ? $tmpstr : $tmpstr."...";
 
 return $tmpstr;
 
}

 

?>

 

3.表单类

<?php

 

   //////////////////////////表单类///////////////////////
class form
{
 private $action;
 private $method;
 private $enctype;
 private $id;
 private $name;
 private $sty; //表单样式
 
 public function form($action="",$id="",$name="",$method="POST",$enctype="multipart/form-data",$sty="")
 {
  $this->action = $action;
  $this->method = $method;
  $this->enctype = $enctype;
  $this->id  = $id;
  $this->name  = $name;
  $this->sty  = $sty;
 }
 public function form_start()
 {
  echo "<form action='".$this->action."' method='".$this->method."' enctype='".$this->enctype."' style='".$sty."' id='".$this->id."' name='".$this->name."'>";
 }
 public function create_text($type,$name,$value,$sty="",$id="",$onclick="javascript:void(0);")
 {
  $text = "<input type='".$type."' name='".$name."' value='".$value."' id='".$id."' style='".$sty."' onclick='".$onclick."'>";
  echo $text;
 }
 public function create_textarea($name,$value,$sty="")
 {
  $textarea = "<textarea name='".$name."' style='".$sty."'>".$value."</textarea>";
  echo $textarea;
 }
 public function create_file($name,$sty="")
 {
  $file = "<input type='file' name='".$name."' style='".$sty."'>";
  echo $file;
 }
 public function create_sub($name,$value="提交",$sty="")
 {
  $sub = "<input type='submit' name='".$name."' value='".$value."' style='".$sty."'>";
  echo $sub;
 }
 public function form_end()
 {
  echo "</form>";
 }

}

 

?>

 

 4.文件上传类

<?PHP

/*********************文件上传类**********************/
/**
 *调用方法:
 *  $upload = new uploadfile($_FILES['pic']);
 * $upload->upload("pic");
 *
 */
class uploadfile
{
 private $root; //文件存放根目录
 
 //文件类型
 private $typearr = array('pic'=>array(".jpg",".gif",".bmp",".png"),
          'txt'=>array(".txt",".doc",".xls",".ppt"),
          'rar'=>array(".rar",".zip")); 
       
 private $posarr = array(); //存放对应文件类型格式的数组
       
 private $filearr = array(); //存放上传文件各项参数的数组
 
 //php5构造函数
 public function __construct(&$arr,$root="./file/")
 {
  $this->root  = $root;
  $this->filearr = $arr;
  if(!file_exists($this->root)){
   $this->_mkdir($this->root);
  }
 }
 //php4构造函数
 public function uploadfile(&$arr,$root="./file/")
 {
 
  $this->__construct(&$arr,$root="./file/"); 
 
 }
 
 //目录创建
 public function _mkdir($root)
 {
  chmod($root,0777); //分配给目录最大可能的访问权
  mkdir($root); //创建目录
 }
 
 //上传文件
 public function upload($ftype = "pic")
 {
  
  foreach($this->typearr as $key => $va){
   if($ftype == $key){
    $this->posarr = $va; //将对应文件格式存放到数组$posarr中
    break;
   }
  }
  
  foreach($this->filearr as $key => $va){
   if(is_array($va)){
    foreach($this->filearr['error'] as $p => $error){
     //没有错误发生
     if($error == UPLOAD_ERR_OK){
      $tmp_name = $this->filearr["tmp_name"][$p];
      $name  = $this->filearr["name"][$p];
      $pos  = strrchr($name,".");
      
      if(!in_array($pos,$this->posarr)){
       echo "<script language='javascript'>alert('你上传的文件格式不对吧?^-^');history.go(-1);</script>";
       exit();
      }
      $rename = date("YmdHis").$p.$pos;
      $path = $this->root.$rename;
      move_uploaded_file($tmp_name,$path);
     }

    }
   } else {
    $tmp_name = $this->filearr['tmp_name'];
    $name  = $this->filearr['name'];
    $pos  = strrchr($name,".");
    if(!in_array($pos,$this->posarr)){
     echo "<script language='javascript'>alert('你上传的文件格式不对吧?^-^');history.go(-1);</script>";
     exit();
    }
    $rename = date("YmdHis").$pos;
    $path = $this->root.$rename;
    move_uploaded_file($tmp_name,$path);
   }
   
  }
 }
 
}

?>

未完待续。。。

原创粉丝点击