存储和检索搜索结果

时间:2014-10-01 09:28:36

标签: javascript php jquery html zend-framework

我对一段代码感到有些困惑。我有一个搜索字段,它调用一个查看数据库的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来实现我的结果,但这不会给我正确的解决方案。我也尝试使用带有此功能的Cookie:

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时,它只返回了整个方法。

当我点击它时,我可以获得产品名称,但我只是不知道如何将它存储到SESSION或其他任何我可以在任何页面上实现的其他内容。

$(document).on('click', 'a.searchItem', function(e){
  e.preventDefault();
  var search = $(this).attr('value'));
});

1 个答案:

答案 0 :(得分:0)

我已经修复了我已经拥有的功能:

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){
  ....
});

也许这会在某个时候帮助别人..