2014-10-01 206 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){ 
    .... 
}); 

也许这将帮助别人的某个时候..