我报告了在服务器中创建的数据库,显示了这些数据库上类似表的一些行计数器结果,并使用名为Host的表来更改数据库配置。。现在我尝试在同一视图中添加一个简单的搜索表单,只显示macthed结果。但我在分页方面有问题。
在主机控制器中:
<?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>
在应用程序控制器中(主机表中每行有一个数据库配置):
function changeDbSource($database = 'default') {
$db = ConnectionManager::getInstance();
$connected = $db->getDataSource($database);
if($connected->isConnected()) {return true;} else {return false;}}
因此,只使用$this->paginate();给我主机表的完整列表(或搜索结果),不要分页,只显示按钮结果的leyend
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)
)
);?>
它以我需要的方式显示了我需要什么,但排序选项显然不起作用。。我想我不会用它
在视图中添加搜索表单:
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替换为要在hosts表中搜索的列-如果需要,可以添加更多)。
标准的Cake分页器将在您的索引视图中为您工作:
<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>
请记住,分页器->排序('heading')必须与表中列的名称相匹配,如果您希望在页面上显示不同的标题,则可以使用第二个-title-参数,即分页器->sort('column_name','title to Display');
应该会让你动起来的。