2014-10-27 21 views
1

首先,感謝您的寶貴的時間和興趣 - 任何輸入到初學者非常感謝!Codeigniter:我如何傳遞多個同名的輸入?

如何插入多個輸入(多數情況下)多個同名?這裏是我的代碼:

查看:

//Simplified version, original is a jquery append script 
    Passes: <input type="text" name="passes[]"> 
    Points: <input type="text" name="points[]"> 
    Passes: <input type="text" name="passes[]"> 
    Points: <input type="text" name="points[]"> 

控制器:

//Loops through a table like form for inputting stats 
function add_stat() { 
    $i = 0; 
    foreach ($this->input->post('points') as $points) { 
    $dataSet1[$i++] = array (
      'points' => $points 
    ); 
    } 

    foreach ($this->input->post('passes') as $passes) { 
    $dataSet2[$i++] = array (
      'passes' => $passses 
    ); 
    } 
//Not sure how to pass multiple arrays, or if even possible 
$this->sport_model->insert_stat($dataSet1, $dataSet2); 
} 

型號:

//Passing multiple params error out due to "String to Array Conversion" 
function insert_stat($dataSet1, $dataSet2) { 
    $this->db->insert_batch('table', $dataSet1, $dataSet2); 
    return $this->db->insert_id(); 
} 

回答

2

將您的控制器和型號設置爲:

控制器:

//Loops through a table like form for inputting stats 
function add_stat() { 

    $points = $this->input->post('points'); 
    $passes = $this->input->post('passes'); 
    for($i=0;$i<sizeof($points);$i++) 
    { 
    $dataSet[$i] = array ('points' => $points[$i], 'passes' => $passses[$i]); 
    } 
    // $dataSet is an array of array 
    $this->sport_model->insert_stat($dataSet); 
} 

型號:

function insert_stat($dataSet) 
{ 
    $this->db->insert_batch('table', $dataSet); 
    return $this->db->insert_id(); // this will return the id of last item inserted. 
} 
+1

工程就像一個魅力,兄弟!非常感謝! – ekm383 2014-10-28 04:40:15

0

首先,insert_batch()只需要兩個參數:表名和一個數據集。

其次,insert_id()對批量插入不會有太大幫助,但那不是什麼大問題。

我假設你想要點和通行證在相同的記錄,並且你通過JS附加了另一組點/通道輸入。

查看:

Passes: <input type="text" name="scores[0][passes]"> 
Points: <input type="text" name="scores[0][points]"> 
Passes: <input type="text" name="scores[1][passes]"> 
Points: <input type="text" name="scores[1][points]"> 
// etc... 

在你的控制器,你就用一個通過$this->input->post('scores') $數據集陣列和循環

當$ DataSet是完整的,你會批量插入與$this->db->insert_batch('table', $dataSet)

+1

謝謝您的回覆!我改變了name屬性,但是,我仍然收到一個字符串錯誤 'var points = document.createElement('input');' 'setAttributes(points,{「name」:「scores [] [points ]「,」id「:」points「,」value「:」0「});' – ekm383 2014-10-28 02:02:45

相關問題