Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Fetch the data from the database and display the fetched data in the dropdown

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 205
    Comment on it

    In this web journal, I'll demonstrate to you generally accepted methods to bring the information from the database and showing the got information in the drop down. I do have a database named CakePHP and in that database I have one table named products. In the item table, there are fields like item id, item name, item cost, item shading. So we will bring just the item name and show that name in the dropdown list.

    In the controller, we make the ProductsController.php. The code for the ProductsController.php goes this way.

    <?php
    /**
    * Static content controller.
    *
    * This file will render views from views/pages/
    *
    * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
    * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
    *
    * Licensed under The MIT License
    * For full copyright and license information, please see the LICENSE.txt
    * Redistributions of files must retain the above copyright notice.
    *
    * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
    * @link          http://cakephp.org CakePHP(tm) Project
    * @package       app.Controller
    * @since         CakePHP(tm) v 0.2.9
    * @license       http://www.opensource.org/licenses/mit-license.php MIT License
    */
    
    App::uses('AppController', 'Controller');
    
    /**
    * Static content controller
    *
    * Override this controller by placing a copy in controllers directory of an application
    *
    * @package       app.Controller
    * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
    */
    class ProductsController extends AppController {
    
    /**
    * This controller does not use a model
    *
    * @var array
    */
    public $uses = array();
    
    /**
    * Displays a view
    *
    * @return void
    * @throws NotFoundException When the view file could not be found
    *  or MissingViewException in debug mode.
    */
    public function display() 
    {
        $this->layout = null;
        $prodList = $this->Product->find('all', array('fields' => array('Product.id, Product.name')));
        $prod = array();
        foreach($prodList as $product) {
            $prod[$product['Product']['id']] = $product['Product']['name'];
        }
    //echo "<pre>";print_r($prod);die;
    
        $this->set('productList',$prod);
    
    }
    }
    

     

    In the ProductsController.php we make a display function to display the value of the fetched data in the dropdown.

    In the View, we make a folder named Products in the Products we make a display.ctp for the specified controller.

    <?php
    
    echo $this->Form->input('products', array('type'=>'select', 'id'=>'products', 'label'=>'Products*', 'options'=>$productList, 'selected'=>0));

    $ProductList contain data fetched from database i.e. Id and Name.

    So, dropdown show all the Product name .

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: