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