Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

Selecting Random Images/Banners & Displaying Them

images php coding issues or problems here.

Moderators: macek, egami, gesf

Selecting Random Images/Banners & Displaying Them

Postby JeffNY » Tue Jun 21, 2011 8:35 pm

Hi All,

I do our motorcycle club web site (for free hehe), and I am looking for a way to show the "Sponsor Banners" on this page in random order, so as not to show favoritism to any one sponsor:
http://www.statelineriders.org/sponsors.htm

I would also like to write some php code to randomly pick 4 or 6 Sponsor Banners and mix them into the clubs main index page and our "Motorcycle News Page" (random 2 banners side by side in 2 or 3 locations, between sections on the pages)
http://www.statelineriders.org/motorcyclenews.htm

….Sponsors pay $10 a month for a Banner so the more people can see it, the more value it has to the sponsor (and the club). I'd like to eventually have 20 to 30 sponsor banners, all of which goes to help our "land fund" so the club can hopefully buy land someday. Land for the club to ride on is expensive, so every little bit towards that, or a mortgage payment, helps :)

I am just now starting to learn php, and how it integrates with html web pages. But any help very much appreciated...

Thanks,
Jeff
JeffNY
New php-forum User
New php-forum User
 
Posts: 9
Joined: Tue Nov 16, 2010 7:01 pm

Re: Selecting Random Images/Banners & Displaying Them

Postby egami » Wed Jun 22, 2011 7:19 am

I am a fellow motorcycle enthusiast. I ride a 2002 Suzuki Hayabusa w/ 56k miles on it.

first things first, how do you store your banner id's/pictures?
Are they in a database? just in a directory?

One thing you don't want to do is have the same banner displayed twice on a page. That's bad form.

So,
You'll create an array of your randomly selected banners. You'll have to loop the array several times to make sure there aren't any duplicates.. but if you have a large enough seed, there shouldn't be any doubles.

But before I can create a sniplet, i need to know how you store the information.
Also, do you keep track of how many people click on the ads?
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Selecting Random Images/Banners & Displaying Them

Postby JeffNY » Wed Jun 22, 2011 10:54 am

>>Are they in a database? just in a directory?<

They are kept in the directory:
http://statelineriders.org/sponsors

All banners are the same size.


>>Also, do you keep track of how many people click on the ads?<<

I currently don't. But that would be a nice info to be able to give sponsors. I suppose I could get that info from our web stats? In fact, the first couple months we started this and I put up the Edelmanns banner to see what kind of response we'd get for Tim. I'd have to look back at our emails, but the number of times people clicked the banner was pretty good. And Tim and I did the math, for the $10, the cost per-click was about half what he was paying for a commercial ad somewhere. But that is the idea too, it helps the club, but also helps our sponsors get at least the cost of the banner back in new business, hopefully several times that.

The idea of the Motorcycle News Page was to help the web site always have new content and great motorcycle stories for people to read when they come to the site….and also be a place to show off our sponsor banners, to hopefully create a revenue stream to the club…to go towards buying land.

We have some great club members that donate a ton of time and work to the club, and ask for nothing in return, except for maybe a pat on the back. We have some great sponsors too (I'll be adding a couple more shortly too). I help one of them with their web site (again for "free"), kinda in exchange for buying a banner, but whenever I see him he's like "What do you need?'

Do you ever get over this way? You are always welcome at a club meeting…and some free pizza ;)

Thanks for the help!
Jeff
JeffNY
New php-forum User
New php-forum User
 
Posts: 9
Joined: Tue Nov 16, 2010 7:01 pm

Re: Selecting Random Images/Banners & Displaying Them

Postby JeffNY » Wed Jun 22, 2011 11:14 am

By the way, the club web site now (and for some time now) has over 20,000 hits a month, and 3100 to 3800 "visits" a month.

I may not the smartest guy when it comes to doing web sites, but I try :D

I am trying to be better about updating the Motorcycle News Page, I am way behind since the last update there, just got side tracked here with "projects" :( (my goal was/is to update that page with new stories every 7 to 10 days...)
JeffNY
New php-forum User
New php-forum User
 
Posts: 9
Joined: Tue Nov 16, 2010 7:01 pm

Re: Selecting Random Images/Banners & Displaying Them

Postby egami » Wed Jun 22, 2011 11:45 am

Code: Select all
<?

$directory = '/path/to/sponsor/directory';
$file_array = scandir($directory);
unset(
$file_array['0']);
unset(
$file_array['1']);

$set_sponsors = '3'; // really, it's 4 because 0 is a number.. 

$array = array();
$max = count($file_array);

while (count($array) <= $set_sponsors) { 
  $random 
= mt_rand(1,$max);
  $sponsor = $file_array[$random];
  if (!isset($array[$sponsor])) { 
    $array
[$sponsor] = $sponsor;
  }
}

echo '<pre>'; print_r($array); echo '</pre>';
 
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Selecting Random Images/Banners & Displaying Them

Postby egami » Wed Jun 22, 2011 11:50 am

A more convenient way to write it would be this way, so that you can call it by $array['0'], $array['1'], etc..

Code: Select all

$directory 
= '/path/to/sponsor/directory';
$file_array = scandir($directory);
unset(
$file_array['0']);
unset(
$file_array['1']);

$set_sponsors = '3'; // really, it's 4 because 0 is a number.. 

$array = array();
$max = count($file_array);

while (count($array) <= $set_sponsors) { 
  $random 
= mt_rand(1,$max);
  $sponsor = $file_array[$random];
  if (!in_array($sponsor,$array)) { 
    $array
[] = $sponsor;
  }
}

echo '<pre>'; print_r($array); echo '</pre>';
 
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Selecting Random Images/Banners & Displaying Them

Postby egami » Wed Jun 22, 2011 11:56 am

Now, before you go off and use this little generator..
Let me explain a few things to you.

This has no security wrapped around it, so if by chance your site gets hacked, or someone is able to put "images" into your directory, well.. all bets are off.

You can wrap some security around it, by looking for file types, meta content, etc..
this is just a quick and really dirty way of doing things to get your feet up out of water. :)

Just make sure that the sponsors directory is safe from uploads, changes, moves, etc..
ie.. no chmod 777 filename, stuff.

Anyway, cya! good luck on your adventures.
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT


Return to PHP coding => Images

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.