codeigniter pass data from view to model

2019-09-14 15:36发布

问题:

i need to show category and sub category, my model right now is show static $id as you can see i attempted to write $id = 4 (for testing only), so it's only show subcategory where $id = 4 in every category.

  public function get_subkriterias($id = 4)
    {
    $this->db->select('kriterias.nama as kriterias_nama, kriterias.id, sub_kriterias.id, sub_kriterias.nama, sub_kriterias.nilai');
    $this->db->from('kriterias');
    $this->db->join('sub_kriterias', 'kriterias.id = sub_kriterias.kriteria_id', 'LEFT');
    $this->db->where('kriteria_id', $id);
    $this->db->order_by('kriterias.id');
    $query = $this->db->get();
    return $query->result_array();
}

this is my controller

    public function index()
    {
    $data['kriterias'] = $this->subkriterias_model->tampil();
    $data['sub_kriterias'] = $this->subkriterias_model->get_subkriterias();
    $this->load->view('sub_kriterias/tampil', $data);
    }

view:

            <?php foreach ($kriterias as $item) { ?>
        <tr>
            <td><?php echo $item['nama']; ?></td>
            <td>
                <table class="table">
                    <tbody>
                    <?php foreach ($sub_kriterias as $item) { ?>
                    <tr>
                        <td><?php echo $item['nama']?></td>
                        <td><?php echo $item['nilai'] ?></td>

how do i pass data from view or anywhere to $id so this model can retrieve $id dynamically and finally show only right subcategory in every category?

thank you in advance

image

回答1:

There are a few ways you can do this, here is a very limited example using ajax

In your controller:

public function index()
{
    /**getting intial items only**/
    $data['kriterias'] = $this->subkriterias_model->tampil();
    $this->load->view('sub_kriterias/tampil', $data);

}

public function get_category()
{
    /** id posted via ajax once user clicks category**/
    $id = $this->input->post('id');
    $sub_kriterias = $this->subkriterias_model->get_subkriterias($id);
    echo json_encode($sub_kriterias);
    exit();
}         

In the view we'd have a list of items:

 <select id= "categorey_select">
    <? foreach($kriterias as $item):?>
        <option name="selected_cat" value="<?=$item['id'];?>"><?=$item['nama'];?></option>
    <?endforeach;?>
 </select>

<!-- notice this table is empty for now-->
<table id="category_results">
</table>

Here we would use a jquery event listener to send an ajax request once a category is clicked ( you can add this in the view or load a js script that contains it):

<script>
    $("#categoery").change(function(){
        var id = $(this).val();

        $.ajax({
           url: '/path/to_controller/get_category',
           method: "POST",
           data: {id: id},

           success: function(resp){
              /** use console/web tools to see the returned object's structure**/
              /** here you would populate the empty table with the results **/                 
             /** using empty to reset the table each time category changes**/
                $("category_results").empty().append("<tr>...")
           }
         })
    })
</script>


回答2:

To call model's function from view. Try like below:

  1. Load model in view using

    $this->load->model('model_name');

  2. Call to model's function with required parameters

    $this->model_name->method_name($parameters);