Forum Moderators: coopster

Message Too Old, No Replies

Need help with php and arrays

Trying to make an ad serving application for my website in php

         

gilahacker

1:07 am on Oct 28, 2004 (gmt 0)

10+ Year Member



ok, here goes:

I've got a news website that has a few spots for ads on each page and I've got several advertisers. I'm trying to create a little php goober to handle the adserving.

I've got spots for 4 ads. Lets call them: banner, content ad, mini-banner, and logo ad

What the script needs to do:
randomly pick an advertiser for each of the 4 types of ads making sure that we're using a different advertiser for each ad (a recursive function would be fine)

the function needs to output 4 variables, lets say: $banner, $content, $mini, and $logoad

these variables will look something like:
$banner = "<a href=$url><img src=$bannerimg alt=$alttext></a>";

so that I can just put <?php echo $banner?> in my html where the ad should go.

Ideally I'd like to have seperate files for each advertiser that look something like this:
$alttext = "Advertiser Number 1";
$advertiser_1[link] = "http://www.webmasterworld.com";
$banner_img = "ads/advertiser_1_banner.png";
$content_img = "ads/advertiser_1_content.png";
$mini_img = "ads/advertiser_1_mini.png";
$logoad_img = "ads/advertiser_1_logoad.png";

and be able to add a new advertiser by creating a new file and adding a line to the main php file to include that advertisers file.

Have I lost you yet? Hope not, feel free to ask questions.

as you can see I pretty well know what needs to be done but my php knowledge is very basic and I could really use some help. I want to keep this script as simple as possible so that I can make changes to it as needed.

Any suggestions are greatly appreciated, Thanks!

-Jason

gilahacker

1:23 am on Oct 28, 2004 (gmt 0)

10+ Year Member



i.e.

(this is nowhere near real code, just me brainstorming)

advertisers = array(
"advertiser1.inc",
"advertiser2.inc",
"advertiser3.inc",
"advertiser4.inc",
"advertiser5.inc");
(something like this so I can easily add/remove advertisers with one line)

randomly choose one of the advertisers and grab the banner img, alt text, and url and store them in $bannerad

like I said above, something like $banner="<a href=$url><img src=$bannerimg alt=$alttext></a>";

randomly choose one of the advertisers and make sure it hasn't been chosen before then grab the content image, alt text, and url and store them in $contentad

etc. etc.

note:

-the alt text and url are going to be the same for an advertiser, no matter which of their ads (banner, content, mini-banner, or logo ad) gets picked.

-there are only 4 spots for ads, so not every advertiser is going to be shown every time

dreamcatcher

8:35 am on Oct 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why not use array_rand().

This is taken directly from the PHP.net website

$banners[0]['imagen']="imagen0.gif";
$banners[0]['url']="www.nosenada.tal";

$banners[1]['imagen']="imagen1.gif";
$banners[1]['url']="www.nose.tal";

$banners[2]['imagen']="imagen2.gif";
$banners[2]['url']="pagina.html";

$banners[3]['imagen']="imagen3.jpg";
$banners[3]['url']="../pagina.php";

$id_banner = array_rand($banners);

echo "Archivo:--".$banners[$id_banner]['imagen']. "<br />\n";
echo "URL:-----".$banners[$id_banner]['url']. "<br />\n";

Add as many slots as you need. If you are running a PHP version less than v2.0, you must seed the generator before you call array_rand(). After v2.0 it is done automatically. You could also use shuffle().

Just a thought.

:)

gilahacker

8:50 am on Oct 28, 2004 (gmt 0)

10+ Year Member



SWEET. I think you've solved it for me. I was trying to make it way more complex than I needed I guess.

Now I will just have to put in something that checks to make sure that the advertiser isn't already showing on one of the other ads. I think I can manage that.

Thanks A Lot!

-JL

dreamcatcher

9:58 am on Oct 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No problem, glad I could help. :)

gilahacker

11:44 pm on Oct 28, 2004 (gmt 0)

10+ Year Member



okay, I've done a lot since you got me that code but i'm having some trouble with the whole "make sure that advertiser isn't already being shown" code...

here's what I've got: ($id_banner and $id_logoad are already defined)

$id_contentad = array_rand($presidential_ad);
while ($id_contentad == $id_logoad)
{

___$id_contentad = array_rand($presidential_ad);
___while ($id_contentad == $id_banner)
___{
______$id_contentad = array_rand($presidential_ad);
___}
}
$url = $presidential_ad[$id_contentad]['url'];
$img = $presidential_ad[$id_contentad]['contentimg'];
$alttext = $presidential_ad[$id_contentad]['alttext'];
$presidential_contentad = "<a href='$url'><img src='$img' alt='$alttext' border='none'></a>";

(ignore the black lines, I used them to space my code).

The problem I'm having is that I am not getting a unique selection, sometimes I'm getting the same advertiser used for the banner or logo ads used for the content ad.

I'm using a single while loop for the banner ad (to make sure it doesn't select the same one as the logo ad) and that seems to be working fine. But nesting another while loop to check both values doesn't seem to be working...

also, this method seems to take a long time, anyone got a faster, easier way of doing this?

note: I'm currently using 5 advertisers in the array for it to choose from

dreamcatcher

1:08 am on Oct 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try just pulling one lot of data by specifying a limit clause for the array. Heres how I would generate a random banner using the previous code with an alt tag.


$banners[0]['imagen']="imagen0.gif";
$banners[0]['url']="www.nosenada.tal";
$banners[0]['alt']="Nosenada";

$banners[1]['imagen']="imagen1.gif";
$banners[1]['url']="www.nose.tal";
$banners[1]['alt']="Nose";

$banners[2]['imagen']="imagen2.gif";
$banners[2]['url']="pagina.html";
$banners[2]['alt']="Pagina";

$banners[3]['imagen']="imagen3.jpg";
$banners[3]['url']="../pagina.php";
$banners[3]['alt']="Pagina2";

shuffle($banners);
$id_banner = array_rand($banners, 1);

$showbanner = '<a href="$banners[$id_banner]['url']"><img src="$banners[$id_banner]['imagen']" border="0"><alt="$banners[$id_banner]['alt']"></a>';

echo $showbanner;

Hope that helps. The parameter in the array_rand will ensure only one block of data is displayed at a time.

:)