0

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

3
  • You'd pass data to the controller, then to your model. Please post both your controller and the part of the view that contains the input Commented Apr 14, 2017 at 14:42
  • @Kisaragi it's edited now Commented Apr 14, 2017 at 14:45
  • You should be a bit more precise here, do you want a table to only populate when a certain category is selected? I can't draw a correlation between the question you are asking and the view snippet you added Commented Apr 14, 2017 at 14:51

2 Answers 2

0

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.