How to pass a variable from model to controller in codeigniter

How can I pass a variable from the model to the controller?

Here is my model:

public function edititem($id){
$query = $this->db->query('SELECT * FROM tblitem WHERE item_id = "$id"');

foreach ($query->result() as $row){
    $name = $row->item_name;
    $description = $row->item_description;
    $price = $row->item_price;


And here is my controller

public function editItem(){

      $name = $this->input->post('name');
      $description = $this->input->post('description');
      $price = $this->input->post('price');

      $data['items'] = $this->ItemModel->itemlist();



In which when the user clicks "Edit Item", it will populate the form with the selected row.

<form action="<?php echo base_url().'item/edititem' ?> " method="post">
       <div class="form-group">
            <label for="name">Name</label>
            <input type="name" class="form-control" id="name" name="name" placeholder="Name" value="<?php echo set_value('name'); ?>">
       <div class="form-group">
            <label for="description">Description</label>
            <textarea class="form-control" rows="3" name="description"><?php echo set_value('description'); ?></textarea>

       <div class="form-group">
            <label class="sr-only" for="price">Amount (in pesos)</label>
            <div class="input-group">
                  <div class="input-group-addon">Php</div>
                  <input type="text" class="form-control" name="price" id="price" placeholder="Amount" value="<?php echo set_value('price'); ?>">
                  <div class="input-group-addon">.00</div>
      <button type="submit" class="btn btn-primary">EDIT ITEM</button>


If it is from model to controller, You can make an array and store that 3 fields. (I assume there is only single row returned by your query since you are using id as where).

$result = array();
foreach ($query->result() as $row)
    $result['name'] = $row->item_name;
    $result['description'] = $row->item_description;
    $result['price'] = $row->item_price;
return $result;

You can access it in the controller using:


Or in your view as


Here your model looks like this

public function edititem($id){
$query = $this->db->query('SELECT * FROM tblitem WHERE item_id = "$id"');

  if ($query->num_rows() > 0) {
        $row = $query->row_array();  // row_array return single row and result_array return multiple row
        return $row['dt'];

And your controller looks like this

public function editItem(){
here you use foreach loop 

foreach ($item_details as $key=>$value){
//do this in $data array
}     //now you can use the specific data to your view.


Your Model: This may useful if your want to return a specific 1 row without using result()

public function edititem($id){
        $this->db->from('tblitem ');
        $this->db->where('item_id ',$id);
        return $query = $this->db->get()->custom_row_object(0, 'ItemModel');

Your Controller: Call the edititem($id) from ItemModel (your model)

public function editItem(){
             $item_id=$this->uri->segment(3); //added this
             $item_details=$this->ItemModel->edititem($item_id); //added this

             $data['item_name'] = $item_details->name;
             $data['item_desc'] = $item_details->description;
             $data['item_price'] = $item_details->price;

             //now you can use the specific data to your view.


In your view, you can use the variable like:



<input type="text" name="item_name" value="<?php echo $item_name;?>" />

Need Your Help

Mongodb $elemMatch query inside map

mongodb map

I need to search inside a map element with a certain value in mongodb.

How to use python build script with teamcity CI?

c++ python continuous-integration teamcity

I am currently researching using the TeamCity CI software for our comapanies CI automation needs but have had trouble finding information about using different build scripts with TeamCity. We have ...