繁体   English   中英

如何在Codeigniter中将变量从模型传递到控制器

[英]How to pass a variable from model to controller in 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个字段。 (由于您将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行而不使用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');
    }

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

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

        }

在您看来,您可以像这样使用变量:

$item_name;
$item_desc;
$item_price;

例如:

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

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

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

        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM