2017-03-05 91 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