2014-10-01 159 views
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它只是返回給我的整個方法。

我能實現產品的名稱,當我點擊它,但我不知道我怎麼能這樣存儲到會話或別的什麼,我可以實現任何時間任何頁面上..

+0

你可以使用jQuery和Cookie存儲被點擊在cookie中的那些 – 2014-10-01 09:30:23

+0

今天早上我一直在玩Cookies,但是我無法以正確的方式得到它。我編輯了我的問題,並添加了用於Cookie的功能。 – Matheno 2014-10-01 09:30:54

+0

'alert(list.items())'而不是'alert(list.items)' – 2014-10-01 11:57:54

回答

0

I'ts得到修復與功能我已經有:

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(',')); 
     }, 
     "contain": function (val) { 
     //Check if an item is there. 
     if (!Array.prototype.indexOf) { 
      Array.prototype.indexOf = function(obj, start) { 
       for (var i = (start || 0), j = this.length; i < j; i++) { 
        if (this[i] === obj) { return i; } 
       } 
       return -1; 
      }; 
     } 
     var indx = items.join(',').indexOf(val); 
     if(indx > -1){ 
      return true; 
     }else{ 
      return false; 
     }             }, 
     "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; 
     } 
    } 
} 

當用戶點擊一個搜索結果它添加到Cookie列表:

$(document).on('click', 'a.searchItem', function(e){ 
    var search = $(this).attr('value'); 
    var list = new cookieList("MyItems"); 
    if(!list.contain(search)){ 
    list.add(search); 
    } 
}); 

開放時,在搜索框中我顯示列表的結果:

jQuery.each(list.items(), function(index, value){ 
    .... 
}); 

也許這將幫助別人的某個時候..