Home Backend Development PHP Tutorial php uses history function

php uses history function

Jun 07, 2018 pm 04:50 PM
php history record

本篇文章主要介绍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 &#39;debug.php&#39;;
/**
* 历史记录操作类
* 传入或者构造一个数组。形如:
array(
  &#39;history_num&#39;=>20,  //队列节点总共个数
  &#39;first&#39;=>0,     //起始位置,从0开始。数组索引值
  &#39;last&#39;=>0,      //终点位置,从0开始。
  &#39;back&#39;=>0,      //从first位置倒退了多少步,差值。
  &#39;history&#39;=>array(  //数组,存放操作队列。
    array(&#39;path&#39;=>&#39;D:/&#39;),
    array(&#39;path&#39;=>&#39;D:/www/&#39;),
    array(&#39;path&#39;=>&#39;E:/&#39;),
    array(&#39;path&#39;=>&#39;/home/&#39;)
    ……
  )
)
*/
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(&#39;path&#39;=>&#39;&#39;));
      }
      $array=array(
        &#39;history_num&#39;=>$num,
        &#39;first&#39;=>0,//起始位置
        &#39;last&#39;=>0,//终点位置
        &#39;back&#39;=>0,  
        &#39;history&#39;=>$history
      );
    }    
    $this->history_num=$array[&#39;history_num&#39;];
    $this->first=$array[&#39;first&#39;];
    $this->last=$array[&#39;last&#39;];
    $this->back=$array[&#39;back&#39;]; 
    $this->history=$array[&#39;history&#39;];  
  }
  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(
      &#39;history_num&#39;=> $this->history_num,
      &#39;first&#39;   => $this->first,     
      &#39;last&#39;    => $this->last,
      &#39;back&#39;    => $this->back,     
      &#39;history&#39;  => $this->history
    );
  }
  function add($path){
    if ($this->back!=0) {//有后退操作记录的情况下,进行插入。
      $this->goedit($path);
      return;
    }    
    if ($this->history[0][&#39;path&#39;]==&#39;&#39;) {//刚构造,不用加一.首位不前移
      $this->history[$this->first][&#39;path&#39;]=$path;
      return;
    }else{
      $this->first=$this->nextNum($this->first);//首位前移
      $this->history[$this->first][&#39;path&#39;]=$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][&#39;path&#39;];
  }
  function gonext(){//从first后退N步的地方前进一步。
    $this->back-=1;
    if ($this->back<0) {//退到最后点
      $this->back=0;
    }
    return $this->history[$this->prevNum($this->first,$this->back)][&#39;path&#39;];
  }
  function goedit($path){//后退到某个点,没有前进而是修改。则firs值为最后的值。
    $pos=$this->minus($this->first,$this->back);
    $pos=$this->nextNum($pos);//下一个   
    $this->history[$pos][&#39;path&#39;]=$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(&#39;s&#39;.$i);  
}
pr($hi->goback());
pr($hi->goback());
pr($hi->goback());
pr($hi->gonext());
pr($hi->gonext());
pr($hi->gonext());
pr($hi->gonext());
$hi->add(&#39;asdfasdf&#39;);
$hi->add(&#39;asdfasdf2&#39;);
pr($hi->getHistory());
$ss=new history($hi->getHistory());//直接用数组构造。
$ss->add(&#39;asdfasdf&#39;);
$ss->goback();
pr($ss->getHistory());
?>
Copy after login

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=&#39;<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>&#39;;
  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(&#39;/"(.*)"/&#39;,&#39;<b id="debug_var_str">"&#39;.&#39;\\1&#39;.&#39;"</b>&#39;,$out);//高亮字符串变量
  $out=preg_replace(&#39;/=\>(.*)/&#39;,&#39;=>&#39;.&#39;<b id="debug_str">&#39;.&#39;\\1&#39;.&#39;</b>&#39;,$out);//高亮=>后面的值
  $out=preg_replace(&#39;/\[(.*)\]/&#39;,&#39;<b id="debug_tag1">[</b><b id="debug_var">&#39;.&#39;\\1&#39;.&#39;</b><b id="debug_tag1">]</b>&#39;,$out);//高亮变量
  $from = array(&#39;  &#39;,&#39;(&#39;,&#39;)&#39;,&#39;=>&#39;);
  $to  = array(&#39; &#39;,&#39;<b id="debug_tag2">(</i>&#39;,&#39;<b id="debug_tag2">)</b>&#39;,&#39;<b id="debug_set">=></b>&#39;);
  $out=str_replace($from,$to,$out);  
  $keywords=array(&#39;Array&#39;,&#39;int&#39;,&#39;string&#39;,&#39;class&#39;,&#39;object&#39;,&#39;null&#39;);//关键字高亮
  $keywords_to=$keywords;
  foreach($keywords as $key=>$val)
  {  
    $keywords_to[$key] = &#39;<b id="debug_keywords">&#39;.$val.&#39;</b>&#39;;
  }
  $out=str_replace($keywords,$keywords_to,$out); 
  echo $style.&#39;<pre id="debug"><b id="debug_keywords">&#39;.get_var_name($var).&#39;</b> = &#39;.$out.&#39;
'; 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; } ?>
Copy after login

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

php+ajax异步上传文件或图片功能的方法

PHP preg_match实现正则表达式匹配功能的方法

php生成code128条形码的方法

The above is the detailed content of php uses history function. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles