Sunday, February 13, 2022

[FIXED] how to show data on select from a dropdown in index page in cakephp 3

Issue

In the index page I have a select option. If I select a user from drop down it will show only that user's article list. In database I have two table called "articles" and "profiles". Now how can I show data on click from drop down in cakephp version 3? Index Page


Solution

I have tried the following code to my project

<!-- 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>


Answered By - Mustafa

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.