php 实现历史记录前进、后退等功能

前端之家收集整理的这篇文章主要介绍了php 实现历史记录前进、后退等功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:
  1. /**
  2. * 历史记录操作类
  3. *
  4. * @param
  5. * @author 编程之家 jb51.cc jb51.cc
  6. **/
  7. include 'debug.PHP';
  8. /**
  9. * 传入或者构造一个数组。形如:
  10. array(
  11. 'history_num'=>20,//队列节点总共个数
  12. 'first'=>0,//起始位置,从0开始。数组索引值
  13. 'last'=>0,//终点位置,从0开始。
  14. 'back'=>0,//从first位置倒退了多少步,差值。
  15. 'history'=>array( //数组,存放操作队列。
  16. array('path'=>'D:/'),array('path'=>'D:/www/'),array('path'=>'E:/'),array('path'=>'/home/')
  17. ……
  18. )
  19. )
  20. */
  21. class history{
  22. var $history_num;
  23. var $first;
  24. var $last;
  25. var $back;
  26. var $history=array();
  27. function __construct($array=array(),$num=12){
  28. if (!$array) {//数组为空.构造一个循环队列。
  29. $history=array();
  30. for ($i=0; $i < $num; $i++) {
  31. array_push($history,array('path'=>''));
  32. }
  33. $array=array(
  34. 'history_num'=>$num,'first'=>0,//起始位置
  35. 'last'=>0,//终点位置
  36. 'back'=>0,'history'=>$history
  37. );
  38. }
  39. $this->history_num=$array['history_num'];
  40. $this->first=$array['first'];
  41. $this->last=$array['last'];
  42. $this->back=$array['back'];
  43. $this->history=$array['history'];
  44. }
  45. function nextNum($i,$n=1){//环路下n一个值。和时钟环路类似。
  46. return ($i+$n)<$this->history_num ? ($i+$n):($i+$n-$this->history_num);
  47. }
  48. function prevNum($i,$n=1){//环路上一个值i。回退N个位置。
  49. return ($i-$n)>=0 ? ($i-$n) : ($i-$n+$this->history_num);
  50. }
  51. function minus($i,$j){//顺时针两点只差,i-j
  52. return ($i > $j) ? ($i - $j):($i-$j+$this->history_num);
  53. }
  54. function getHistory(){//返回数组,用于保存或者序列化操作。
  55. return array(
  56. 'history_num'=> $this->history_num,'first' => $this->first,'last' => $this->last,'back' => $this->back,'history' => $this->history
  57. );
  58. }
  59. function add($path){
  60. if ($this->back!=0) {//有后退操作记录的情况下,进行插入。
  61. $this->goedit($path);
  62. return;
  63. }
  64. if ($this->history[0]['path']=='') {//刚构造,不用加一.首位不前移
  65. $this->history[$this->first]['path']=$path;
  66. return;
  67. }else{
  68. $this->first=$this->nextNum($this->first);//首位前移
  69. $this->history[$this->first]['path']=$path;
  70. }
  71. if ($this->first==$this->last) {//起始位置与终止位置相遇
  72. $this->last=$this->nextNum($this->last);//末尾位置前移。
  73. }
  74. }
  75. function goback(){//返回从first后退N步的地址。
  76. $this->back+=1;
  77. //最大后退步数为起点到终点之差(顺时针之差)
  78. $mins=$this->minus($this->first,$this->last);
  79. if ($this->back >= $mins) {//退到最后点
  80. $this->back=$mins;
  81. }
  82. $pos=$this->prevNum($this->first,$this->back);
  83. return $this->history[$pos]['path'];
  84. }
  85. function gonext(){//从first后退N步的地方前进一步。
  86. $this->back-=1;
  87. if ($this->back<0) {//退到最后点
  88. $this->back=0;
  89. }
  90. return $this->history[$this->prevNum($this->first,$this->back)]['path'];
  91. }
  92. function goedit($path){//后退到某个点,没有前进而是修改。则firs值为最后的值。
  93. $pos=$this->minus($this->first,$this->back);
  94. $pos=$this->nextNum($pos);//下一个
  95. $this->history[$pos]['path']=$path;
  96. $this->first=$pos;
  97. $this->back=0;
  98. }
  99. //是否可以后退
  100. function isback(){
  101. if ($this->back < $this->minus($this->first,$this->last)) {
  102. return ture;
  103. }
  104. return false;
  105. }
  106. //是否可以前进
  107. function isnext(){
  108. if ($this->back>0) {
  109. return true;
  110. }
  111. return false;
  112. }
  113. }
  114. //测试
  115. /*** 代码来自编程之家 jb51.cc(jb51.cc) ***/

猜你在找的PHP相关文章