Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
Message : undefine Offset : 10 from codeigniter

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.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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**
}
about 4 years ago · Santiago Trujillo Report

0

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));
about 4 years ago · Santiago Trujillo Report

0

When i run you code i got few erros.

  1. $a=explode(",", $iddata[0]); in this point only one data returning
  2. $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

  1. how to check for special characters php
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!