/    Sign up×
Community /Pin to ProfileBookmark

Notice: Undefined offset: 3 in line

I am a PHP newbie and I am getting two notices on a code that is part of a gallery, can you help me figure this out? Notices are

[CODE]Notice: Undefined offset: 3 in /home/raj/public_html/viewgallery.php on line 408
Notice: Undefined offset: 3 in /home/raj/public_html/viewgallery.php on line 426[/CODE]

Line 408 is

[CODE] ‘pcaption’ => $caption_array[$next]);[/CODE]

And line 426 is

[CODE]$result_final.= ‘<div class=”limage”><table><tr><td><table class=”image”><tr><td><a href=”photos/’.$cname.’/’.$caption_array[$next].’/’.$q.’/”><img src=”‘ . $images_dir . ‘/’ . $photo_filename . ‘” alt=” ‘ . $photo_keywords . ‘”></a><div class=”caption”>’ . $photo_caption . ‘</div><div class=”excerpt”>’ . $photo_description . ‘</div></td></tr></table></td></tr></table><div class=”underline”></div></div>’.$backString.'</div>’;
[/CODE]

The above lines are part of this code…

[CODE]
$next = 0;
$prev = 0;
if ($ct > 1)
{

$key = array_search($pcaption, $caption_array);
$prev = $key – 1;
if ($prev < 0) $prev = $ct – 1;
$next = $key + 1;
$searchNext = $key + 1;

if ($next == $ct) $next = 0;
$total_count = count($caption_array);
$result_final.= “<div id=’prevnext’>”;
$parameters_next = array(
‘cname’ => $cname,
‘pcaption’ => $caption_array[$next]
);
$parameters_prev = array(
‘cname’ => $cname,
‘pcaption’ => $caption_array[$prev]
);

$categoryNamePrev = $cname;
$photoCaptionNext = $caption_array[$next];
$photoCaptionPrev = $caption_array[$prev];

if($q !=” && $searchNext==$ct ) {

$previousUrl = ‘photos/’.$categoryNamePrev.’/’.$photoCaptionPrev.’/’.$q.’/’;
$nextUrl = ‘/searchmyway.php?q=’.$q.’&form_id%3Asearch=1′;
} else {

if($q != ”) {
$previousUrl = ‘photos/’.$categoryNamePrev.’/’.$photoCaptionPrev.’/’.$q.’/’;
$nextUrl = ‘photos/’.$categoryNamePrev.’/’.$photoCaptionNext.’/’.$q.’/’;
} else {
$previousUrl = ‘photos/’.$categoryNamePrev.’/’.$photoCaptionPrev.’/’;
$nextUrl = ‘photos/’.$categoryNamePrev.’/’.$photoCaptionNext.’/’;
}
}

$result_final.= ‘<span class=”prev”><a href=”‘.$previousUrl.'”><img src=”/photos/assets/left.png” alt=”left navigation”></a></span>’;
$result_final.= ‘<span class=”next”><a href=”‘.$nextUrl.'”><img src=”/photos/assets/right.png” alt=”right navigation”></a></span>’;
$result_final.= “</div>”;
}
}

$key = array_search($pcaption, $caption_array);
$prev = $key – 1;
if ($prev < 0) $prev = $ct – 1;
$next = $key + 1;
$searchNext = $key + 1;
$parameters_next = array(
‘cname’ => $cname,
‘pcaption’ => $caption_array[$next]
);
$parameters_prev = array(
‘cname’ => $cname,
‘pcaption’ => $caption_array[$prev]
);

if($q != ”) {
$backString = ‘<a href=”/searchmyway.php?q=’.$q.’&form_id%3Asearch=1″>Back</a>’;
} else {
$backString = ”;
}

if($q == ”) {
$result_final.= ‘<div class=”limage”><table><tr><td><table class=”image”><tr><td><a href=”photos/’.$cname.’/’.$caption_array[$next].’/”><img src=”‘ . $images_dir . ‘/’ . $photo_filename . ‘” alt=” ‘ . $photo_keywords . ‘”></a><div class=”caption”>’ . $photo_caption . ‘</div><div class=”excerpt”>’ . $photo_description . ‘</div></td></tr></table></td></tr></table><div class=”underline”></div></div>’.$backString.'</div>’;
} else {
$result_final.= ‘<div class=”limage”><table><tr><td><table class=”image”><tr><td><a href=”photos/’.$cname.’/’.$caption_array[$next].’/’.$q.’/”><img src=”‘ . $images_dir . ‘/’ . $photo_filename . ‘” alt=” ‘ . $photo_keywords . ‘”></a><div class=”caption”>’ . $photo_caption . ‘</div><div class=”excerpt”>’ . $photo_description . ‘</div></td></tr></table></td></tr></table><div class=”underline”></div></div>’.$backString.'</div>’;
}[/CODE]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 29.2014 — Well, it's because $next is equal to 3 at that point, but there is no array key 3 in $caption_array. Since it looks like $next is just set to whatever the current $key value is plus 1, it would seem you are making an incorrect assumption that there is another array element after the one referenced by $key. So, you either need to put in some defensive code (e.g. using isset()) before trying to reference $caption_array[$next], or else rethink your logic and code structure.
×

Success!

Help @yathrakaaran spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...