Home     Articles & Projects     Products & Web Services     Forum

Basic question

Hi David,

Forgive my Numptyness here, but Just trying to learn php basics

If I was trying to show one banner randomly each time my homepage was opened, would the following work at all? or am I miles off?

<?php
$image1
= "img src='http.wwww.mydomain/images/banner1.jpg'";
$image2 = "img src='http.wwww.mydomain/images/banner2.jpg'";
$image3 = "img src='http.wwww.mydomain/images/banner3.jpg'";

$randimages = array("$image1", "$image2", "$image3");

$splash = array_rand($randimages);

{

  echo
"$splash";

}
?>

Thanks for any help you can offer

Regards

Paul

Can I ask why you don't use

Can I ask why you don't use MYSQL for this?
One it would be easier (i think)
Plus I think that it would help when you grow the number of banners.

If you used MYSQL the php code would be something like this

<?php
$sql
= "SELECT img FROM images ORDER BY RAND";
$result = mysql_query($sql) or die ("Error Searching");
$row = mysql_fetch_assoc($result);

print
"<img src='".$row['img']."' />";
?>

Thanks for your response, as

Thanks for your response, as I said just grasping the basics, and by the looks of it not even doing that...

I guess putting into a database would be more practical and using an sql query to ramdomise the output.

TBH hadn't expected to grow the amount of banners to anymore than 3 as they often have limited shelf life, so thought that a database with 3 urls in would be largely superfluous? Though thinking about it I would need to put anchor links to associate with the images.

So is it worth having a database with 2 columns in img url and link url?

Thnaks again for your reply

Paul

Yes, I think a database is

Yes, I think a database is the best way to do it, as if you have an admin system built later on, the person who does it, can make an upload script to upload new banner, delete old ones, and update the database.

If you need any help just ask.
We are all here to learn and help.

I do use David's Price

I do use David's Price Tapestry script, so in a way I already have an admin system. However I feel for me to learn my objective is to work out how to do something for myself rather than pick through something as complex as PT...

So can you point me in the right direction please?

I need to insert details into a new sql database, so where would you suggest I start?

Sorry for being so back to basics, hence the open questions?

Regards

Paul

Hi Paul, A database would of

Hi Paul,

A database would of course be a good way to develop your script in the future; but in terms of your code above; it's almost there. Have a go with something like:

<?php
$images
= array();
$images[] = "http.wwww.mydomain/images/banner1.jpg";
$images[] = "http.wwww.mydomain/images/banner2.jpg";
$images[] = "http.wwww.mydomain/images/banner3.jpg";

echo
"<img src='".$images[array_rand($images)]."' />";
?>

(array_rand() actually returns an index into an array rather than an array entry itself)

Taking it a step further; you might want to have different click through URLs associated with each banner, so you could also store that in a multi-dimensional array, like this:

<?php
$images
= array();
$images[0]["src"] = "http://www.mydomain/images/banner1.jpg";
$images[0]["url"] = "http://www.example.com/";
$images[1]["src"] = "http://www.mydomain/images/banner2.jpg";
$images[1]["url"] = "http://www.example.net/";
$images[2]["src"] = "http://www.mydomain/images/banner3.jpg";
$images[2]["url"] = "http://www.example.org/";
$key = array_rand($images);
echo
"<a href='".$images["key"]["url"]."'><img src='".$images[$key]["src"]."' /></a>";
?>

The PHP documentation for arrays is a good introduction...

http://www.php.net/manual/en/language.types.array.php

Hope this helps,
Cheers,
David.

Hi David, Thanks so much for

Hi David,

Thanks so much for clearing that up, appreciate your help.

Ordinarily, would the above solution be typical for such a small selection of objects? or is best practise to put them into a datbase or doesn't it matter?

Regards

Paul

Hi Paul, If the number of

Hi Paul,

If the number of banners was going to grow significantly a database would definitely be the way to go; however don't forget that there are very well established (and very good) open source PHP banner / advertising inventory management scripts.

For example OpenX (used to be phpAdsNew)

http://www.openx.org/ad-server

The "Open X Community Download" is a PHP script that gives you a full management interface to manage your banner ads (and any other type of advertising); and then it just gives you a snippet of HTML to place in your web pages wherever you want a particular banner (or random banner from a pool) to appear. Hundreds (probably thousands) of affiliate marketers use OpenX to manage their banner ads across their sites.

Thanks Once again

Thanks Once again David,

Thanks for the link, its nice to know these tools are available, though I would like to learn a little more about php so I can put together basic scripts myself, If I understand how it fuctions I can appreciate more fully the capabilities of say Price Tapestry for example (and carry out the basic stuff myself without pestering you as much as I do)

Cheers!

Paul

Hi David, What was that

Hi David,

What was that about pestering you less?

with reference to previous code, I've got it to work, but the affiliate links associated with the image do not work? can you hazaerd a guess why? the code I've used is as follows:

{code saved}

The images show OK, but the link through to the merchant don't?

Thanks once again

Hi Paul, You

Hi Paul,

You have:

$images["key"]["url"]

...it should be:

$images[$key]["url"]

That should be all it is...!

Cheers,
David.

Hi David, It seems so

Hi David,

It seems so obvious now that you pointed it out!

All works well now, Thanks a lot for your triple A support yet again

Paul