To implement a function of recording operation history
- A function similar to the undo and anti-undo functions. (Realize forward and backward operations)
- Log in to the discuz forum to view the posts (you can go forward and backward to view the posts, and view the post history)
- The logic is the same as the forward and backward functions of the Windows Explorer address bar.
Based on this need, a data structure is implemented. I wrote a general class, temporarily called the history class.
[The principle is similar to that of a clock. When instantiating an object, you can construct a ring with a length of N (the length can be determined as needed) nodes]
Then integrate various operations. Forward, backward, insert, modify insert.
The class can construct an array. Or pass in array parameters to construct an object.
After each operation, you can get the array after the operation.
After the operation data can be saved in a suitable way according to your needs. Put it in a cookie or session, or serialize it, or convert it to json data and save it in the database, or put it in a file. Convenient for next time use. In order to facilitate expansion, store more data. Specifically, each piece of data is also an array record.
For example, expand as needed: array('path'=>'D:/www/','sss'=>value)
------------------------------------------------ ----------------------------------
By the way, I posted a file for debugging variables that I wrote.
- pr() can format and highlight output variables. pr($arr),pr($arr,1) is to exit after output.
- debug_out() is used to output multiple variables. The default is to exit.
- debug_out($_GET,$_SERVER,$_POST,$arr) ;
- include 'debug.php';
- /**
- * History operation class
- * Pass in or construct an array. In the form:
- array(
- 'history_num'=>20, //Total number of queue nodes
- 'first'=>0, //Starting position, starting from 0. Array index value
- 'last'=> ;0, //End position, starting from 0.
- 'back'=>0, //How many steps back from the first position, the difference.
- 'history'=>array( //Array, storing the operation queue .
- 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) {//The array is empty. Construct a circular queue.
- $history=array();
- for ($i=0; $i < $num; $i++) {
- array_push($history,array('path'=>''));
- }
- $ array=array(
- 'history_num'=>$num,
- 'first'=>0,//Starting position
- 'last'=>0,//Ending position
- '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 values under the loop. Similar to clock loop.
- return ($i+$n)<$this->history_num ? ($i+$n):($i+$n-$this->history_num);
- }
- function prevNum($i,$n= 1){//The last value i on the loop. Go back N positions.
- return ($i-$n)>=0 ? ($i-$n) : ($i-$n+$this->history_num);
- }
-
- function minus($i,$j){ //The only difference between two clockwise points is i-j
- return ($i > $j) ? ($i - $j):($i-$j+$this->history_num);
- }
-
-
- function getHistory (){//Return array, used for saving or serialization operations.
- 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) {//If there is a back operation record, insert it.
- $this->goedit($path);
- return;
- }
- if ($this->history[0]['path']=='') {//Just constructed, no need to add one. First position Not moving forward
- $this->history[$this->first]['path']=$path;
- return;
- }else{
- $this->first=$this->nextNum($ this->first);//Move the first position forward
- $this->history[$this->first]['path']=$path;
- }
- if ($this->first==$ this->last) {//The starting position and the ending position meet
- $this->last=$this->nextNum($this->last);//The end position moves forward.
- }
- }
-
- function goback(){//Return the address N steps back from first.
- $this->back+=1;
- //The maximum number of steps back is the difference from the starting point to the end point (clockwise difference)
- $mins=$this->minus($this->first,$this- >last);
- if ($this->back >= $mins) {//Back to the last point
- $this->back=$mins;
- }
-
- $pos=$this-> prevNum($this->first,$this->back);
- return $this->history[$pos]['path'];
- }
-
- function gonext(){//Back N from first Take one step forward.
- $this->back-=1;
- if ($this->back<0) {//Return to the last point
- $this->back=0;
- }
- return $this->history [$this->prevNum($this->first,$this->back)]['path'];
- }
- function goedit($path){//Back to a certain point without moving forward It's a modification.The firs value is the last value.
- $pos=$this->minus($this->first,$this->back);
- $pos=$this->nextNum($pos);//Next
- $this-> ;history[$pos]['path']=$path;
- $this->first=$pos;
- $this->back=0;
- }
-
- //Can I go back
- function isback() {
- if ($this->back < $this->minus($this->first,$this->last)) {
- return ture;
- }
- return false;
- }
- // Is it possible to move forward
- function isnext(){
- if ($this->back>0) {
- return true;
- }
- return false;
- }
- }
-
-
- //Test code.
- $hi=new history(array(),6);//If an empty array is passed in, the array construction will be initialized.
- 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());//Constructed directly with array.
- $ss->add('asdfasdf');
- $ss->goback();
- pr($ss->getHistory());
-
-
- ?>
Copy code
- /**
- * Get the name of the variable
- * eg hello="123" Get the ss string
- */
- function get_var_name(&$aVar){
- foreach($GLOBALS as $key=>$var)
- {
- if($aVar== $GLOBALS[$key] && $key!="argc"){
- return $key;
- }
- }
- }
-
- /**
- * Formatted output variables, or objects
- * @param mixed $var
- * @param boolean $exit
- */
- function pr($var,$exit = false){
- ob_start();
- $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;}';
- 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();//Buffer output to $out variable
-
- $out=preg_replace('/"(. *)"/','"'.'\1'.'"',$out);//Highlight string variable
- $out=preg_replace ('/=>(.*)/','=>'.''.'\1'.'',$out);/ /Highlight=>The following value
- $out=preg_replace('/[(.*)]/','['.'\1'.']',$out);//Highlight variable
-
- $from = array( ' ','(',')','=>');
- $to = array(' ','(',')','=>');
- $out=str_replace($from,$to,$ out);
-
- $keywords=array('Array','int','string','class','object','null');//Keyword highlighting
- $keywords_to=$keywords;
- foreach ($keywords as $key=>$val)
- {
- $keywords_to[$key] = ''.$val.'';
- }
- $ out=str_replace($keywords,$keywords_to,$out);
- echo $style.'
<b id="debug_keywords">'.get_var_name($var).'< ;/b> = '.$out.' ';
- if ($exit) exit;//Exit if true
- }
-
- /**
- * Debug output variables, object values.
- * Any number of parameters (variables of any type)
- * @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 code
|