Topic: Undefined offset (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=31967" title="Pages that link to Topic: Undefined offset (Page 1 of 1)" rel="nofollow" >Topic: Undefined offset <span class="small">(Page 1 of 1)</span>\

 
andresg
Neurotic (0) Inmate
Newly admitted

From:
Insane since: Aug 2010

posted posted 08-30-2010 11:02

Hi there,

First i want to tell i am not that expirienced in PHP however i am able to read and to understand whats going on however i having a hard time to edit PHP.

Since a few days our website reporting some errors.

Like this one:

Notice: Undefined offset: 13 in:
Here is the code which i think is causing the error:

code:
if (substr($fields[11],0,strlen($countryicao)) == $countryicao OR substr($fields[13],0,strlen($countryicao)) == $countryicao) {
                        array_push($pilots, $fields);



I hope you can help me to fix this error.

Thanks in advance

Andresg

Edit TP: added code tags

(Edited by Tyberius Prime on 08-30-2010 12:23)

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 08-30-2010 12:24

so $fields does not contain 13 entries. You will need to stick a var_dump($fields); into th line before that to see what's actually in there, and it would really help if you had an idea what should be in there...

andresg
Obsessive-Compulsive (I) Inmate

From:
Insane since: Aug 2010

posted posted 08-30-2010 14:51

well the strange thing is that it start working again a few hours ago, the script it self is just anoying, is there not a way to disable the warning message. or if $fieldsB is empty that he don't show an error?

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 08-30-2010 14:57

stick an @ before $fieldsB (or before the substr... my PHP is rusty).

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 08-30-2010 16:13

To suppress errors from functions, you need to prefix them with @ (as TP mentioned), i.e. @substr(...)

However, the proper way to fix this would be to check how many elements are in that array, before calling substr, something like this:

code:
if ((count($fields) >= 14 && substr($fields[13],0,strlen($countryicao)) == $countryicao) || (count($fields) >= 12 && substr($fields[11],0,strlen($countryicao)) == $countryicao)) { ... }




Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 08-30-2010 20:05

nah, the proper way would be to figure out why the array suddenly contains fewer elements than before...

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 08-31-2010 04:15

I just suggested an easy way out (that's somewhat better than suppressing errors with @)

As far as variable fields count is concerned, a wild guess would be that fields are being populated by different db queries depending on some criteria, in which case it would be much better to fetch data as associative array and access fields by name. However, without seeing the rest of the code, I would still suggest to simply not waste time on this and take the easy way out (you can't fix every bad code)...


Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 08-31-2010 09:43

and I say, if you want to kludge, use the most prominent one available, which would be the '@'.
Bonus: It doesn't look like it was intentional coding in the first place, but a horrible horrible damage, eh kludge



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu