bronzemonkey Posted August 17, 2007 Share Posted August 17, 2007 I've been using the code below but it is throwing up "PHP Notice: Undefined offset". My pages are going to display varying numbers of photos and if I don't provide a full set of 8 photos then I get php notices. I tried using if(isset()) preceding 'echo' at the bottom of the code but got nowhere. How can I displaying varying quantities of information without generating these php notices? Thanks <?php $page = $_SERVER["PHP_SELF"]; if(strpos($page, 'page1.php') !== false) { $pic [1] = '<a href="#"><img src="#" alt="" /></a>'; $pic [2] = '<a href="#"><img src="#" alt="" /></a>'; $pic [3] = '<a href="#"><img src="#" alt="" /></a>'; $pic [4] = '<a href="#"><img src="#" alt="" /></a>'; } elseif(strpos($page, 'page2.php') !== false) { $pic [1] = '<a href="#"><img src="#" alt="" /></a>'; $pic [2] = '<a href="#"><img src="#" alt="" /></a>'; $pic [3] = '<a href="#"><img src="#" alt="" /></a>'; $pic [4] = '<a href="#"><img src="#" alt="" /></a>'; $pic [5] = '<a href="#"><img src="#" alt="" /></a>'; $pic [6] = '<a href="#"><img src="#" alt="" /></a>'; $pic [7] = '<a href="#"><img src="#" alt="" /></a>'; $pic [8] = '<a href="#"><img src="#" alt="" /></a>'; } ?> <div id="photos"> <h4>Photos</h4> <ul> <li><?php echo $pic[1]; ?></li> <li><?php echo $pic[2]; ?></li> <li><?php echo $pic[3]; ?></li> <li><?php echo $pic[4]; ?></li> <li><?php echo $pic[5]; ?></li> <li><?php echo $pic[6]; ?></li> <li><?php echo $pic[7]; ?></li> <li><?php echo $pic[8]; ?></li> </ul> </div> Quote Link to comment https://forums.phpfreaks.com/topic/65473-solved-undefined-offset-solutions/ Share on other sites More sharing options...
wildteen88 Posted August 17, 2007 Share Posted August 17, 2007 Change this code: <li><?php echo $pic[1]; ?></li> <li><?php echo $pic[2]; ?></li> <li><?php echo $pic[3]; ?></li> <li><?php echo $pic[4]; ?></li> <li><?php echo $pic[5]; ?></li> <li><?php echo $pic[6]; ?></li> <li><?php echo $pic[7]; ?></li> <li><?php echo $pic[8]; ?></li> into a loop: for($i = 1; $i < count($pic); $i++) { echo '<li>' . $pic[$i] . "</li>\n "; } Quote Link to comment https://forums.phpfreaks.com/topic/65473-solved-undefined-offset-solutions/#findComment-326907 Share on other sites More sharing options...
bronzemonkey Posted August 17, 2007 Author Share Posted August 17, 2007 Thank you for this, it works except for one small problem - the last picture in the array doesn't display. On a page that has 4 photos only 3 are showing up. Quote Link to comment https://forums.phpfreaks.com/topic/65473-solved-undefined-offset-solutions/#findComment-327001 Share on other sites More sharing options...
wildteen88 Posted August 17, 2007 Share Posted August 17, 2007 Change this line: for($i = 1; $i < count($pic); $i++) to: for($i = 1; $i <= count($pic); $i++) Quote Link to comment https://forums.phpfreaks.com/topic/65473-solved-undefined-offset-solutions/#findComment-327005 Share on other sites More sharing options...
dbo Posted August 17, 2007 Share Posted August 17, 2007 I'd suggest storing the lenght of the array in a variable rather than calling the function each time. $len = count($pic); try starting your loop at 0 instead of 1. Quote Link to comment https://forums.phpfreaks.com/topic/65473-solved-undefined-offset-solutions/#findComment-327008 Share on other sites More sharing options...
wildteen88 Posted August 17, 2007 Share Posted August 17, 2007 I'd suggest storing the lenght of the array in a variable rather than calling the function each time. $len = count($pic); try starting your loop at 0 instead of 1. bronzemonkey's pic array starts at 1. Thats why I start the loop at 1. Calling count each time wont have much of a difference either (parsing time wise). However I get what you mean though. Quote Link to comment https://forums.phpfreaks.com/topic/65473-solved-undefined-offset-solutions/#findComment-327012 Share on other sites More sharing options...
dbo Posted August 17, 2007 Share Posted August 17, 2007 I knew why you posted what you did. Just think that since arrays are zero indexed they should be used as such. And yeah, on small inputs the count thing isn't noticable, but on larger inputs and taking these considerations across your code I think you'll see improvements. Quote Link to comment https://forums.phpfreaks.com/topic/65473-solved-undefined-offset-solutions/#findComment-327016 Share on other sites More sharing options...
bronzemonkey Posted August 17, 2007 Author Share Posted August 17, 2007 Is it more correct to begin my array at 0? I'm sure there is a better way than storing all the html for photos/quotes/links etc in an array...but I am new to php so this is the best I have come up with so far! Before I read you replies I tried out this code, which also seems to avoid the php notices. But is there a reason neither of you recommended it over your suggestions? <? foreach ($pic as $value) { echo '<li>' . $value . '</li>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65473-solved-undefined-offset-solutions/#findComment-327018 Share on other sites More sharing options...
dbo Posted August 17, 2007 Share Posted August 17, 2007 foreach loop is fine as well, but I'd absolutely index my arrays at 0 instead of 1 Quote Link to comment https://forums.phpfreaks.com/topic/65473-solved-undefined-offset-solutions/#findComment-327021 Share on other sites More sharing options...
wildteen88 Posted August 17, 2007 Share Posted August 17, 2007 There are many ways for looping through arrays. I just used one of the methods. Quote Link to comment https://forums.phpfreaks.com/topic/65473-solved-undefined-offset-solutions/#findComment-327024 Share on other sites More sharing options...
bronzemonkey Posted August 17, 2007 Author Share Posted August 17, 2007 OK. Thanks very much for your quick replies and help...much appreciated. I think everything has been solved now. Quote Link to comment https://forums.phpfreaks.com/topic/65473-solved-undefined-offset-solutions/#findComment-327028 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.