Jump to content

[SOLVED] I've never see this error/notice before


mike12255

Recommended Posts

So i got the following error:

 

Notice: Undefined offset: 11 in /home/sgiclan/public_html/gnomes/index.php on line 414

57 d align="center">

 

im not sure what the error means, but i know something is wrong because only one image appears and all the words are the same.

 

    <?php
		$randoms = array();
$start =1;
$total =11;
		while (count($randoms) < 3) {
  $random = mt_rand($start,$total);
  if (!in_array($random, $randoms)) {
    $randoms[] = $random;
  }
}
$captions = array("RBC","Prodomax","Liews Motors","Advanced Motion Controls","Burnside","TD Canada Trust","Reinhart","SCDSB","ARO","Stayner Lions","test","ComTek");
$urls = array("http://www.royalbank.com", "http://prodomax.com/","http://a-m-c.com/","http://www.rjburnside.com/","http://tdcanadatrust.com","http://www.reinhartfoods.com/", "http://www.scdsb.on.ca/", "http://www.aronet.com/", "http://www.lionsclubs.org/","test","http://www.comtekcomputers.com/");

for ( $i = 0; $i < 3; $i++){

  $caps = $captions[$randoms[$i]];;
  $img = $urls[$randoms[$i]];
}

?>

Link to comment
Share on other sites

yeah, i found the one i was missing and added it in though, but still got errors as can be see at (http://sgi-clan.info/gnomes/index.php under the "some sponsers" header)

 

my problem is im still only getting 1 caption to appear and all three urls that appear are the same. + before the first image/caption appears it shows this:

 

d align="center">

 

Here is the code for the table:

 

<table width="100%" border="0" cellpadding="0" cellspacing="0" class="blackbg">
        <tr>
          <td><img src="images/black3.jpg" width="3" height="3" /></td>
          <td align="right"><img src="images/black4.jpg" width="3" height="3" /></td>
        </tr>
      </table>
</td>
    <td width="190"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><img src="images/modifiedfinalsliced_138.gif" width="177" height="25" /></td>
      </tr>
      <tr>
        <td class="sponsorbg"><table width="90%" border="0" align="center" cellpadding="0" cellspacing="5">
            <tr>
              <td align="center"> </td>
            </tr>
            <tr>
            <?php
		$randoms = array();
$start =1;
$total =11;
		while (count($randoms) < 3) {
  $random = mt_rand($start,$total);
  if (!in_array($random, $randoms)) {
    $randoms[] = $random;
  }
}
$captions = array("RBC","Prodomax","Liews Motors","Advanced Motion Controls","Burnside","TD Canada Trust","Reinhart","SCDSB","ARO","Stayner Lions","test","ComTek");
$urls = array("http://www.royalbank.com", "http://prodomax.com/","http://www.lewismotorsinc.com/","http://a-m-c.com/","http://www.rjburnside.com/","http://tdcanadatrust.com","http://www.reinhartfoods.com/", "http://www.scdsb.on.ca/", "http://www.aronet.com/", "http://www.lionsclubs.org/","test","http://www.comtekcomputers.com/");

for ( $i = 0; $i < 3; $i++){

  $caps = $captions[$randoms[$i]];;
  $img = $urls[$randoms[$i]];
}

?>


          <td align="center"><img src="images/random/<?php echo $random.".jpg" ?>" height="25"/> </td>
            </tr>
            <tr>
              <td align="center"><a href="<?php echo $img ?>" class="link"><?=$caps?></a></td>
            </tr>
            <tr>
              <td align="center" class="dotline"> </td>
            </tr>
            <tr>
              <td align="center"><img src="images/random/<?php echo $randoms.".jpg" ?>"  height="25" /></td>
            </tr>
            <tr>
              <td align="center"><a href="<?php echo $img ?>" class="link"><?php echo $caps ?></a></td>
            </tr>

            <tr>
              <td align="center" class="dotline"> </td>
            </tr>
            <tr>
              d align="center"><img src="images/random/<?php echo $randoms.".jpg" ?>"  height="25" /></td>
            </tr>
            <tr>
              <td align="center"><a href="<?php echo $img ?>" class="link"><?php echo $caps ?></a></td>
            </tr>
        </table></td>
      </tr>

Link to comment
Share on other sites

The reason it wasn't working, is because you're calling $caps, $img, $randoms outside the loop, so it'll just show the last element.

for ( $i = 0; $i < 3; $i++){

  $caps = $captions[$randoms[$i]];;
  $img = $urls[$randoms[$i]];
}

I could set $caps equal to 5, then 3, and finally 2. However, when you call the variable outside the loop, the only one that you can see is 2 since you overwrote the others.

 

I played around with your code a bit. It won't work to your full intents, i.e. there is no dashed line, and you'd have to rename your images probably. Nonetheless, it'd be a lot easier to expand your list later on:

<?php
// Create image array
$imageArray = array(
		'RBC' => 'http://www.royalbank.com',
		'Prodomax' => 'http://prodomax.com/',
		'Liews Motors' => 'http://www.lewismotorsinc.com/',
		'Advanced Motion Controls' => 'http://a-m-c.com/',
		'Burnside' => 'http://www.rjburnside.com/',
		'TD Canada Trust' => 'http://tdcanadatrust.com',
		'Reinhart' => 'http://www.reinhartfoods.com/',
		'SCDSB' => 'http://www.scdsb.on.ca/',
		'ARO' => 'http://www.aronet.com/',
		'Stayner Lions' => 'http://www.lionsclubs.org/',
		'test' => 'test',
		'ComTek' => 'http://www.comtekcomputers.com/',
	);

// Select a few items
$imageRand = array_rand($imageArray, 3);
// Run the loop
foreach($imageRand as $name) {
	echo '<tr><td align="center"><a href="',$imageArray[$name],'" class="link">',$name,'</a></td></tr>';	
?>

 

By the way, with the code you gave, there were several minor things wrong with it. "$caps = $captions[$randoms[$i]];;" - double semicolons, your table code isn't lining up (I had a hard time finding the matching opening closing cell & row tags), yeah... Anyways, feel free to play around with what I gave you.

 

 

 

Link to comment
Share on other sites

Pretty much, it simplified the random process & will allow for easier expansion (and prevents the error you originally had - missing one of the captions/urls). The only thing it doesn't do is show the image, but that's just because of the difference in ways I would do the sponsor image (I'd use their name, instead of a number)

 

If you wanted yours to work, pretty much just put:

 

<tr>
              <td align="center"><img src="images/random/<?php echo $randoms.".jpg" ?>"  height="25" /></td>
            </tr>
            <tr>
              <td align="center"><a href="<?php echo $img ?>" class="link"><?php echo $caps ?></a></td>
            </tr>

 

Inside the loop:

for ( $i = 0; $i < 3; $i++){

  $caps = $captions[$randoms[$i]];;
  $img = $urls[$randoms[$i]];
}

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.