如何在codeigniter中将变量从模型传递给控制器

时间:2016-03-16 01:47:22

标签: php codeigniter

如何将变量从模型传递给控制器​​?

这是我的模特:

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;

}

这是我的控制器

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

}

当用户点击“编辑项目”时,它将使用所选行填充表单。

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

3 个答案:

答案 0 :(得分:0)

如果是从模型到控制器,您可以创建一个数组并存储该3个字段。 (我假设您的查询只返回单行,因为您使用id作为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;

您可以使用以下方法在控制器中访问它:

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

或者在您的视图中

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

答案 1 :(得分:0)

你的型号: 如果您想在不使用result()的情况下返回特定的1行,这可能很有用。

L = open(os.path.join(dir, file), "r").read()
            line = L.rstrip()
            tokens = line.split()

            for word in tokens:
                for char in word:

您的控制器:从ItemModel(您的模型)调用edititem($ id)

{ {2}}

在您看来,您可以使用如下变量:

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

例如:

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

        }

答案 2 :(得分:0)

这里你的模型看起来像这样

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'];

你的控制器看起来像这样

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

        }