Jump to content

Jumble Sponsor Images


rymco

Recommended Posts

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)...

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!!!

Link to comment
Share on other sites

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

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.