【问题标题】:Store and retrieve search results存储和检索搜索结果
【发布时间】:2014-10-01 09:28:36
【问题描述】:

我有点卡在一段代码上。我有一个搜索字段,它调用一个查看数据库的 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'));
});

【问题讨论】:

  • 你可以使用jQuery和cookies来存储被点击的cookies
  • 今天早上我一直在玩 Cookies,但我无法正确使用它。我编辑了我的问题并添加了我用于 cookie 的函数。
  • alert(list.items()) 而不是alert(list.items)
  • 哦哈哈,我真是个愚蠢的错误!感谢您指出这一点

标签: javascript php jquery html zend-framework


【解决方案1】:

我已经拥有的功能没有得到解决:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-02
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 2019-08-04
    • 2022-01-24
    • 2013-07-12
    相关资源
    最近更新 更多