PHP based on bidirectional loop queue to achieve the historical record of the forward back and other functions _php skills

Source: Internet
Author: User

This article describes the PHP based on bidirectional loop queue to achieve the history of the forward back and other functions. Share to everyone for your reference. Specifically as follows:

To achieve a record operation history of the function

1. And undo, reverse undo function similar to a function. (To achieve the action of the forward back)
2. and Discuz Forum after login to view posts (can go forward back to view posts, as well as posts to view the history)
3. Logic and the Windows Explorer address bar forward back function.

Based on this need, a data structure has been implemented. Write a generic class, called The History class bar.

"The principle is similar to the clock. When instantiating an object, you can construct a loop with a length of n (which can be fixed as long as you want).

Then consolidate the various operations. Forward, backward, insert, modify insert.

Class can construct an array. or pass in an array parameter to construct an object. After each operation, an array of operations can be obtained. The finished data can be saved in the appropriate way according to your own needs. Put it in the cookie,session, or serialize it, or convert it to JSON data in the database, or in a file. Easy to use next time.

For ease of expansion, more data is stored. Each piece of data is also a single array record.
For example, expand according to need: Array (' path ' => ' d:/www/', ' SSS ' =>value)

By the way, a file for debugging variables that you write yourself.

1. PR () can format and highlight output variables. PR ($arr), PR ($arr, 1) is output after exit.
2. Debug_out () is used to output multiple variables. Default is exit.
3. Debug_out ($_get,$_server,$_post, $arr);

history.class.php file:

<?php include ' debug.php '; /** * History Operation class * Incoming or constructed an array. Shape: Array (' History_num ' =>20,//Queue node total number ' first ' =>0,//start position, starting from 0.
  Array index value ' last ' =>0,//end position, starting from 0.
  ' Back ' =>0,//backwards from the first position how many steps, the difference value.
    ' History ' =>array (//array), holding 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) {//array is empty. Constructs a loop queue.
      $history =array ();
      For ($i =0 $i < $num; $i + +) {Array_push ($history, Array (' path ' => ')); } $array =array (' History_num ' => $num, ' first ' =>0,//start position ' last ' =>0,//end position ' Ba
    CK ' =>0, ' history ' => $history);
    } $this->history_num= $array [' History_num '];
    $this->first= $array [' a ']; $this->last= $array [' Last']; 
    $this->back= $array [' Back '];  
  $this->history= $array [' History ']; function Nextnum ($i, $n =1) {//loop n a value below.
    Similar to the clock loop. Return ($i + $n) < $this->history_num?
  ($i + $n):($i + $n-$this->history_num); function Prevnum ($i, $n =1) {//loop last value I.
    Back n positions. Return ($i-$n) >=0?   
  ($i-$n): ($i-$n + $this->history_num); function minus ($i, $j) {//Clockwise two point only, I-j return ($i > $j)?
  ($i-$j):($i-$j + $this->history_num);
    The function gethistory () {//returns an array for saving or serializing operations.    Return Array (' History_num ' => $this->history_num, ' the ' a ' => $this->first, ' last '
  => $this->last, ' Back ' => $this->back, ' history ' => $this->history);
      If the function add ($path) {if ($this->back!=0) {//has a back action record, insert.
      $this->goedit ($path);
    Return
      } if ($this->history[0][' path ']== ') {//just constructed, do not add one. First move $this->history[$this->first][' path ']= $path;
    Return}else{$this->first= $this->nextnum ($this->first); First forward $this->history[$this->first][' path ']= $p      
    Ath 
    } if ($this->first== $this->last) {//Start position meets end position $this->last= $this->nextnum ($this->last);//End position forward.
    The function GoBack () {//returns the address from first back n steps.
    $this->back+=1;
    The maximum number of back steps is the difference between the starting point and the end point (the difference between the clockwise) $mins = $this->minus ($this->first, $this->last);
    if ($this->back >= $mins) {//retreat to the last point $this->back= $mins;
    $pos = $this->prevnum ($this->first, $this->back);
  return $this->history[$pos] [' Path '];
    The function GoNext () {//is one step forward from the first back n step.
    $this->back-=1;
    if ($this->back<0) {//back to the last point $this->back=0;
  return $this->history[$this->prevnum ($this->first, $this->back) [' Path ']; function Goedit ($path) {//back to a point, not forward but modified.
    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 rewind function isback () {if ($this->back < $this->minus ($this->first, $this->last)) {RET
    Urn ture;
  return false;
    }//Can forward function Isnext () {if ($this->back>0) {return true;
  return false;
}//Test code.
$hi =new History (Array (), 6);//pass in an empty array, initialize the array construct.  
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 ());//directly with array construction.
$ss->add (' asdfasdf ');
$ss->goback ();

 PR ($ss->gethistory ());?>

debug.php File:

<?php/** * 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; }}/** * format output variable, or Object * @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;p Adding: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 ();//buffer output to $out variable $out =preg_replace ('/' (. *) '/', ' <b id= ' debug_var_str ' > '. ' \\1 '. ' " </b> ', $out);//Highlight string variable $out =preg_replace ('/=\> (. *)/', ' => '. ' <b id= "Debug_str" > '. \\1 '. ' </b> ', $out);///Highlight the value following the => $out =preg_replace ('/\[(. *) \]/', ' <b id= ' debug_tag1 ' >[</b><b id= ' Debug_var ">". \\1 '. '
  </b><b id= "Debug_tag1" >]</b>, $out);//Highlight variable $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 '), or keyword highlighting $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;//is True Then exit}/** * Debug output variable, object value.
  * parameter any (variable 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

 }?>

I hope this article will help you with your PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.