0

I am trying to display random banner each time the user refresh the page. The problem I am facing is that first banner to be displayed again and the banner fetch from data base. I am biegner in php..so suggested the code for banner refresh.

skaffman
  • 398,947
  • 96
  • 818
  • 769

1 Answers1

0

You could use PHP's rand function, setting the minimum to 0 and the maximum to the number of rows - 1, which would be used to select the banner at random.

Here's how I would do it, assuming that you're using a MySQL database (obviously you'll need to replace the MySQL parameters with your own):

$conn = mysql_connect("localhost", "username", "password");
$db = mysql_select_db("database_name_here", $conn);
$query = mysql_query("SELECT * from banner_table);
$max = mysql_num_rows($query) - 1;
$image = mysql_result($query, rand(0, $max), "Image_Url_Column");

Then wherever your image is contained:

<img src="<?php echo $image; ?>" alt="Banner image" />

Or if you're outputting the whole element in PHP:

echo "<img src=\"" . $image . "\" alt=\"Banner image\" />";

Update: If 3 banners are displayed at the same time, perhaps you could do something like this:

$conn = mysql_connect("localhost", "username", "password");
$db = mysql_select_db("database_name_here", $conn);
$query = mysql_query("SELECT * from banner_table);
$rows = mysql_num_rows($query);

$bannerToRetrieve; //the banner (database row number) to be retrieved from database
$alreadyRetrieved = array(); //holds values of previous numbers generated by rand() so the same banner isn't output again

for($i = 0; $i < 3; $i++)
{

    //Only set $bannerToRetrieve to a row that hasn't already been called (stored in $alreadyRetrieved)
    do
    {
        $bannerToRetrieve = rand(0, $rows - 1);
    }
    while(in_array($bannerToRetrieve, $alreadyRetrieved)); //if number is in array, it will generate another number

    $image = mysql_result($query, $bannerToRetrieve, "Image_Url_Column");
    echo "<img src=\"" . $image . "\" alt=\"Banner image\" />";

    $alreadyRetrieved[] = $bannerToRetrieve;
}

And the $image variable represents the file name or perhaps URL of the image file to be loaded e.g. "banner1.png". Is this how you have designed the system?

James Wright
  • 3,000
  • 1
  • 18
  • 27
  • thank you for reply.But in my website's home page banners come from data base...and in middle part(width 80%) at a time 3 banner displayed.so I want to do that when I refresh all of 3 banner change.and that banner show after 1 hours..and why are you use $image variable..whats its reason,I can't understand..plz explain..its urgent. – Alica_Casaligi May 07 '12 at 11:35
  • @Alica_Casaligi I have modified my answer, see above. – James Wright May 08 '12 at 10:04
  • @Alica_Casaligi Great! :) Can you please click the tick next to my comment and vote it up? Thank you. – James Wright May 08 '12 at 12:21
  • I have another problem..that is,I want to do,when I put banner code in mu banner field then banner width and hight automatically insert according to banner code..plz suggested php code.. – Alica_Casaligi May 15 '12 at 12:17
  • @Alica_Casaligi You could apply height and width attributes to the image tag: `echo "\"Banner";` If that has solved your problem, can you please accept my answer (click the green tick), otherwise let me know. – James Wright May 15 '12 at 15:15
  • no..my banner come from database...and when I add banner in add from(do that using insert query) its goes in db...and my problem is that when I add banner in next input box is width and hight,so in that box automatically insert width and hight,according to banner html code.. – Alica_Casaligi May 18 '12 at 11:57
  • @Alica_Casaligi I don't understand, could you please elaborate further? – James Wright May 24 '12 at 09:10