0

我有以下搜索表格

<?php
echo $this->Form->create('Order', array('action' => 'search','type'=>'get'));?>
<?php echo $this->Form->input('SearchTerm',array('label' => 'Find:')); ?>
<?php $options=array('full_name'=>'By Name','code'=>'By Code','phone'=>'By Phone','email'=>'By Mail'); ?>
<?php echo $this->Form->input('options', array('type'=>'select', 'label'=>'Search:', 'options'=>$options)); ?>

<?php echo $this->Form->end('search'); ?>  

<?php if($rs!=0){?>
<?php if($rs!=null) { ?>
    results found : <?php print $results; //print_r ($result['Order']['full_name']); ?>
<h1 class="ico_mug">Results Matching Term: <?php print '"'.$term.'"' ;?></h1>
<table id="table">
    <tr>

        <th>Client Name</th>
        <th>Order Code</th>
        <th>Phone</th>
        <th>Received Order</th>
        <th>Working On Order </th>
        <th>Order Ready </th>
    </tr>

    <!-- Here is where we loop through our $posts array, printing out post info -->

    <?php foreach ($rs as $order): ?>
    <tr>

        <td>
            <?php echo $this->Html->link($order['Order']['full_name'],
            array('controller' => 'orders', 'action' => 'view', $order['Order']['id'])); ?>
        </td>
        <td><?php echo $order['Order']['code']; ?></td>
        <td><?php echo $order['Order']['phone']; ?></td>
        <td><?php echo $this->OrderStatus->getStatusString($order['Order']['receive_state']); ?></td>
        <td><?php echo $this->OrderStatus->getStatusString($order['Order']['working_state']); ?></td>
        <td><?php echo $this->OrderStatus->getStatusString($order['Order']['ready_state']); ?></td>
        <td> <?php //echo $this->Html->link($this->Html->image("cancel.jpg"), array('action' => 'index'), array('escape' => false));?><?php echo $this->Html->link($this->Html->image("edit.jpg"), array('action' => 'edit',$order['Order']['id']), array('escape' => false));/*echo $this->Html->link('Edit', array('action' => 'edit', $order['Order']['id']));*/?></td>
    </tr>
    <?php endforeach; ?>
    <tr ><?php //echo $this->Paginator->numbers(array('first' => 'First page')); ?></tr>
    <?php
    $urlParams = $this->params['url'];
    unset($urlParams['url']);
    $optionss=array('url' => array('?' => http_build_query($urlParams)));
    //$this->Paginator->options($optionss);
    $this->Paginator->settings['paramType'] = 'querystring';

    ?>

    <tr ><?php  //echo" << ".$this->Paginator->counter(
       // 'Page {:page} of {:pages}');
        ?></tr>

</table>

 <?php }else echo"no results found"; ?>


<?php } //endif?>

这是我的控制器动作

function search($options=null){
        $this->set('results',"");
        $this->set('term',"");
        $this->set('rs',0);    

        if ((isset($_GET["SearchTerm"]))||(isset($_GET["options"])) ) {
            $SearchTerm=$_GET["SearchTerm"];
            $options=$_GET["options"];

            if (!$options || !$SearchTerm) {
                $this->Session->setFlash('Please enter something to search for');
            }
            else {
                $SearchArray = array($options." LIKE " => "%".$SearchTerm."%");

                $this->paginate = array('conditions' =>  $SearchArray,'limit'=>2,'convertKeys' => array($options, $SearchTerm));
                $data=$this->paginate('Order');
                $this->set('rs', $data);
                $this->set('term',$SearchTerm);

            }
        }

如您所见,我正在使用获取参数选项和 SearchTerm。我希望这两个留在分页链接中。我尝试了在 stackoverflow 和其他站点上找到的各种修复程序(例如:

$urlParams = $this->params['url'];
    unset($urlParams['url']);
    $optionss=array('url' => array('?' => http_build_query($urlParams)));

但我仍然收到以下错误消息:

间接修改重载属性 PaginatorHelper::$settings 无效 [APP\View\orders\search.ctp, line 75

这是为什么?那么解决方案呢?(即使我使用食谱中的 wxample

$this->Paginator->settings['paramType'] = 'querystring';

我仍然遇到同样的错误:S 你能帮忙/解释一下吗?

4

1 回答 1

2

如果我正确理解您的代码,您应该能够替换:

<?php
    $urlParams = $this->params['url'];
    unset($urlParams['url']);
    $optionss=array('url' => array('?' => http_build_query($urlParams)));
    //$this->Paginator->options($optionss);
    $this->Paginator->settings['paramType'] = 'querystring';
?>

<?php $this->Paginator->options(array('url' => $this->passedArgs)); ?>

分页中的表单域 如果您实际上是想将表单元素放入分页中,则需要稍微更改代码。您将需要为搜索选项中的每个项目构建命名参数。然后,在控制器的操作中,您将需要检查 request->data 和 params['named'] 版本的搜索,以确保在这两种情况下都使用它们。我还会清理代码,使其更具可读性。

首先,您需要使用 cake 方法来获取数据。它将确保它为您清理请求等。此外,您需要考虑作为命名变量传递并以表单形式提交的搜索词和过滤器选项。所以你需要更新你的控制器如下:

function search($options=null){

    $this->set('results',""); // where is this used ??
    $this->set('rs', 0); 
    $this->set('SearchTerm', '');
    $this->set('FilterBy', '');   
    $this->set('options', array('full_name'=>'By Name','code'=>'By Code','phone'=>'By Phone','email'=>'By Mail'));

    if ($SearchTerm = $this->request->data['Order']['SearchTerm']) or $SearchTerm = $this->params['named']['SearchTerm']) {
        if ($FilterBy = $this->request->data['Order']['FilterBy'] or $FilterBy = $this->params['named']['FilterBy'])) {
            $this->paginate = array(
                'conditions' => array($FilterBy." LIKE " => "%".$SearchTerm."%"), 
                'limit' => 2, 
                'convertKeys' => array($FilterBy, $SearchTerm)
            );
            $this->set('rs', $this->paginate('Order'));
            $this->set('SearchTerm', $SearchTerm);
            $this->set('FilterBy', $FilterBy);
        } else {
            $this->Session->setFlash('Please enter something to search for');
        }
    } else {
        $this->Session->setFlash('Please enter something to search for');
    }
}

接下来,我们关注视图。删除移动这个:

$options=array('full_name'=>'By Name','code'=>'By Code','phone'=>'By Phone','email'=>'By Mail');

它不应该出现在视图中。它属于控制器(它现在所在的位置)。

接下来,清理表单:

<?php
  echo $this->Form->create('Order', array('action' => 'search','type'=>'get'));
  echo $this->Form->input('SearchTerm',array('label' => 'Find:'));
  echo $this->Form->input('FilterBy', array('type'=>'select', 'label'=>'Search:', 'options'=>$options));
  echo $this->Form->end('search'); 
?>  

*请注意,我将options名称更改为,FilterBy以便在控制器中更容易找到。还有其他可以在视图中清理的东西。但是,我只会解决与问题相对应的问题。

现在您需要替换此代码:

<tr ><?php //echo $this->Paginator->numbers(array('first' => 'First page')); ?></tr>
    <?php
    $urlParams = $this->params['url'];
    unset($urlParams['url']);
    $optionss=array('url' => array('?' => http_build_query($urlParams)));
    //$this->Paginator->options($optionss);
    $this->Paginator->settings['paramType'] = 'querystring';

    ?>

    <tr ><?php  //echo" << ".$this->Paginator->counter(
       // 'Page {:page} of {:pages}');
        ?></tr>

</table>

使用此代码:

</table>
<p>
<?php
  $this->Paginator->options(array('url' => array_merge(array('SearchString' => $SearchString, 'FilterBy' => $FilterBy), $this->passedArgs)));

echo $this->Paginator->counter(array(
'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%')
));
?>  
</p>

<div class="paging">
    <?php echo $this->Paginator->prev('<< ' . __('previous'), array(), null, array('class'=>'disabled'));?>
 |  <?php echo $this->Paginator->numbers();?>
 |  <?php echo $this->Paginator->next(__('next') . ' >>', array(), null, array('class' => 'disabled'));?>
</div>

您当然可以采用不同的格式来满足您的需求。但是代码应该与应用于分页的搜索词和过滤器选项一样工作。

祝你好运,编码快乐!

于 2012-02-10T05:47:21.213 回答