php - codeigniter pass data from view to model -
i need show category , sub category, model right show static $id can see attempted write $id = 4 (for testing only), it's show subcategory $id = 4 in every category.
public function get_subkriterias($id = 4) { $this->db->select('kriterias.nama 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 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 $item) { ?> <tr> <td><?php echo $item['nama']; ?></td> <td> <table class="table"> <tbody> <?php foreach ($sub_kriterias $item) { ?> <tr> <td><?php echo $item['nama']?></td> <td><?php echo $item['nilai'] ?></td>
how pass data view or anywhere $id model can retrieve $id dynamically , show right subcategory in every category?
thank in advance
there few ways can this, here limited example using ajax
in 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 view we'd have list of items:
<select id= "categorey_select"> <? foreach($kriterias $item):?> <option name="selected_cat" value="<?=$item['id'];?>"><?=$item['nama'];?></option> <?endforeach;?> </select> <!-- notice table empty now--> <table id="category_results"> </table>
here use jquery event listener send ajax request once category clicked ( can add in view or load js script 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 see returned object's structure**/ /** here populate empty table results **/ /** using empty reset table each time category changes**/ $("category_results").empty().append("<tr>...") } }) }) </script>
Comments
Post a Comment