codeigniter pass data from view to model


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


<?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

<a href="https://i.stack.imgur.com/yIWll.png" rel="nofollow">image</a>


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>


To call <strong>model's</strong> function from <strong>view</strong>. Try like below:


Load model in view using


</li> <li>

Call to model's function with required parameters


</li> </ol>


  • Why does the font in these TD elements render at different sizes?
  • garbled css name when styling within UiBinder
  • How to open html table in xls on click of a button
  • how to get username into sql trigger when multiple users signed on from asp membership
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • XSLT foreach repeating nodes to flat
  • JqueryMobile Popup menu is not working
  • List images(01.png) and descriptions(01.txt) from directory
  • How does this usort cmp function actually work?
  • Breaking out column by groups in Pandas
  • Not able to aggregate on nested fields in elasticsearch
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Custom validator control occupying space even though display set to dynamic
  • How do I alternate colors in Flat List (React Native)
  • D3 nodes and links from JSON with nested arrays of children
  • $wpdb not working in file of WordPress plugin
  • Meteor helpers not available in Angular template
  • Read a local file using javascript
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Can Jackson SerializationFeature be overridden per field or class?
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Unanticipated behavior
  • A cron job substitute?
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Proper way to use connect-multiparty with express.js?
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?