如何在cakephp 3中的索引页面的下拉列表中显示数据

时间:2017-03-20 05:32:43

标签: php cakephp cakephp-3.0

在索引页面中,我有一个选择选项。如果我从下拉列表中选择一个用户,它将只显示该用户的文章列表。在数据库中,我有两个名为“articles”和“profiles”的表。现在我怎样才能在cakephp版本3的下拉列表中显示点击数据? Index Page

1 个答案:

答案 0 :(得分:1)

我在我的项目中尝试了以下代码

<!-- controller -->
    <?php 
    public function index($user_id=null){
            if($this->request->is(['post'])){
                return $this->redirect(['action'=>'index', $this->request->data['user_id']]);
            }
            $this->loadModel('Users');
            $users = $this->Users->find('list');
            $articles = $this->Articles->find('all', ['conditions' => ['Articles.user_id'=>$user_id,]]);
            $this->set(compact('user_id', 'articles', 'users'));        
        }
     ?>

<!-- view[index.ctp] -->
 <?php echo $this->Form->create(null); ?>
<?php echo $this->Form->input('user_id', ['empty'=>'Select', 'options'=>$users, 'value'=>$user_id]); ?>
<?php echo $this->Form->button(__('Go'), ['class'=>'btn-success']); ?>
<?php $this->end(); ?>
<div>
<?php
foreach($articles as $article){
    echo $article['title'].'<br>';
}
?>
</div>