2015-06-07 26 views
1

我建立我在哪裏使用jQuery插件數據表一個Laravel 5應用填充輸入。在數據表中,每行有3個輸入字段。我想要做的是插入所有填充的行輸入。在每一行中,您必須填寫所有3個輸入,以便將數據發佈到數據庫中。 (數據庫是sql服務器。)我嘗試了一些東西,但它不工作。以下是我在控制器存儲方法已經試過:插入到數據庫中只有一個html表

$dates = $request->get('month'); 
    $values = $request->get('value'); 
    $comments = $request->get('comment'); 
    $reports = []; // this will keep what we input in our DB 
    /*if(!empty($row['month']) && !empty($row['value']) && !empty($row['comment'])){ 
     foreach($row as $rows){ 
      $index = +1; 
     } 
    }*/ 
    if(!empty($dates) && !empty($values)){ 
    for($i = 0; $i < count($dates); $i++) 
    { 
     //here i am dividing month and year 
     $date = explode('-', $dates[$i]); 
     $year = $date[0]; 
     $month = $date[1]; // <-- line 75 

      $reports[] = Reports::create([ 
       'month' => $month, 
       'year' => $year, 
       'value' => $values[$i], 
       'comment' => $comments[$i] 
      ]); 

     } 
    } 
    return $reports; 

這引發了我的錯誤,但:

ErrorException in ReportController.php line 75: 
Undefined offset: 1 

我搜索它,它說,我是傳遞一個數組而不是一個字符串。

這裏是堆棧跟蹤(的一部分):

at HandleExceptions->handleError('8', 'Undefined offset: 1', 'E:\socgen\soc-gen\app\Http\Controllers\ReportController.php', '75', array('request' => object(Request), 'dates' => array('2015-06', '2015-02', ''), 'values' => array('369', '22223', ''), 'comments' => array('878', '5466', ''), 'reports' => array(object(Reports)), 'i' => '1', 'date' => array(''), 'year' => '', 'month' => '06')) in ReportController.php line 75 

任何幫助或暗示表示讚賞

+0

你在第75行的代碼是什麼 –

+0

'$ row'是什麼?它來自哪裏? – itachi

+0

@itachi對不起,這是一個我早些時候做的測試。它應該已經在評論這裏 – xhulio

回答

0

上php.net官方文檔閱讀以下有關爆炸功能。

返回一個字符串數組,其中每一個是通過拆分它由定界符形成邊界形成的子串。

它的結論是爆炸功能的PHP只需要字符串作爲參數,在你的情況下,你傳遞一個數組。我建議你首先把你的數據放在一個字符串中,然後通過它來爆炸。例如

for($i = 0; $i < count($dates); $i++) 
{ 
    //remove empty indexes from your array 
    array_diff($dates[$i], array('')); 

    //create a new string and put your date into it. 
    $dateString = $dates[$i]; 

    $date = explode('-', $dateString); 
    $year = $date[0]; 
    $month = $date[1]; // <-- line 75 

    //rest of your code goes here 
    } 
} 
+0

不起作用。問題是,當這些值發佈時,它們以數組形式出現。我只想得到非空白的值。 atm所有我得到的是$日期('2015-06','2015-07',''),如果我離開最後一行沒有價值。檢查 – xhulio

+0

使用isset,如果你的陣列的當前指數不爲空。你可能還會使用array_diff();過濾掉你的數組 –

+0

空白索引你能編輯答案嗎? – xhulio

相關問題