rymco Posted December 21, 2009 Share Posted December 21, 2009 I look after a website for a curling club and I'm always getting complaints from sponsors that their logo never makes it to the top viewable portion of the page (http://www.simcoecurlingclub.ca/sponsors.php). I'm looking for a way to randomly mix up the images so that I'm not "favoring" any specific sponsor. I'm definitely not a PHP savy user and basically just get by with my PHP skills. I've found some samples of displaying a random image on a page but I need to show all images on the page but mix them up... Can someone help me with some sample code? The images are stored in a single folder (images\sponsors) and are named according to the sponsor (eg. \images\sponsors\mcdonalds.png)... Quote Link to comment Share on other sites More sharing options...
aeroswat Posted December 21, 2009 Share Posted December 21, 2009 Maybe in your mysql statement use the following line SELECT * from tblSponsors ORDER BY random(); Quote Link to comment Share on other sites More sharing options...
rymco Posted December 21, 2009 Author Share Posted December 21, 2009 No MySQL involved... the sponsors HTML is static. Like I said, I don't know PHP well enough to do all the cool stuff. I'm basically just using PHP for <? inlcude('myfile'); ?> so I can have a generic template for header, footer, link lists, etc. and build my pages that way. Everything else is static HTML... it's served me well up to this point. Quote Link to comment Share on other sites More sharing options...
aeroswat Posted December 21, 2009 Share Posted December 21, 2009 No MySQL involved... the sponsors HTML is static. Like I said, I don't know PHP well enough to do all the cool stuff. I'm basically just using PHP for <? inlcude('myfile'); ?> so I can have a generic template for header, footer, link lists, etc. and build my pages that way. Everything else is static HTML... it's served me well up to this point. I see... Well that makes things a little more difficult. I guess what you could do is put them in a php array and then do a while loop until the array is empty. Every time you select a random section of the array which stores url's to the images. Once it selects that it echo's the html to display an image + the array value. Then delete it out of the array. That'd probably be the easiest way I can think of. I'll try to write the code up real fast. Quote Link to comment Share on other sites More sharing options...
aeroswat Posted December 21, 2009 Share Posted December 21, 2009 Try this $sponsors[0] = "http://www.images.com/sponsor1.jpg"; $sponsors[1] = "http://www.images.com/sponsor2.jpg"; etc... $count=0; echo "<table><tr>"; while(count($sponsors) > 0) { $count++; $rand=rand(0,count($sponsors)-1); if(($count % 3) == 0) { echo "</tr><tr>" . "<td><img src='" . $sponsors[$rand] . "'/></td>"; } else{ echo "<td><img src='" . $sponsors[$rand] . "'/></td>"; } unset($sponsors[$rand]); } echo "</tr></table>"; added table format Quote Link to comment Share on other sites More sharing options...
rymco Posted December 21, 2009 Author Share Posted December 21, 2009 I figured it would involve an array... I just wasn't sure what the code would look like. I'll work with your sample code and see what I can come up with. Thanks! Quote Link to comment Share on other sites More sharing options...
aeroswat Posted December 21, 2009 Share Posted December 21, 2009 I figured it would involve an array... I just wasn't sure what the code would look like. I'll work with your sample code and see what I can come up with. Thanks! No problem. Good luck and if it doesn't work out post here and I'll try to help ya more. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted December 21, 2009 Share Posted December 21, 2009 $path = "./graphics/"; $graphics = array(); $dir_handle = @opendir($path) or die("Unable to open $path"); while ($file = readdir($dir_handle)) { if($file == "." || $file == "..") continue; array_push($graphics,$file); } closedir($dir_handle); shuffle($graphics); foreach($graphics as $graphic) { echo "<img src=\"$path$graphic\">\n<br />"; } HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
rymco Posted December 21, 2009 Author Share Posted December 21, 2009 I've got the code in my page and the page is attempting to load but it freezes up and I have to kill my IE session... http://www.simcoecurlingclub.ca/sponsors_test.php My finished code: <? $sponsors[0]="images/sponsors/sponsor0.png"; $sponsors[1]="images/sponsors/sponsor1.png"; $sponsors[2]="images/sponsors/sponsor2.png"; ... ... $sponsors[59]="images/sponsors/sponsor59.png"; $sponsors[60]="images/sponsors/sponsor60.png"; $count=0; echo "<table border='0' cellpadding='2' cellspacing='4' width='100%'><tr>"; while(count($sponsors) > 0) { $count++; $rand=rand(0,count($sponsors)-1); if(($count % 3) == 0) { echo "</tr><tr>" . "<td class='sponsor'><img src='" . $sponsors[$rand] . "'/></td>"; } else { echo "<td class='sponsor'><img src='" . $sponsors[$rand] . "'/></td>"; } unset($sponsors[$rand]); } echo "</tr></table>"; ?> What am I doing wrong? Thanks for the additional code teamatomic but I'll be fine with the array method because I'll be adding a url array once I get this working... Quote Link to comment Share on other sites More sharing options...
aeroswat Posted December 21, 2009 Share Posted December 21, 2009 It could be an infinite loop. Trying replacing the > 0 in the while loop to > 1... although that won't make much sense see if it works. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 21, 2009 Share Posted December 21, 2009 extending on what aeroswat did you can simply do this <?php $sponsors[0]="images/sponsors/sponsor0.png"; $sponsors[1]="images/sponsors/sponsor1.png"; $sponsors[2]="images/sponsors/sponsor2.png"; $sponsors[3]="images/sponsors/sponsor3.png"; shuffle($sponsors); echo "<table border='0' cellpadding='2' cellspacing='4' width='100%'><tr>"; $count = 0; foreach ($sponsors as $sponsor) { if(($count % 3) == 0) { echo "</tr><tr>" . "<td class='sponsor'><img src='" . $sponsor . "'/></td>"; } else { echo "<td class='sponsor'><img src='" . $sponsor . "'/></td>"; } $count++; } echo "</tr></table>"; ?> Quote Link to comment Share on other sites More sharing options...
rymco Posted December 21, 2009 Author Share Posted December 21, 2009 That works pretty good Rajiv... is there a way I can add the URL to this method... ie multi-dimensional array? EDIT: Nevermind... I found it. $sponsors[0][0]="images\sponsors\sponsor0.php" $sponsors[0][1]="http:\\www.mywebsite.com" Thanks for all the great and quick help everyone!!! Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted December 21, 2009 Share Posted December 21, 2009 extending on what aeroswat did you can simply do this <?php $sponsors[0]="images/sponsors/sponsor0.png"; $sponsors[1]="images/sponsors/sponsor1.png"; $sponsors[2]="images/sponsors/sponsor2.png"; $sponsors[3]="images/sponsors/sponsor3.png"; shuffle($sponsors); echo "<table border='0' cellpadding='2' cellspacing='4' width='100%'><tr>"; $count = 0; foreach ($sponsors as $sponsor) { if(($count % 3) == 0) { echo "</tr><tr>" . "<td class='sponsor'><img src='" . $sponsor . "'/></td>"; } else { echo "<td class='sponsor'><img src='" . $sponsor . "'/></td>"; } $count++; } echo "</tr></table>"; ?> shortened it up a bit: <?php $range = range (0, 60); shuffle ($range); echo '<table border="0" cellpadding="2" cellspacing="4" width="100%"><tr>'; for ($i=0; $i<=count($range); $i++) { if (($i % 3) == 0) { echo '</tr><tr><td class="sponsor"><img src="images/sponsers/sponser' . $i . '.png" /></td>'; } else { echo '<td class="sponsor"><img src="images/sponsers/sponser' . $i . '.png"/></td>'; } } echo '</tr></table>'; ?> Quote Link to comment Share on other sites More sharing options...
rymco Posted December 21, 2009 Author Share Posted December 21, 2009 Thanks for the reply mrMarcus... I change the sponsorX.png file names for the purpose of posting the code only. 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.