搜索功能在CakePHP中的索引处显示结果

时间:2014-11-18 16:29:06

标签: cakephp cakephp-2.0

如何在索引视图中从搜索功能显示查询结果?显然,搜索工作正常,默认蛋糕底部区域显示的内容。     选择Moviemovie_idMovietitleMoviedateMoviedescription FROM { {1}}。cTrader AS movies WHERE MovieMovie< = 20141118。 所以,我想知道如何根据index.ctp中的日期搜索显示我的所有帖子(电影) 提前谢谢,安德鲁

观点:

date

控制器

<h1>Movies</h1>
<?php echo $this->Form->create('Movie', array('type' => 'get', 'action' => 'search'));?>
<?php echo $this->Form->input('date', array(
    'type'  => 'date',
    'label' => 'Date',
    'empty' => false,
    'dateFormat' => 'DMY',
    'minYear'=>'1990',
    'maxYear'=>date('Y'),
     ));  ?>

 <?php echo $this->Form->end(__('Submit')); ?> 
<table>
    <tr>
        <th>Title</th>
        <th>Year</th>
        <th>Description</th>
   </tr>
<?php foreach ($movies as $movie): ?>
    <tr>
        <td><?php echo $movie['Movie']['title'];?></td>
        <td><?php echo $movie['Movie']['date'];?></td>
        <td><?php echo $movie['Movie']['description'];?></td>
    </tr>
<?php endforeach; ?>
<?php unset($movie); ?>
</table>
<ul>
   <li><?php echo $this->Html->link(__('Insert a new movie'), array('controller' => 'movies', 'action' => 'add')); ?> </li>
</ul>

1 个答案:

答案 0 :(得分:0)

将表单更改为以下

<h1>Movies</h1>
<?php echo $this->Form->create('Movie', array('type' => 'get', 'action' => 'index'));?>
<?php echo $this->Form->input('date', array(
    'type'  => 'date',
    'label' => 'Date',
    'empty' => false,
    'dateFormat' => 'DMY',
    'minYear'=>'1990',
    'maxYear'=>date('Y'),
     ));  ?>

 <?php echo $this->Form->end(__('Submit')); ?> 
<table>
    <tr>
        <th>Title</th>
        <th>Year</th>
        <th>Description</th>
   </tr>
<?php foreach ($movies as $movie): ?>
    <tr>
        <td><?php echo $movie['Movie']['title'];?></td>
        <td><?php echo $movie['Movie']['date'];?></td>
        <td><?php echo $movie['Movie']['description'];?></td>
    </tr>
<?php endforeach; ?>
<?php unset($movie); ?>
</table>
<ul>
   <li><?php echo $this->Html->link(__('Insert a new movie'), array('controller' => 'movies', 'action' => 'add')); ?> </li>
</ul>

你的控制器要跟随。

<?php
   class MoviesController extends AppController {

  public $helpers = array('Html', 'Form');

  public function index() {
     if(isset($this->request->query['date']){
             $result = $this->request->query['date']['year'] . $this->request->query['date']['month'] .     $this->request->query['date']['day'];
              $searchdate = array(
             'Movie.date <=' => $result,
          );
         $this->set('movies', $this->Movie->find('all', array('conditions' => $searchdate )));
         $this->render = 'search';
     }
     else{
         $this->set('movies', $this->Movie->find('all'));
     }
  }

&GT;