麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發(fā) > PHP > 正文

php基于雙向循環(huán)隊列實現(xiàn)歷史記錄的前進(jìn)后退等功能

2024-05-04 23:38:41
字體:
供稿:網(wǎng)友

這篇文章主要介紹了php基于雙向循環(huán)隊列實現(xiàn)歷史記錄的前進(jìn)后退等功能,較為詳細(xì)的分析了php使用歷史記錄功能所涉及的相關(guān)技巧與實現(xiàn)方法,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了php基于雙向循環(huán)隊列實現(xiàn)歷史記錄的前進(jìn)后退等功能。分享給大家供大家參考。具體如下:

為實現(xiàn)一個記錄操作歷史的功能

1. 和撤銷,反撤銷功能類似的一個功能。(實現(xiàn)操作的前進(jìn)后退)

2. 和discuz論壇登錄后查看帖子(可以前進(jìn)后退查看過的帖子,還有帖子查看歷史記錄)

3. 邏輯和windows資源管理器地址欄前進(jìn)后退功能一樣。

根據(jù)這種需要,實現(xiàn)了一個數(shù)據(jù)結(jié)構(gòu)。寫了一個通用的類,暫叫歷史記錄類吧。

【原理和時鐘類似。實例化對象時可以構(gòu)造長度為N(可以根據(jù)需要定長度)個節(jié)點的環(huán)】

然后整合各種操作。前進(jìn)、后退、插入、修改插入。

類可以構(gòu)造一個數(shù)組。或者傳入數(shù)組參數(shù)構(gòu)造一個對象。 每次操作之后可以取得操作后的數(shù)組。 操作完的 數(shù)據(jù)可以根據(jù)自己的需要以合適的方式保存。 放在cookie,session里面,或者序列化,或轉(zhuǎn)為json數(shù)據(jù)保存在數(shù)據(jù)庫里,或者放在文件里面都可以。 方便下一次使用。

為了便于擴(kuò)展,存放更多的數(shù)據(jù)。具體每一條數(shù)據(jù)也是一條數(shù)組記錄。

比如根據(jù)需要進(jìn)行擴(kuò)展:array('path'=>'D:/www/','sss'=>value)

順便貼出,自己寫的調(diào)試變量用的一個文件。

1. pr()可以格式化并高亮輸出變量。pr($arr),pr($arr,1)是輸出后退出。

2. debug_out() 用來輸出多個變量。默認(rèn)為退出。

3. debug_out($_GET,$_SERVER,$_POST,$arr);

history.class.php文件:

 

 
  1. <?php  
  2. include 'debug.php'
  3. /** 
  4. * 歷史記錄操作類 
  5. * 傳入或者構(gòu)造一個數(shù)組。形如: 
  6. array( 
  7. 'history_num'=>20, //隊列節(jié)點總共個數(shù) 
  8. 'first'=>0, //起始位置,從0開始。數(shù)組索引值 
  9. 'last'=>0, //終點位置,從0開始。 
  10. 'back'=>0, //從first位置倒退了多少步,差值。 
  11. 'history'=>array( //數(shù)組,存放操作隊列。 
  12. array('path'=>'D:/'), 
  13. array('path'=>'D:/www/'), 
  14. array('path'=>'E:/'), 
  15. array('path'=>'/home/') 
  16. …… 
  17. ) 
  18. ) 
  19. */ 
  20. class history{ 
  21. var $history_num; 
  22. var $first; 
  23. var $last; 
  24. var $back; 
  25. var $history=array(); 
  26. function __construct($array=array(),$num=12){ 
  27. if (!$array) {//數(shù)組為空.構(gòu)造一個循環(huán)隊列。 
  28. $history=array(); 
  29. for ($i=0; $i < $num; $i++) { 
  30. array_push($history,array('path'=>'')); 
  31. $array=array( 
  32. 'history_num'=>$num, 
  33. 'first'=>0,//起始位置 
  34. 'last'=>0,//終點位置 
  35. 'back'=>0,  
  36. '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. function nextNum($i,$n=1){//環(huán)路下n一個值。和時鐘環(huán)路類似。 
  45. return ($i+$n)<$this->history_num ? ($i+$n):($i+$n-$this->history_num); 
  46. function prevNum($i,$n=1){//環(huán)路上一個值i。回退N個位置。 
  47. return ($i-$n)>=0 ? ($i-$n) : ($i-$n+$this->history_num);  
  48. function minus($i,$j){//順時針兩點只差,i-j 
  49. return ($i > $j) ? ($i - $j):($i-$j+$this->history_num); 
  50. function getHistory(){//返回數(shù)組,用于保存或者序列化操作。 
  51. return array( 
  52. 'history_num'=> $this->history_num, 
  53. 'first' => $this->first,  
  54. 'last' => $this->last, 
  55. 'back' => $this->back,  
  56. 'history' => $this->history 
  57. ); 
  58. function add($path){ 
  59. if ($this->back!=0) {//有后退操作記錄的情況下,進(jìn)行插入。 
  60. $this->goedit($path); 
  61. return
  62. }  
  63. if ($this->history[0]['path']=='') {//剛構(gòu)造,不用加一.首位不前移 
  64. $this->history[$this->first]['path']=$path; 
  65. return
  66. }else
  67. $this->first=$this->nextNum($this->first);//首位前移 
  68. $this->history[$this->first]['path']=$path;  
  69. if ($this->first==$this->last) {//起始位置與終止位置相遇 
  70. $this->last=$this->nextNum($this->last);//末尾位置前移。 
  71. }  
  72. function goback(){//返回從first后退N步的地址。 
  73. $this->back+=1; 
  74. //最大后退步數(shù)為起點到終點之差(順時針之差) 
  75. $mins=$this->minus($this->first,$this->last); 
  76. if ($this->back >= $mins) {//退到最后點 
  77. $this->back=$mins; 
  78. $pos=$this->prevNum($this->first,$this->back); 
  79. return $this->history[$pos]['path']; 
  80. function gonext(){//從first后退N步的地方前進(jìn)一步。 
  81. $this->back-=1; 
  82. if ($this->back<0) {//退到最后點 
  83. $this->back=0; 
  84. return $this->history[$this->prevNum($this->first,$this->back)]['path']; 
  85. function goedit($path){//后退到某個點,沒有前進(jìn)而是修改。則firs值為最后的值。 
  86. $pos=$this->minus($this->first,$this->back); 
  87. $pos=$this->nextNum($pos);//下一個  
  88. $this->history[$pos]['path']=$path; 
  89. $this->first=$pos; 
  90. $this->back=0; 
  91. //是否可以后退 
  92. function isback(){ 
  93. if ($this->back < $this->minus($this->first,$this->last)) { 
  94. return ture; 
  95. return false
  96. //是否可以前進(jìn) 
  97. function isnext(){ 
  98. if ($this->back>0) { 
  99. return true
  100. return false
  101. //測試代碼。 
  102. $hi=new history(array(),6);//傳入空數(shù)組,則初始化數(shù)組構(gòu)造。 
  103. for ($i=0; $i <8; $i++) {  
  104. $hi->add('s'.$i);  
  105. pr($hi->goback()); 
  106. pr($hi->goback()); 
  107. pr($hi->goback()); 
  108. pr($hi->gonext()); 
  109. pr($hi->gonext()); 
  110. pr($hi->gonext()); 
  111. pr($hi->gonext()); 
  112. $hi->add('asdfasdf'); 
  113. $hi->add('asdfasdf2'); 
  114. pr($hi->getHistory()); 
  115. $ss=new history($hi->getHistory());//直接用數(shù)組構(gòu)造。 
  116. $ss->add('asdfasdf'); 
  117. $ss->goback(); 
  118. pr($ss->getHistory()); 
  119. ?> 

debug.php文件:

 

 
  1. <?php  
  2. /** 
  3. * 獲取變量的名字 
  4. * eg hello="123" 獲取ss字符串 
  5. */ 
  6. function get_var_name(&$aVar){ 
  7. foreach($GLOBALS as $key=>$var
  8. if($aVar==$GLOBALS[$key] && $key!="argc"){ 
  9. return $key; 
  10. /** 
  11. * 格式化輸出變量,或者對象 
  12. * @param mixed $var 
  13. * @param boolean $exit 
  14. */ 
  15. function pr($var,$exit = false){ 
  16. ob_start(); 
  17. $style='<style> 
  18. 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;} 
  19. pre#debug b{font-weight:400;} 
  20. #debug #debug_str{color:#E75B22;} 
  21. #debug #debug_keywords{font-weight:800;color:00f;} 
  22. #debug #debug_tag1{color:#22f;} 
  23. #debug #debug_tag2{color:#f33;font-weight:800;} 
  24. #debug #debug_var{color:#33f;} 
  25. #debug #debug_var_str{color:#f00;} 
  26. #debug #debug_set{color:#0C9CAE;}</style>'; 
  27. if (is_array($var)){ 
  28. print_r($var); 
  29. else if(is_object($var)){ 
  30. echo get_class($var)." Object"
  31. else if(is_resource($var)){ 
  32. echo (string)$var
  33. else
  34. echo var_dump($var); 
  35. }  
  36. $out = ob_get_clean();//緩沖輸出給$out 變量 
  37. $out=preg_replace('/"(.*)"/','<b id="debug_var_str">"'.'//1'.'"</b>',$out);//高亮字符串變量 
  38. $out=preg_replace('/=/>(.*)/','=>'.'<b id="debug_str">'.'//1'.'</b>',$out);//高亮=>后面的值 
  39. $out=preg_replace('//[(.*)/]/','<b id="debug_tag1">[</b><b id="debug_var">'.'//1'.'</b><b id="debug_tag1">]</b>',$out);//高亮變量 
  40. $from = array(' ','(',')','=>'); 
  41. $to = array(' ','<b id="debug_tag2">(</i>','<b id="debug_tag2">)</b>','<b id="debug_set">=></b>'); 
  42. $out=str_replace($from,$to,$out);  
  43. $keywords=array('Array','int','string','class','object','null');//關(guān)鍵字高亮 
  44. $keywords_to=$keywords; 
  45. foreach($keywords as $key=>$val) 
  46. {  
  47. $keywords_to[$key] = '<b id="debug_keywords">'.$val.'</b>'
  48. $out=str_replace($keywords,$keywords_to,$out);  
  49. echo $style.'<pre id="debug"><b id="debug_keywords">'.get_var_name($var).'</b> = '.$out.'</pre>'
  50. if ($exit) exit;//為真則退出 
  51. /** 
  52. * 調(diào)試輸出變量,對象的值。 
  53. * 參數(shù)任意個(任意類型的變量) 
  54. * @return echo 
  55. */ 
  56. function debug_out(){ 
  57. $avg_num = func_num_args(); 
  58. $avg_list= func_get_args(); 
  59. ob_start(); 
  60. for($i=0; $i < $avg_num; $i++) { 
  61. pr($avg_list[$i]); 
  62. $out=ob_get_clean(); 
  63. echo $out; 
  64. exit; 
  65. ?> 

希望本文所述對大家的php程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 免费三级大片 | 毛片视频网址 | 91精品国 | 精品国产91久久久久久久妲己 | 一级在线观看 | 特级毛片a级毛片100免费 | 欧美日性 | 精品1| 狠狠操电影 | 草莓视频在线导航 | 日韩欧美中文字幕视频 | 久久精品黄 | 成品片a免人视频 | 青青草成人免费视频在线 | 一级电影在线观看 | 国产又白又嫩又紧又爽18p | 九九热精品在线 | 第一区免费在线观看 | 午夜生活理论片 | 秋霞a级毛片在线看 | 国产一级做a爰片在线看 | 美国av免费看 | 欧美成人久久 | 免费的性生活视频 | 中文字幕免费在线看 | 日韩在线毛片 | 天天躁狠狠躁夜躁2020挡不住 | 又黄又爽又色无遮挡免费 | 日韩精品中文字幕在线播放 | 日韩一级免费毛片 | 日韩a毛片免费观看 | 免费一级a毛片免费观看 | 欧美性受xxx黑人xyx性爽 | 三人弄娇妻高潮3p视频 | 91久久国产露脸精品免费 | 久久国产精品免费视频 | 久久久亚洲欧美综合 | 亚洲欧美国产高清va在线播放 | 91九色精品 | 一级毛片电影院 | 青草伊人网 |