Welcome Guest, Not a member yet? Register   Sign In
Problem creating a helper (Error undefined offset)
#1

[eluser]yoelrodguez[/eluser]
Hi I had problems with this helper that I created in codeigniter me this error is generated (Message: Undefined offset: 2) in the line:
($ iten == $ array [$ v])? $ found = true $ found =’‘;
happened also the code of the function:
$v = 0;
foreach($array as $key => $value){

$data = array(
$campo2=>$value,
$campo1=>$id
);

$ci->db->where($campo1, $id);
$qi = $ci->db->get($tabla);

foreach($qi->result() as $rows){

$found = false;
$iten = $rows->$campo2;

($iten == $array[$v])? $found = true: $found = ‘’;

if($found == false){
$ci->db->insert($tabla,$data);
}
$v++;
}
}
#2

[eluser]yoelrodguez[/eluser]
hello to all forum colleagues throw this question yesterday and a colleague gave me the solution by mail what happened to me is that the array index was smaller than the cycle this was the solution:

(count($array) > $i)?($iten == $array[$i])? $found = true : '':'';

Thanks for the views of all
#3

[eluser]Pert[/eluser]
As array indexes could be anything, you can't trust count($array). You can have array that looks like this:
Code:
$array[1] = 'a';
$array[1000] = 'b';
$array[-1] = 'c';

Better option would be to check if array element has been set with said index and then do the check for array element value:
Code:
$found = isset($array[$v]) && $iten == $array[$v] ? true : '';
#4

[eluser]yoelrodguez[/eluser]
Thanks




Theme © iAndrew 2016 - Forum software by © MyBB