2013-07-21 56 views
0

我是Codeignite中的新成員。我有一個帶有「index()」和「view($ id)」函數的「測試」控制器。索引方法轉到「test1.php」視圖。在「test1.php」中,我有一個下拉選項。如果我在這個頁面上提交,將會調用「測試」控制器的「視圖」方法。我的問題是,如何將選項值傳遞給「view」函數,以便將方法的參數設置爲選項值,然後URL將如"http://localhost/test/view/id"這些id是來自「test1.php」的選項值顯示html選項值codeigniter

測試控制器

class Test extends CI_Controller{ 
    public function indx() { 
    //some code 
     $this->load->view('test1.php'); 
    } 
    public function view($id) 
    { 
    //some code, here I use $id which I want to be option value from test1.php 
    } 

test1.php

<?php 
$options = array(
       '1' => 'One', 
       '2' => 'Two', 
       '3' => 'Three', 
       '4' => 'Four', 
      ); 

$js = 'id="shirts" onChange="this.form.submit();"'; 
echo form_dropdown('shirts', $options, '1', $js); 
/* here I want to call echo form_open() as echo form_open("/test/view/[option value]") but I don't know how to do this;*/ 

回答

0

可以加載視圖數據。

class Test extends CI_Controller{ 
    public function index($id) { 
     $this->data['result'] = get_results($id); 
     $this->load->view('test1.php', $this->data); 
    } 
} 

在視圖文件:

foreach ($results as $result) { 
    // action 
} 
0

如果其重要的是你通過在URL中的ID,那麼你就需要使用JavaScript的使用的ID追加到表單操作URL在選擇字段上更改事件偵聽器。

但是,恕我直言,你最好從POST數據中檢索視圖()方法中的值。

使用$ id = $ this-> input-> post('shirts',TRUE),而不是強制通過URL。