PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 13, 2022

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

 February 13, 2022     cakephp, cakephp-3.0, php     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing