我报告了在服务器中创建的数据库,显示了这些数据库上类似表的一些行计数器结果,使用名为Host的表来更改数据库配置。现在我试图添加一个简单的在同一视图中搜索表单以仅显示已删除的结果。但是我遇到了分页问题。
在HostController中:
<?php
class HostsController extends AppController {
public $paginate = array('limit' => 1); //just as a test
function index() {
$conditions = array();
if(!empty($_GET['host_name'])){
$asd=($_GET['host_name']);
$asd=strtoupper($asd);
$conditions = array(
'OR' => array(
'upper(Host.client_name) LIKE' => "%$asd%",
'upper(Host.contact_name) LIKE' => "%$asd%",
'upper(Host.contac_email) LIKE' => "%$asd%"));
}
$hosts = $this->Host->find('all', array('conditions'=>$conditions));
foreach ($hosts as $key => $host) {
App::import('model','Product');
$this->Product = new Product;
$this->Product->changeDataSource($host['Host']['db_name']);
if($this->changeDbSource($host['Host']['db_name'])) {
$count = $this->Product->query('SELECT count(id) as "Product__count" from products');
$hosts[$key]['Host']['count_products']= $count[0]['Product']['count'];
$options = array(
'conditions' => array(),
'order' => array(
'Product.created DESC'));
$product = $this->Product->find('first', $options);
$hosts[$key]['Host']['last_product'] = $product['Product'];
}
App::import('model','Sale');
$this->Sale = new Sale;
$this->Sale->changeDataSource($host['Host']['db_name']);
if($this->changeDbSource($host['Host']['db_name'])) {
$count = $this->Sale->query('SELECT count(id) as "Sale__count" from sales');
$hosts[$key]['Host']['count_sales']= $count[0]['Sale']['count'];
$options = array(
'conditions' => array(),
'order' => array(
'Sale.created DESC'));
$sale = $this->Sale->find('first', $options);
$hosts[$key]['Host']['last_sale'] = $sale['Sale'];
} }
$this->set(compact('hosts'));
$this->paginate ();}///<<<< Here is the problem?>
在视图中:
<form method="GET" >
<input id="clientNameSearch" type="text" name="host_name"
value="<?php echo !empty($_GET['host_name']) ?
$_GET['host_name']:"";?>" placeholder="Client, Contact or email of the contact" >
<input type="submit" value="Search">
</form>
在app控制器中(在hosts表中每行有一个数据库配置):
function changeDbSource($database = 'default') {
$db = ConnectionManager::getInstance();
$connected = $db->getDataSource($database);
if($connected->isConnected()) {return true;} else {return false;}}
所以,只使用$ this-&gt; paginate();给我主机表的整个列表(或搜索结果)并没有分页,只有按钮的结果
echo $this->Paginator->counter(array('format' => __());
视图中的似乎有效 我一直在尝试对paginate()参数进行一些修改,使用条件,定义自定义paginate()以及此处建议的其他选项,但没有正面结果,可能是因为我对此有了新的认识。
根据文档,我应该能够使用:
<?php
if (!empty($_GET['host_name'])){
$asd=($_GET['host_name']);
$asd=strtoupper($asd);
$this->paginate = array(
'conditions' => array(
'OR' => array(
'upper(Host.client_name) LIKE' => "%$asd%",
'upper(Host.contact_name) LIKE' => "%$asd%",
'upper(Host.contac_email) LIKE' => "%$asd%")
),'limit' => 2);}//just a test
if(empty($_GET['host_name'])) {$this->paginate();}?>
仍然没有结果。
所以,如果有人知道如何使其发挥作用或任何建议,我全都耳朵。 提前致谢(对不起英语和编码很差)。
因为没有安慰者......我得到了什么: 我只是将下一个代码放在foreach循环之前
if (!empty($_GET['host_name'])){
$getvar=($_GET['host_name']);
$getvar=strtoupper($getvar);
$this->paginate = array(
'conditions' => array(
'OR' => array(
'upper(Host.client_name) LIKE' => "%$getvar%",
'upper(Host.contact_name) LIKE' => "%$getvar%",
'upper(Host.contac_email) LIKE' => "%$getvar%",
'upper(Host.company_ruc) LIKE' => "%$getvar%")
),
'limit' =>2);}
$hosts=$this->paginate ();
在Index.php中:
<?php
$args = $_GET;
unset($args['url']);
$paginator->options(
array(
'url' => $this->passedArgs + array('?'=>$args)
)
);?>
它以我需要的方式显示我需要的东西,但排序选项显然不起作用..我想我不会用它
答案 0 :(得分:0)
在视图中添加搜索表单:
echo $this->Form->create('Host');
echo $this->Form->text('keywords');
echo $this->Form->submit('Search');
echo $this->Form->end();
在控制器中添加搜索表单方法:
$settings = array();
// Simple search method
if(! empty($this->request->data['Host']['keywords'])){
$keywords = explode(' ',$this->request->data['Host']['keywords']);
foreach($keywords as $key=>$keyword){
$settings['conditions']['OR'][] = array('Host.foo Like' => '%'.$keyword.'%');
$settings['conditions']['OR'][] = array('Host.bar Like' => '%'.$keyword.'%');
}
}
$this->paginate = $settings;
$this->set('hosts', $this->Paginator->paginate('Host'));
(将Host.foo和Host.bar替换为您要在主机表中搜索的列 - 如果需要,可以添加更多内容。)
标准的Cake paginator将在您的索引视图中为您服务:
<table>
<thead>
<tr>
<th><?php echo $this->Paginator->sort('name'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($hosts as $host): ?>
<tr>
<td><?php echo h($host['Host']['name']); ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
请记住,Paginator-&gt; sort(&#39;标题&#39;)必须与表格中列的名称相匹配,如果您希望在页面上显示不同的标题,则可以使用第二个 - 标题 - 论证即Paginator-&gt; sort(&#39; column_name&#39;,&#39; Title to Display&#39;);
应该让你滚动。