2014-03-06 65 views
0

。嗨,大家好我是新來的CodeIgniter請幫我想從ID數據傳遞到控制器模型笨將數據傳遞給控制器​​或任何其他

public function itemdetails(){ 

      $id = $_REQUEST['id']; 

      $this->db->WHERE('assetTag', $id); 
      $query = $this->db->get('mis_inventory'); 

      foreach ($query->result_array() as $row); 
      echo "<table border='1'>"; 
      echo 'ASSET TAG'; 
      echo strtoupper($row['assetTag']); 
      echo 'ITEM TYPE'; 
      echo $row['itemType']; 
      echo 'BRAND'; 
      echo $row['brand']; 
      echo 'MODEL'; 
      echo $row['model']; 
      echo 'SERIAL'; 
      echo $row['serial']; 


      echo anchor('main/update_item?id='. ucwords($row['assetTag']), 'EDIT'); 

2.然後當我點擊編輯它會重定向到控制器代碼如下

  public function update_item(){ 
       if ($this->session->userdata('is_logged_in')){ 
        $this->load->model('model_items'); 
        $this->load->view('update'); 
       } 
      } 

3.then追溯到模型

 public function update_item(){ 


      $id = $_REQUEST['id']; 

      $this->db->WHERE('assetTag', $id); 
      $query = $this->db->get('mis_inventory'); 

      foreach ($query->result_array() as $row); 

      echo form_open('main/update_validation'); 
      echo "<table border='1'>"; 
      echo 'ASSET TAG'; 
      echo strtoupper($row['assetTag']); 
      echo 'ITEM TYPE'; 
      echo form_input('itemType', $row['itemType']); 
      echo 'BRAND'; 
      echo form_input('itemType', $row['brand']); 
      echo 'MODEL'; 
      echo form_input('itemType', $row['model']); 
      echo 'SERIAL'; 

      echo form_submit('submit', 'UPDATE'); 
      echo validation_errors(); 

4.update VA lidation代碼

  public function update_validation(){ 

       $this->load->library('form_validation'); 

       $this->load->model('model_items'); 
       $id = $_REQUEST['id']; 

       $this->form_validation->set_rules('itemType', 'itemtype', 'required'); 
       $this->form_validation->set_rules('brand', 'brand', 'required'); 
       $this->form_validation->set_rules('model', 'model', 'required'); 
       $this->form_validation->set_rules('serial', 'serial', 'required'); 
       $this->form_validation->run(); 
       $this->model_items->can_update_item(); 

       echo $this->db->affected_rows().' record updated'; 

/**我只是想在數據庫編輯項目,當我在它的ID單擊該值應該是表單標籤內,然後將編輯一些內容,點擊更新按鈕,但失敗了。 我非常需要幫助。 感謝**/

+0

1.你'foreach'什麼都不做,你在它後面加了一個';'。 2.顯示的錯誤是什麼? 3.如果你想把什麼東西傳遞給模型的功能,只需要調用你的函數'$這個 - >模型 - >例如($ ID);遇到' – Kalzem

+0

一個PHP錯誤 嚴重性:注意 消息:未定義指數:ID 文件名:控制器/ main.php 行號:158 甲PHP錯誤遇到 嚴重性:警告 消息:缺少參數1 Model_items :: can_update_item(),被稱爲在C:\ XAMPP \ htdocs \ code \ application \ controllers \ main.php 175行,並定義爲 文件名:型號/ model_items.php 行號:遇到26 一個PHP錯誤 嚴重性:注意 消息:未定義指數:ID 文件名:型號/ model_items.php 行號:28 – user3386671

+0

謝謝你爲你的答覆我有點困惑管理模型和控制器,我會嘗試$ this-> model-> example($ id); – user3386671

回答

0

下面是它如何工作:

對於控制器

public function my_url() 
{ 
    $userInput['id'] = $this->input->post('id'); //It is a safe $_POST['id'] (XSS Injection safe, so use this instead of $_POST) 
    $userInput['email'] = $this->input->post('email'); 

    $this->load->model('user_model'); 
    $result = $this->user_model->create_user($userInput); 
    echo $result; 
    //Or you can do : 
    //$data['result'] = $this->user_model->create_user($userInput); 
    //$this->load->view('result_view',$data); 
} 

對於模型

public function create_user($userInput) 
{ 
    //Check for the required field etc 
    if(!isset($userInput['email']) 
     return false; 

    $this->db->insert('my_user_table',$userInput); 

    return $this->db->affected_rows(); //Should return 1 if user is created else 0 
} 
相關問題