简体   繁体   中英

Getting an “8: Undefined offset: 1” error when re-installing forum mod

To preempt this being marked as a duplicate question, please understand that I have zero experience with PHP coding/syntax and I am struggling to apply a general answer to my specific problem. I think I've figured out where the problem is coming from, but I'm not even 100% about that, so I am seeking the assistance of someone who can help guide me through figuring this out for certain.

We are installing a dice rolling mod to a Simple Machines forum. The first install went smoothly, no problems. We wanted to change some of the mod's formatting, so we uninstalled it, changed the files, and reinstalled it. After reinstallation, posting new topics or make replies resulted in a . When we disabled the mod, we could reply and post new topics as usual.

After some hunting, we found this in our error log;

http://irate-pirate.org/forums/index.php?action=admin;area=packages;get;sa=browse;server=1;relative=posting  
8: Undefined offset: 1  
File: /home4/iratepir/public_html/forums/Sources/Subs-Package.php  
Line: 1503

We found the Subs-Package.php file, and here is the surrounding code:

1501      // Build an array of parts.
1502        $versions[$id] = array(
1503             'major' => (int) $parts[1],
1504             'minor' => !empty($parts[2]) ? (int) $parts[2] : 0,
1505             'patch' => !empty($parts[3]) ? (int) $parts[3] : 0,
1506             'type' => empty($parts[4]) ? 'stable' : $parts[4],
1507             'type_major' => !empty($parts[6]) ? (int) $parts[5] : 0,
1508             'type_minor' => !empty($parts[6]) ? (int) $parts[6] : 0,
1509             'dev' => !empty($parts[7]),
1510        );
1511   }

So, after looking at this post , I think(???) need to add:

//isset()
$value = isset($array['my_index']) ? $array['my_index'] : '';
//array_key_exists()
$value = array_key_exists('my_index', $array) ? $array['my_index'] : '';

to the code I've found in the Subs-Package.php file. I'm guessing that I add this before the line the error was found in.

I'm also guessing some of these variables need to be changed to match the parts from our code, but I don't know which variables.

Any help would be absolutely fantastic, and I hope I've given enough information!

You need to check if the $parts array indexes are set or not. It can happen that the array $parts does not exists. So, you need to add isset to handle such cases.

For eg

1501      // Build an array of parts.
1502        $versions[$id] = array(
1503             'major' => isset($parts[1]) ?(int) $parts[1] : 0,
1504             'minor' => isset($parts[2]) ?(int) $parts[2] : 0,
1505             'patch' => isset($parts[3]) ?(int) $parts[3] : 0,
1506             'type' => isset($parts[4]) ?(int) $parts[4] : 0,
1507             'type_major' => isset($parts[5]) ?(int) $parts[5] : 0,
1508             'type_minor' => isset($parts[6]) ?(int) $parts[6] : 0,
1509             'dev' => isset($parts[7]) ?(int) $parts[7] : 0,
1510        );
1511   }


isset() will check if the referenced value is defined or not. 
empty($parts[index]) can give error in cases where array $parts is not defined 
as it will try to check the value that doesn't exists.

This code will work even if $parts array is not defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM