Jump to content

[SOLVED] Undefined offset - solutions?


bronzemonkey

Recommended Posts

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>

Link to comment
Share on other sites

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            ";
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.