I Have Problem function for in controller, form more code like bellow :
i send data iddata with ajax to controller "Update_Kebutuhan_ke_Terpasang"
http://localhost/gishubkbm/Terpasang/Update_Kebutuhan_ke_Terpasang?iddata=[1,2,16,15,17,3,14,5,9,11]
code in Controller like bellow :
$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));
but all Proses run, data can input on database.
You're over iterating
Since arrays are 0-indexed in PHP, an array containing 10 elements doesn't have an element with an index of 10 - the final index is 9.
Your for loop should only iterate $i when it's less than the number of elements, not less than or equal to:
Change:
for($i = 0; $i <= $jumlahdata; $i++)
{
$c=$b[$i]; // **problem here with message undefined offset : 10**
}
to:
for($i = 0; $i < $jumlahdata; $i++)
{
$c=$b[$i]; // **problem here with message undefined offset : 10**
}
More than one things need to change. Check the comments and code like below:-
$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));
When i run you code i got few erros.
$a=explode(",", $iddata[0]); in this point only one data returning $b=preg_replace('/[^A-Za-z0-9\-]/', '', $a); no ide what you mean by this...Final working code for you
$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);
}
if you use
empty()its always true, Cz you're not fixing the actual error
This might help full for you