Forum Moderators: coopster

Message Too Old, No Replies

Undefined Offset Error in PHP

         

stem2veterans

1:27 pm on Jan 18, 2012 (gmt 0)

10+ Year Member



Any help much appreciated...

I keep getting this intermitment error:

[Notice: Undefined offset: 1 in industry_process_page() (line 47 of /home/muniport/public_html/sites/all/themes/industry/template.php).]

... based on the following:

/*
* Process page
* pre-process hook_preprocess_page
*/
function industry_process_page(&$variables){

global $base_url;

/*********front page different color title**********/

$variable=$variables['title'];
$text=(explode(" ", $variable));
$count = count($text);
if($count < 3) {
$variables['title'] = '<span><span class="title_default">'.$text[0].'</span> '.$text[1].'</span>';
}
else {
$output = '<span>';
foreach ($text as $k => $v) {
if($k < 2) {
$output .= '<span class="title_default">'.$v.'</span> ';
}
else {
$output .= ' '.$v.' ';
}
}
$output .= '</span>';
$variables['title'] = $output;
}


This is part of a drupal template that is supposedly free and I just want to get rid of the error message. I have tried increasing the number of $count and $k to no avail.

Any suggestions?

Thanx, Brad

penders

3:29 pm on Jan 18, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if($count < 3) {
$variables['title'] = '<span><span class="title_default">'.$text[0].'</span> '.$text[1].'</span>';
}


I assume this is line#47? Undefined offset: 1 in this case is telling you that $text[1] does not exist, which it won't if there is only 1 word in your 'title'.

You need to check for the value first...
$variables['title'] = '<span><span class="title_default">'.$text[0].'</span> '.(isset($text[1])?$text[1]:'').'</span>';

stem2veterans

12:48 pm on Jan 19, 2012 (gmt 0)

10+ Year Member



Thanks Penders for your reply. Turns out if I set $count = 0 and $k = 0 the error goes away. Simple, but works. Thanks again!

penders

2:11 pm on Jan 19, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Turns out if I set $count = 0 and $k = 0 the error goes away.


Unless there is additional code that you've not posted, I can't see how doing this would resolve anything. In fact by doing this it is likely to change how the script works. The "Notice" you were getting initially is not an error, the script was probably functioning as intended, but the developer of the template probably had their error_reporting level set differently so that Notices were suppressed (which is the default behaviour).