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程序设计有所帮助。
这个是一个action类,最后一句$this->display('Public:text');是显示public下的text模板。也就是说先执行这个类的这个方法,然后显示模板。模板用到的变量就从这里调用assign分配过去。比如$this->assign('time',time());就是把当前时间用time名字分配到模板中。else里面的html代码应该是写到模板中的。
前台就是一个多选框,然后在Controller里判断这个多选框是否有选中,用 where 语法赛选下就好了。 一般就是$this->dao->where("仅显示有货=$a")

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment