Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Fetch the particular data on click of dropdown and display that data in the table

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 107
    Comment on it

    To fetch a particular data with the help of id on click of a drop down, We have to make a database with table named products and in that table we have to add various columns like id, name, cost, color.

    Then write following code in the ProductController.php file

    <?php
    
    App::uses('AppController', 'Controller');
    
    
    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($data = null) 
    {
    // pr($data);exit;
        $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;
    
    
        if(isset($data) && !empty($data)) {
            $fetchdata = $this->Product->find('all', array('conditions' => array('Product.id' => $data)));
        }else {
            $fetchdata = $this->Product->find('all', array('fields' => array('Product.id, Product.name, Product.cost, Product.color')));
        }
    
    // echo "<pre>";print_r($fetchdata);die();
    
        $this->set('productList',$prod);
        $this->set('products',$fetchdata);
        $this->set('data', $data);
    
    }
    }

     

    Then we make the view. In the view, we will make a file display.ctp, the code for display.ctp goes like this.

    <!-- code to make the selectbox and fetching the productname and displaying it on the dropdown menu -->
    <?php
    
    echo $this->Form->input('products', array('type'=>'select', 'id'=>'products', 'label'=>'Products*', 'options'=>$productList, 'selected'=>$data, 'empty' =>"selected"));
    ?>
    <!-- this is the code to display the table -->
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    
        <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    </head>
    <body>
        <table border=5>
            <tr>
                <th>id</th>
                <th>Name</th>
                <th>Cost</th>
                <th>Colour</th>
                <tr>
                    <?php foreach($products as $product):?> 
    
                        <tr>
    
                            <td><?php echo $product['Product']['id'];?></td>
                            <td><?php echo $product['Product']['name'];?></td>
                            <td><?php echo $product['Product']['cost'];?></td>
                            <td><?php echo $product['Product']['color'];?></td>       
                            <tr>
    
                            <?php endforeach; ?>
    
                        </table>
                    </body>
                    </html>
                    <!-- this is the jquery code with the help of which user can change the url -->
                    <script type="text/javascript">
    
                        $(document).ready(function(){
                            $("#products").change(function(e) {
                                window.location.href = 'http://localhost/jquerycake/Products/display/' + $(this).val()
                            }); 
    
                        });
    
                    </script>

 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: