2012-08-12 80 views
0

我有一系列相互依存的選擇列表,它們可以在CI之外正常工作。當我試圖在Codeigniter中實現它時,由於沒有填充第一個選擇列表,所以我沒有出現,或者回顯代碼不正確,或者我不知道是什麼。這裏的問題將引用第一個選擇列表,也就是說,你不會看到任何jquery,因爲第一個直接從數據庫填充而沒有任何「功能改變」。Codeigniter:相互依賴的下拉列表。完成代碼

所以這裏的模塊:

VIEW

<?php echo form_open('control_form/add_all'); ?> 
     <label for="f_state">State<span class="red">*</span></label> 
     <select id="f_state" name="f_state"> 
      <option value=""></option> 
      <?php 

       foreach($result as $row) 
       { 
       echo '<option value="' . $row->pais_id . '">' . $row->pais_name . '</option>'; 
       } 

      ?> 
     </select> 

     <label for="f_city">City<span class="red">*</span></label> 
     <!--this will be filled based on the tree selection above--> 
     <select id="f_city" name="f_city" id="f_city_label"> 
      <option value=""></option> 
     </select> 

     <label for="f_membername">Member Name<span class="red">*</span></label> 
     <input type="text" name="f_membername"/> 
<?php echo form_close(); ?> 

控制

public function add_all() 
    { 

     #Validate entry form information 
     $this->load->model('model_form','', TRUE);   
     $this->form_validation->set_rules('f_state', 'State', 'required'); 
     $this->form_validation->set_rules('f_city', 'City', 'required'); 
     $this->form_validation->set_rules('f_membername', 'Member Name', 'required'); 

     $data['city'] = $this->model_form->get_state(); //gets the available groups for the dropdown 


     if ($this->form_validation->run() == FALSE) 
     { 
       $this->load->view('view_form_all', $data); # parece ser que vuelve a meter los mismos datos que tenia la Form 
     } 
     else 
     { 
      #Add Member to Database 
      $this->model_form->add_all(); 
      $this->load->view('view_form_success'); 
     } 


    } 

模型

<?php 
class Model_form extends CI_Model 
{ 
     function __construct() 
    { 
      // Call the Model constructor 
      parent::__construct(); 
    } 

    function get_state() 
    { 
     $query = $this->db->query('SELECT pais_id, pais_name FROM pais'); 
     return $query->result(); 
    } 


    function add_all() 
    { 
     $v_state = $this->input->post('f_state'); 
     $v_membername = $this->input->post('f_membername'); 

     $data = array(
       'pais_id' => NULL, 
       'pais_name' => $v_state 

     ); 

     $this->db->insert('members', $data); 
    } 



} 
+0

你在哪裏發送'$ result'到視圖? – 2012-08-12 09:11:51

+0

嗯我相信這裏:$ this-> load-> view('view_form_all',$ data);即,$ data是從Model查詢到數據庫的數組。如果選擇列表沒有被觸及,那麼它應該與數據一起加載視圖 – iaintunderstand 2012-08-12 09:15:38

+0

注意你正在發送'$ city'而不是'$ result'。沒有'$ result'變量。 – 2012-08-12 09:17:01

回答

0

你是不是發送$result的視圖,你需要的東西像:

$data['result'] = ...//get some data 
$this->load->view('view_form_all',$data); 

如果你想顯示的列表是城市名單,那麼你需要在你的看法改變:

foreach($city as $row) 
{ 
     echo '<option value="' . $row->pais_id . '">' . $row->pais_name . '</option>'; 
} 

,因爲在你的控制器,你正在做的:

$data['city'] = $this->model_form->get_state(); 
+0

但是,這不是$ data ['city'] = $ this-> model_form-> get_state();其實? :(是不是一樣,我有?$ this-> load-> view('view_form_all',$ data); – iaintunderstand 2012-08-12 09:18:11

+0

是的,但在你看來你調用錯誤的參數,你有'$ data ['city' ]',所以在視圖中,變量名將是'$ city'不是'$ result' – Tomer 2012-08-12 09:19:31

+0

好吧,讓我試試吧 – iaintunderstand 2012-08-12 09:20:15