thinkphp浏览历史功能实现方法,thinkphp历史_PHP教程

WBOY
Freigeben: 2016-07-13 10:15:41
Original
837 Leute haben es durchsucht

thinkphp浏览历史功能实现方法,thinkphp历史

本文实例讲述了thinkphp浏览历史功能实现方法,分享给大家供大家参考。具体实现方法分析如下:

历史浏览功能都是使用了cookie功能记录用户信息放到了本地了,这样我们只要读取存储在cookies中的值就可以了,下面来给大家介绍一个基于thinkphp 实现浏览历史功能例子。

就像浏览器一样,能够记录访问了哪些页面,这样能够减少时间,下面我们实现浏览历史的功能。

1.在你需要记录浏览数据的产品或新闻页面,记录cookie需要保存的信息,例如下面这行代码,把页面ID,产品名称,价格,缩略图,网址传给cookie_history。

复制代码 代码如下:
cookie_history($id,$info['title'],$info['price'],$info['pic'],$thisurl);

2.function.php 里面添加代码

复制代码 代码如下:
/**
  +----------------------------------------------------------
 * 浏览记录按照时间排序
  +----------------------------------------------------------
 */
function my_sort($a, $b){
$a = substr($a,1);
$b = substr($b,1);
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
  }
/**
  +----------------------------------------------------------
 * 网页浏览记录生成
  +----------------------------------------------------------
 */
function cookie_history($id,$title,$price,$img,$url){
$dealinfo['title'] = $title;
$dealinfo['price'] = $price;
$dealinfo['img'] = $img;
$dealinfo['url'] = $url;
$time = 't'.NOW_TIME;
$cookie_history = array($time => json_encode($dealinfo));  //设置cookie
if (!cookie('history')){//cookie空,初始一个
cookie('history',$cookie_history);
}else{
$new_history = array_merge(cookie('history'),$cookie_history);//添加新浏览数据
uksort($new_history, "my_sort");//按照浏览时间排序
$history = array_unique($new_history);
if (count($history) > 4){
$history = array_slice($history,0,4);
}
cookie('history',$history);
}
}
/**
  +----------------------------------------------------------
 * 网页浏览记录读取
  +----------------------------------------------------------
 */
function cookie_history_read(){
$arr = cookie('history');
foreach ((array)$arr as $k => $v){
$list[$k] = json_decode($v,true);
}
return $list;
}

3.在需要显示浏览记录的页面输出信息

复制代码 代码如下:
$this->assign('history',cookie_history_read());

模板里面用volist显示出来就行了。

希望本文所述对大家的PHP程序设计有所帮助。

thinkphp实现最基本的功可以

这个是一个action类,最后一句$this->display('Public:text');是显示public下的text模板。也就是说先执行这个类的这个方法,然后显示模板。模板用到的变量就从这里调用assign分配过去。比如$this->assign('time',time());就是把当前时间用time名字分配到模板中。else里面的html代码应该是写到模板中的。
 

用thinkphp实现这种功可以怎实现?

前台就是一个多选框,然后在Controller里判断这个多选框是否有选中,用 where 语法赛选下就好了。 一般就是$this->dao->where("仅显示有货=$a")
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/902779.htmlTechArticlethinkphp浏览历史功能实现方法,thinkphp历史 本文实例讲述了thinkphp浏览历史功能实现方法,分享给大家供大家参考。具体实现方法分析如下:...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!