mike12255 Posted February 13, 2009 Share Posted February 13, 2009 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]]; } ?> Quote Link to comment Share on other sites More sharing options...
samshel Posted February 13, 2009 Share Posted February 13, 2009 you have 12 captions , where as only 11 URLs as 11th element ( i.e. 12th URL cannot be found, it is throwing the warning..) Quote Link to comment Share on other sites More sharing options...
mike12255 Posted February 13, 2009 Author Share Posted February 13, 2009 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> Quote Link to comment Share on other sites More sharing options...
Philip Posted February 13, 2009 Share Posted February 13, 2009 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. Quote Link to comment Share on other sites More sharing options...
mike12255 Posted February 13, 2009 Author Share Posted February 13, 2009 so what you wrote is the equivalent of what i wrote? Quote Link to comment Share on other sites More sharing options...
Philip Posted February 13, 2009 Share Posted February 13, 2009 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]]; } Quote Link to comment Share on other sites More sharing options...
mike12255 Posted February 13, 2009 Author Share Posted February 13, 2009 ok thanks, ill add it my way for now and play around with your way. thanks again. Quote Link to comment 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.