我有分页代码。它工作正常。 但我添加了搜索功能。 所以问题是,当我按下提交按钮时,它显示搜索结果与分页。 当我点击页码页面获得刷新并且网格具有所有默认值时,它将丢失搜索名称。
那么如何在整个页面安装过程中阻止搜索名称?如果我使用会话,那么我什么时候可以进入会话以及何时删除会话?
我获取所有产品的代码如下:
$data = $this->paginate('Product');
$this->set("Products",$data);
并且条件如下:
$productname = $this->data['User']['search'];
$productname = trim($productname);
$cond = array('Product.productname LIKE' => "%$productname%");
$this->set('Products', $this->paginate("Product", $cond));
我可以解决我的问题吗? 感谢
答案 0 :(得分:0)
您可以将搜索请求添加到此类
这样的参数中$this->params['named']['search'] = urlencode(json_encode($productname));
在浏览页面时,产品名称将可用(检查URL,您将看到您的请求)。然后,您可以通过查看params数组来检索所请求的产品。
if (isset($this->params['named']['search'])) {
$productname = json_decode(urldecode($this->params['named']['search']));
// ...
}
编辑:示例
// inside the index action
if (isset($this->data['User']['search'])) {
$productname = $this->data['User']['search'];
$productname = trim($productname);
$this->params['named']['search'] = urlencode(json_encode($productname));
}
$cond = array();
if (isset($this->params['named']['search'])) {
$productname = json_decode(urldecode($this->params['named']['search']));
$cond = array('Product.productname LIKE' => "%$productname%");
}
$this->set('Products', $this->paginate("Product", $cond));
答案 1 :(得分:0)
您可以使用搜索名称添加分页
在您的控制器中
<?php
$separator = array();
$urlSeparator = array();
$search_keyword = '';
$cond = array();
/* if user enter search keyword then */
if (!empty($this->request->data)) {
if (isset($this->request->data['User']['Search']) && ($this->request->data['User']['Search'] != '')) {
$search_keyword = $this->request->data['User']['Search'];
}
}
/*if search keyword is not enter then look for pagination condition params*/
elseif (!empty($this->request->params['named']['cond'])) {
$this->request->params['named']['cond'] = strtr($this->request->params['named']['cond'], '-_, ', '+/=');
$this->request->params['named']['cond'] = json_decode(base64_decode($this->request->params['named']['cond']), true);
if (isset($this->request->params['named']['cond']['search_keyword']) && $this->request->params['named']['cond']['gender'] != '') {
$search_keyword = trim($this->request->params['named']['cond']['search_keyword']);
}
}
if(!empty($search_keyword)){
$cond[] = "Product.productname LIKE '%" . $search_keyword . "'";
$separator['search_keyword'] = $search_keyword;
}
if (!empty($this->request->params)) {
if (isset($this->request->params["named"]["page"])) {
$urlSeparator[] = 'page:' . $this->request->params["named"]["page"];
}
if (isset($this->request->params["named"]["sort"])) {
$urlSeparator[] = 'sort:' . $this->request->params["named"]["sort"];
}
if (isset($this->request->params["named"]["direction"])) {
$urlSeparator[] = 'direction:' . $this->request->params["named"]["direction"];
}
}
$urlSeparator = implode("/", $urlSeparator);
$this->set('separator', $separator);
$this->set('urlSeparator', $urlSeparator);
$this->set('Products', $this->paginate("Product", $cond));
?>
并在视图文件中添加以下代码。
<?php
$this->Paginator->options(array('update' => '#div_id_for_update',
'url' => array('controller' => 'controller_name', 'action' => 'index', "cond" => strtr(base64_encode(json_encode($separator)), '+/=', '-_,')),
));
?>
请根据您的控制器名称修改上述代码。