1
我有点卡住了一段代码。我有调用PHP函数,看起来到数据库中搜索字段:存储和检索搜索结果
$this->_helper->layout->disableLayout();
$q = $this->getRequest()->getParam('q');
$product_db = new Products();
$this->view->products = $product_db->searchProduct($q);
foreach($this->view->products as $product)
{
....
}
这样做的结果得到通过JQuery加载到我的HTML页面:
var searchItem = function() {
if($('#json_search').val())
{
$('#search-resultlist').load('/search/quick/q/'+ $('#json_search').val());
}
else {
$('#search-resultlist').html('');
}
}
HTML:
<div class="right-searchcontent" >
<input type="text" class="form-control" placeholder="Search ... " id="json_search">
</div>
<div class="right-searchresult">
<ul id="search-resultlist" >
<li >
</li>
</ul>
</div>
现在基本上我试图实现的是创建一个'搜索历史'。 起初,我尝试了一个SESSION阵列在我搜索控制器:
if(!isset($_SESSION['history'])) {
$_SESSION['history'] = array();
}
在我的函数来显示数据库的搜索结果:
if(!empty($product)){
if(!in_array($product->naam, $_SESSION['history']))
{
$_SESSION['history'][] = $product->naam;
}
}
但这是存储所有我所搜索的值因为(如:'sk','ah') 我只想要我实际点击的值。
任何人都可以指向正确的方向吗?
我一直在试图用localStorage实现我的结果,但这不会给我正确的解决方案。此外,我尝试使用此功能的饼干:
var cookieList = function(cookieName) {
var cookie = $.cookie(cookieName);
var items = cookie ? cookie.split(/,/) : new Array();
return {
"add": function(val) {
//Add to the items.
items.push(val);
//Save the items to a cookie.
$.cookie(cookieName, items.join(','));
},
"remove": function (val) {
indx = items.indexOf(val);
if(indx!=-1) items.splice(indx, 1);
$.cookie(cookieName, items.join(',')); },
"clear": function() {
items = null;
//clear the cookie.
$.cookie(cookieName, null);
},
"items": function() {
return items;
}
}
}
但是,当我试图提醒list.items它只是返回给我的整个方法。
我能实现产品的名称,当我点击它,但我不知道我怎么能这样存储到会话或别的什么,我可以实现任何时间任何页面上..
你可以使用jQuery和Cookie存储被点击在cookie中的那些 – 2014-10-01 09:30:23
今天早上我一直在玩Cookies,但是我无法以正确的方式得到它。我编辑了我的问题,并添加了用于Cookie的功能。 – Matheno 2014-10-01 09:30:54
'alert(list.items())'而不是'alert(list.items)' – 2014-10-01 11:57:54