2017-03-05 27 views
2

我有問題函數在控制器,形成更象波紋管代碼:消息:取消定義偏移量:10從笨

我發送數據IDDATA用Ajax到控制器 「Update_Kebutuhan_ke_Terpasang」

http://localhost/gishubkbm/Terpasang/Update_Kebutuhan_ke_Terpasang?iddata=[1,2,16,15,17,3,14,5,9,11] 

代碼控制器像波紋管:

$iddata=array(); 
$iddata=array($_GET['iddata']); //value [1,2,16,15,17,3,14,5,9,11] 
$a=explode(",", $iddata[0]); //explode value iddata 
$b=preg_replace('/[^A-Za-z0-9\-]/', '', $a); 
$jumlahdata=count($b); 
for($i=0;$i<=$jumlahdata;$i++) 
{ 
    $c=$b[$i]; // **problem here with message undefined offset : 10** 
    $data=array('id_perjal'=>$c); 
    $this->M_perjal_terpasang->save($data); 
} 
echo json_encode(array("status" => TRUE)); 

但所有專業人員es運行,數據可以在數據庫上輸入。

回答

1

不止一件事情需要改變。檢查評價和代碼象下面這樣: -

$iddata=array(); 
//$iddata=array($_GET['iddata']); //value [1,2,16,15,17,3,14,5,9,11] this line not needed 
$a=explode(",", $_GET['iddata']); //explode whole $_GET['iddata'] 

$jumlahdata=count($a); // count the length 
for($i=0;$i<$jumlahdata;$i++) // remove = to sign so that it iterates only 10 times from 0 to 9 
{ 
    if(!empty($a[$i])){ // still check variable is set and have value 
     $b=preg_replace('/[^A-Za-z0-9\-]/', '', $a[$i]); // do the replacement here on each array value,not just one outside 
     $c=$b; 
     $data=array('id_perjal'=>$c); 
     $this->M_perjal_terpasang->save($data); 
    } 
} 
echo json_encode(array("status" => TRUE)); 
2

你超過迭代

由於陣列在PHP 0索引,含有10個元素的陣列不具有與10的索引的元素 - 最後的指數爲9

for循環僅應遍歷$i當它比元素的數量少小於或等於

變化:

for($i = 0; $i <= $jumlahdata; $i++) 
{ 
    $c=$b[$i]; // **problem here with message undefined offset : 10** 
} 

到:

for($i = 0; $i < $jumlahdata; $i++) 
{ 
    $c=$b[$i]; // **problem here with message undefined offset : 10** 
} 
1

,當我運行你的代碼,我得到了一些錯誤回報。

  1. 在這一點上$a=explode(",", $iddata[0]);只有一個數據返回
  2. $b=preg_replace('/[^A-Za-z0-9\-]/', '', $a);沒有IDE,你這個是什麼意思?

最終工作代碼

$b=array(); 
$b=array(1,2,16,15,17,3,14,5,9,11); # assume your array 

foreach($b as $item) 
{ 
    echo $item; # this will print all in a row. 
    //$data=array('id_perjal'=>$item); 
    //$this->M_perjal_terpasang->save($data); 
} 

如果使用empty()它總是正確的,CZ你不固定實際的錯誤


這可能有助於全力爲您

  1. how to check for special characters php