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(){
      $this->load->helper('form');
      $this->load->model('ItemModel');
      $this->ItemModel->edititem($this->input->get('id'));

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

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

      $this->load->view('item/item_edit',$data);

}

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>
       <div class="form-group">
            <label for="description">Description</label>
            <textarea class="form-control" rows="3" name="description"><?php echo set_value('description'); ?></textarea>
       </div>

       <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>
            </div>
      </div>
      <button type="submit" class="btn btn-primary">EDIT ITEM</button>
</form>

Answers


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:

$data['items']['name'];
$data['items']['description']
$data['items']['price']

Or in your view as

$items['name'];
$items['description'];
$items['price'];

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(){
             $this->load->helper('form');
             $this->load->model('ItemModel');
             $item_details=$this->ItemModel->edititem($this->input->get('id'));
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.
             $this->load->view('item/item_edit',$data);

        }

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

public function edititem($id){
        $this->db->select('*');
        $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
             $this->load->helper('form');
             $this->load->model('ItemModel');
             $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.
             $this->load->view('item/item_edit',$data);

        }

In your view, you can use the variable like:

$item_name;
$item_desc;
$item_price;

eg:

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