php使用記錄功能

來源:互聯網
上載者:User
本篇文章主要介紹php使用記錄功能,感興趣的朋友參考下,希望對大家有所協助。

為實現一個記錄操作曆史的功能

1. 和撤銷,反撤銷功能類似的一個功能。(實現操作的前進後退)
2. 和discuz論壇登入後查看文章(可以前進後退查看過的文章,還有文章查看記錄)
3. 邏輯和windows資源管理員地址欄前進後退功能一樣。

根據這種需要,實現了一個資料結構。寫了一個通用的類,暫叫記錄類吧。

【原理和時鐘類似。執行個體化對象時可以構造長度為N(可以根據需要定長度)個節點的環】

然後整合各種操作。前進、後退、插入、修改插入。

類可以構造一個數組。或者傳入數組參數構造一個對象。 每次操作之後可以取得操作後的數組。 操作完的 資料可以根據自己的需要以合適的方式儲存。 放在cookie,session裡面,或者序列化,或轉為json資料儲存在資料庫裡,或者放在檔案裡面都可以。 方便下一次使用。

為了便於擴充,存放更多的資料。具體每一條資料也是一條數組記錄。
比如根據需要進行擴充:array('path'=>'D:/www/','sss'=>value)

順便貼出,自己寫的調試變數用的一個檔案。

1. pr()可以格式化並高亮輸出變數。pr($arr),pr($arr,1)是輸出後退出。
2. debug_out() 用來輸出多個變數。預設為退出。
3. debug_out($_GET,$_SERVER,$_POST,$arr);

history.class.php檔案:

<?php include 'debug.php';/*** 記錄操作類* 傳入或者構造一個數組。形如:array(  'history_num'=>20,  //隊列節點總共個數  'first'=>0,     //起始位置,從0開始。數組索引值  'last'=>0,      //終點位置,從0開始。  'back'=>0,      //從first位置倒退了多少步,差值。  'history'=>array(  //數組,存放操作隊列。    array('path'=>'D:/'),    array('path'=>'D:/www/'),    array('path'=>'E:/'),    array('path'=>'/home/')    ……  ))*/class history{  var $history_num;  var $first;  var $last;  var $back;  var $history=array();  function __construct($array=array(),$num=12){    if (!$array) {//數組為空白.構造一個迴圈隊列。      $history=array();      for ($i=0; $i < $num; $i++) {        array_push($history,array('path'=>''));      }      $array=array(        'history_num'=>$num,        'first'=>0,//起始位置        'last'=>0,//終點位置        'back'=>0,          'history'=>$history      );    }        $this->history_num=$array['history_num'];    $this->first=$array['first'];    $this->last=$array['last'];    $this->back=$array['back'];     $this->history=$array['history'];    }  function nextNum($i,$n=1){//環路下n一個值。和時鐘環路類似。    return ($i+$n)<$this->history_num ? ($i+$n):($i+$n-$this->history_num);  }  function prevNum($i,$n=1){//環路上一個值i。回退N個位置。    return ($i-$n)>=0 ? ($i-$n) : ($i-$n+$this->history_num);     }  function minus($i,$j){//順時針兩點只差,i-j    return ($i > $j) ? ($i - $j):($i-$j+$this->history_num);  }  function getHistory(){//返回數組,用於儲存或者序列化操作。    return array(      'history_num'=> $this->history_num,      'first'   => $this->first,           'last'    => $this->last,      'back'    => $this->back,           'history'  => $this->history    );  }  function add($path){    if ($this->back!=0) {//有後退操作記錄的情況下,進行插入。      $this->goedit($path);      return;    }        if ($this->history[0]['path']=='') {//剛構造,不用加一.首位不前移      $this->history[$this->first]['path']=$path;      return;    }else{      $this->first=$this->nextNum($this->first);//首位前移      $this->history[$this->first]['path']=$path;          }    if ($this->first==$this->last) {//起始位置與終止位置相遇      $this->last=$this->nextNum($this->last);//末尾位置前移。    }      }  function goback(){//返回從first後退N步的地址。    $this->back+=1;    //最大後退步數為起點到終點之差(順時針之差)    $mins=$this->minus($this->first,$this->last);    if ($this->back >= $mins) {//退到最後點      $this->back=$mins;    }    $pos=$this->prevNum($this->first,$this->back);    return $this->history[$pos]['path'];  }  function gonext(){//從first後退N步的地方前進一步。    $this->back-=1;    if ($this->back<0) {//退到最後點      $this->back=0;    }    return $this->history[$this->prevNum($this->first,$this->back)]['path'];  }  function goedit($path){//後退到某個點,沒有前進而是修改。則firs值為最後的值。    $pos=$this->minus($this->first,$this->back);    $pos=$this->nextNum($pos);//下一個       $this->history[$pos]['path']=$path;    $this->first=$pos;    $this->back=0;  }  //是否可以後退  function isback(){    if ($this->back < $this->minus($this->first,$this->last)) {      return ture;    }    return false;  }  //是否可以前進  function isnext(){    if ($this->back>0) {      return true;    }    return false;  }}//測試代碼。$hi=new history(array(),6);//傳入空數組,則初始化數組構造。for ($i=0; $i <8; $i++) {   $hi->add('s'.$i);  }pr($hi->goback());pr($hi->goback());pr($hi->goback());pr($hi->gonext());pr($hi->gonext());pr($hi->gonext());pr($hi->gonext());$hi->add('asdfasdf');$hi->add('asdfasdf2');pr($hi->getHistory());$ss=new history($hi->getHistory());//直接用數組構造。$ss->add('asdfasdf');$ss->goback();pr($ss->getHistory());?>

debug.php檔案:

<?php /** * 擷取變數的名字 * eg hello="123" 擷取ss字串 */function get_var_name(&$aVar){  foreach($GLOBALS as $key=>$var)  {    if($aVar==$GLOBALS[$key] && $key!="argc"){      return $key;    }  }}/** * 格式化輸出變數,或者對象 * @param mixed  $var * @param boolean $exit */function pr($var,$exit = false){  ob_start();  $style='<style>  pre#debug{margin:10px;font-size:13px;color:#222;font-family:Consolas ;line-height:1.2em;background:#f6f6f6;border-left:5px solid #444;padding:5px;width:95%;word-break:break-all;}  pre#debug b{font-weight:400;}  #debug #debug_str{color:#E75B22;}  #debug #debug_keywords{font-weight:800;color:00f;}  #debug #debug_tag1{color:#22f;}  #debug #debug_tag2{color:#f33;font-weight:800;}  #debug #debug_var{color:#33f;}  #debug #debug_var_str{color:#f00;}  #debug #debug_set{color:#0C9CAE;}</style>';  if (is_array($var)){    print_r($var);  }  else if(is_object($var)){    echo get_class($var)." Object";  }  else if(is_resource($var)){    echo (string)$var;  }  else{    echo var_dump($var);  }    $out = ob_get_clean();//緩衝輸出給$out 變數  $out=preg_replace('/"(.*)"/','<b id="debug_var_str">"'.'\\1'.'"</b>',$out);//高亮字串變數  $out=preg_replace('/=\>(.*)/','=>'.'<b id="debug_str">'.'\\1'.'</b>',$out);//高亮=>後面的值  $out=preg_replace('/\[(.*)\]/','<b id="debug_tag1">[</b><b id="debug_var">'.'\\1'.'</b><b id="debug_tag1">]</b>',$out);//高亮變數  $from = array('  ','(',')','=>');  $to  = array(' ','<b id="debug_tag2">(</i>','<b id="debug_tag2">)</b>','<b id="debug_set">=></b>');  $out=str_replace($from,$to,$out);    $keywords=array('Array','int','string','class','object','null');//關鍵字高亮  $keywords_to=$keywords;  foreach($keywords as $key=>$val)  {      $keywords_to[$key] = '<b id="debug_keywords">'.$val.'</b>';  }  $out=str_replace($keywords,$keywords_to,$out);   echo $style.'<pre id="debug"><b id="debug_keywords">'.get_var_name($var).'</b> = '.$out.'</pre>';  if ($exit) exit;//為真則退出}/** * 調試輸出變數,對象的值。 * 參數任意個(任意類型的變數) * @return echo */function debug_out(){  $avg_num = func_num_args();  $avg_list= func_get_args();  ob_start();  for($i=0; $i < $avg_num; $i++) {    pr($avg_list[$i]);  }  $out=ob_get_clean();  echo $out;  exit;}?>

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

相關推薦:

php+ajax非同步上傳檔案或圖片功能的方法

PHP preg_match實現Regex匹配功能的方法

php產生code128條碼的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.