ci_lover
ci_lover

Reputation: 718

How to show data from database depending on user's choice from a select

I have to make a select and when user choose option from it, it should be displayed data from database based on this select. I can't use submit. It only should be displayed this select and when option is selected, to show data from this select. I'm new to jQuery and I can't do it. My view is:

 <select name="myselect" id="myselect" >

        <option value=''" . set_select('size, '') . " > </option>

        <option value="1" <?php echo set_select('size', '1'); ?> role="menuitem">1</option>
        <option value="2" <?php echo set_select('size', '2'); ?> role="menuitem">2</option>
        <option value="3" <?php echo set_select('size', '3'); ?> role="menuitem">3</option>
        <option value="4" <?php echo set_select('size', '4'); ?> role="menuitem">4</option>
 </select>

My model is:

<?php

    public function average_price_by_week() {

            $this->db->select(*);
            $this->db->from('items');

            $this->db->where('size', $size); //this $size should be value from select that user has selected

            $query=$this->db->get();
            return $query->result_array();

        }



?>

Upvotes: 0

Views: 742

Answers (1)

Siva.G ツ
Siva.G ツ

Reputation: 839

Try this, It may help you.

I have used JQuery Ajax. Also include JQuery library before use the below code.

$('#myselect').change(function(){
    var size = $('#myselect').val();
    if(size != ''){
        $.ajax({
            type:'post',
            url:"/index.php/average_incomes_by_customer/" + size,
            cache:false,
            success: function(returndata){
                console.log(returndata); //"returndata" is the result got from the DB. Use it where you want.
            }
        });
    }
});

Controller:

public function average_incomes_by_customer($size) { 
    $data['average_price']=$this->receivedOrders_model->average_price_by_week($size);
    $this->load->view('charts', $data); 
}

Model:

public function average_price_by_week($size) {
    $this->db->select(*);
    $this->db->from('items');
    $this->db->where('size', $size); //this $size should be value from select that user has selected           
    $query=$this->db->get();
    return $query->result_array();
}

Thanks.

Upvotes: 1

Related Questions