To enable the record operation history function
- This function is similar to the undo or undo function. (Implement forward and backward operations)
- View the post after logging on to the discuz Forum (you can view the post you have viewed before and after, and view the history of the post)
- The logic is the same as that in the address bar of windows resource manager.
According to this need, a data structure is implemented. I wrote a general class, which is called the history class. [The principle is similar to the clock. When instantiating an object, you can construct a ring with a length of N (you can set the length as needed) nodes] Then integrate various operations. Forward, backward, insert, modify insert. Class can construct an array. Or input an array parameter to construct an object. After each operation, you can obtain the array after the operation.The operated data can be saved as needed. It can be stored in cookies, sessions, or serialized, or converted into json data in the database, or in files. Easy to use next time. To facilitate expansion and store more data. Each piece of data is also an array record. For example, extend the value as needed: array ('path' => 'd:/www/', 'SS' => value) ---------------------------------------------------------------------------- By the way, a file is used for debugging variables written by myself.
- Pr () can be formatted and highlighted. Pr ($ arr), pr ($ arr, 1) is output and then exited.
- Debug_out () is used to output multiple variables. Exit by default.
- Debug_out ($ _ GET, $ _ SERVER, $ _ POST, $ arr );
-
- Include 'debug. php ';
- /**
- * History operation
- * Input or construct an array. Shape:
- Array (
- 'History _ num' => 20, // total number of queue nodes
- 'First' => 0, // start position, starting from 0. Array index value
- 'Last' => 0, // The end point, starting from 0.
- 'Back' => 0, // The number of steps from the first position, difference value.
- 'History '=> array (// array, which stores operation queues.
- 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 cyclic 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 point
- '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) {// A value of n under the loop. Similar to clock loops.
- Return ($ I + $ n) <$ this-> history_num? ($ I + $ n) :( $ I + $ n-$ this-> history_num );
- }
- Function prevNum ($ I, $ n = 1) {// The value I on the loop. Return N locations.
- Return ($ I-$ n)> = 0? ($ I-$ n): ($ I-$ n + $ this-> history_num );
- }
- Function minus ($ I, $ j) {// only two points clockwise, I-j
- Return ($ I> $ j )? ($ I-$ j) :( $ I-$ j + $ this-> history_num );
- }
- Function getHistory () {// returns an array for saving or serializing 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) {// Insert if there is a rollback operation record.
- $ This-> goedit ($ path );
- Return;
- }
- If ($ this-> history [0] ['path'] = '') {// just constructed, do not add one. do not move forward first
- $ This-> history [$ this-> first] ['path'] = $ path;
- Return;
- } Else {
- $ This-> first = $ this-> nextNum ($ this-> first); // move the first
- $ This-> history [$ this-> first] ['path'] = $ path;
- }
- If ($ this-> first ==$ this-> last) {// The starting position and ending position
- $ This-> last = $ this-> nextNum ($ this-> last); // move the end forward.
- }
- }
- Function goback () {// return the N-step back from first.
- $ This-> back + = 1;
- // The maximum number of backward steps is the difference from the start point to the end point (clockwise)
- $ Mins = $ this-> minus ($ this-> first, $ this-> last );
- If ($ this-> back >=$ mins) {// return to the last vertex
- $ This-> back = $ mins;
- }
- $ Pos = $ this-> prevNum ($ this-> first, $ this-> back );
- Return $ this-> history [$ pos] ['path'];
- }
- Function gonext () {// step N after the first step.
- $ This-> back-= 1;
- If ($ this-> back <0) {// return to the last vertex
- $ This-> back = 0;
- }
- Return $ this-> history [$ this-> prevNum ($ this-> first, $ this-> back)] ['path'];
- }
- Function goedit ($ path) {// move back to a certain point. instead of moving forward, it is modified. The firs value is the final value.
- $ Pos = $ this-> minus ($ this-> first, $ this-> back );
- $ Pos = $ this-> nextNum ($ pos); // Next
- $ This-> history [$ pos] ['path'] = $ path;
- $ This-> first = $ pos;
- $ This-> back = 0;
- }
- // Whether the backend is allowed
- Function isback (){
- If ($ this-> back <$ this-> minus ($ this-> first, $ this-> last )){
- Return ture;
- }
- Return false;
- }
- // Whether it can be moved forward
- Function isnext (){
- If ($ this-> back> 0 ){
- Return true;
- }
- Return false;
- }
- }
- // Test the code.
- $ Hi = new history (array (), 6); // input an empty array to initialize the array structure.
- 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 (); // you can directly construct it using arrays.
- $ Ss-> add ('asdfasdf ');
- $ Ss-> goback ();
- Pr ($ ss-> getHistory ());
- ?>
-
- /**
- * Get the variable name
- * Eg hello = "123" get ss string
- */
- Function get_var_name (& $ aVar ){
- Foreach ($ GLOBALS as $ key => $ var)
- {
- If ($ aVar ==$ GLOBALS [$ key] & $ key! = "Argc "){
- Return $ key;
- }
- }
- }
- /**
- * Format the output variable or object
- * @ Param mixed $ var
- * @ Param boolean $ exit
- */
- Function pr ($ var, $ exit = false ){
- Ob_start ();
- $ 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 the output to the $ out variable
- $ Out = preg_replace ('/"(.*)"/','"'.' \ 1 '.'"', $ Out); // highlight string variables
- $ Out = preg_replace ('/=\> (. *)/', '=> '.''.' \ 1 '.'', $ Out); // The value after Highlight =>
- $ Out = preg_replace ('/\ [(. *) \]/','['.' \ 1 '.']', $ Out); // highlight the 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 .'
'.get_var_name($var).' = '.$out.' ';
- If ($ exit) exit; // exit if it is true.
- }
- /**
- * Debug the output variable, the object value.
- * Any number of parameters (any type of variables)
- * @ 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;
- }
- ?>
|