473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Random display of banners using php?

Hi to All,

Here i have the same image stored in the db with different id's and also exist some other images.

Here i have to display banner randomly i have taken imageid's and displayed randomly. Because of same image stored many time i have to wait long time(refresh the page) for change of image.

Here the issue is though if i have same image stored in the db many times i have to display unique image on every refresh of the page.( means previously displayed image should not display if i refresh the page instead fresh image should appear.)

please help me some one in solving this

Thanks in Advance

Ram
May 2 '08 #1
20 2468
coolsti
310 100+
I really do not understand what you mean with all the images stored in the database talk.

But let us say you have a series of images, and you wish one of them to appear in the banner when a user requests a page, and which one does appear is to be random.

Then all you have to do is name the images such that there is an integral counter in the name, for example let us say you have 5 images, so name them something like bimg1.jpg, bimg2.jpg, bimg3.jpg, bimg4.jpg, bimg5.jpg.

Then when the user makes a page request and calls your PHP script, you generate a random integer, here in this example limited between the values 1 and 5, and then you build up the name of the image using this value, e.g.

Expand|Select|Wrap|Line Numbers
  1. $imagename = 'bimg' . $randomnumber . '.jpg';
  2.  
and you build up the HTML that you send out to the user with the banner image pointing at this image.

You adjust this idea if your images are in a database rather than just available on the server as image files, but the idea should be the same.
May 2 '08 #2
ronverdonk
4,258 Expert 4TB
I sure hope coolsti got it right, because I have no clue what your problem or question is! If you want more support on this, please explain your problem or question in plain and clear english.

Ronald
May 2 '08 #3
Atli
5,058 Expert 4TB
Sounds to me like your trying to display a random image, but you don't want the same image to appear twice in a row?

If so you can simply store the image of the last image you display in the $_SESSION and have your random generator re-generate an ID if that ID comes up.

Something like:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Get the last number used
  3. //  @ silences the warning displayed if the session is empty
  4. $last = @$_SESSION['lastNumber'];
  5.  
  6. // Generate random number that is not the same as the last number
  7. do {
  8.   $next = mt_rand(0, 10);
  9. } while ($next == $last);
  10.  
  11. // Add the new number to the session and show an image.
  12. $_SESSION['lastNumber'] = $next;
  13. echo "<img src='image_nr_{$next}.jpg' />";
  14. ?>
  15.  
May 5 '08 #4
Markus
6,050 Expert 4TB
I think he's saying the same image is uploaded many times but with a different name. Therefore, the image is displayed multiple times because one cannot differentiate between the images.

Unfortunately i can see no way of comparing images - may be a phpgd trick?
May 5 '08 #5
Atli
5,058 Expert 4TB
I think he's saying the same image is uploaded many times but with a different name. Therefore, the image is displayed multiple times because one cannot differentiate between the images.

Unfortunately i can see no way of comparing images - may be a phpgd trick?
Why would an image get uploaded multiple times?

But if that is the case, a quick and dirty way to identify them would be to compare file sizes and dimensions, but that would most definitely return false positives every so often.

It would even be possible to sample the data from within the image at set intervals to see if they match.
May 5 '08 #6
TheServant
1,168 Expert 1GB
It would even be possible to sample the data from within the image at set intervals to see if they match.
Or doing that if it returns the same size/dimensions. But I am unsure how you could do that.
May 6 '08 #7
Atli
5,058 Expert 4TB
Or doing that if it returns the same size/dimensions. But I am unsure how you could do that.
You could use the Filesystem functions.

Somewhat like this:
Expand|Select|Wrap|Line Numbers
  1. function files_compare($file1, $file2, $numSamples=10) 
  2. {
  3.     // Check file size
  4.     if(filesize($file1) != filesize($file2)) {
  5.         return false;
  6.     }
  7.  
  8.     // Open streams
  9.     $fha = fopen($file1, "r");
  10.     $fhb = fopen($file2, "r");
  11.  
  12.     // Calculate offset
  13.     $offset = (filesize($file1) / ($numSamples + 1));
  14.  
  15.     // Get and check samples
  16.     for($i = 0; $i < $numSamples; $i++) {
  17.         // Seek forward
  18.         fseek($fha, $offset, SEEK_CUR);
  19.         fseek($fhb, $offset, SEEK_CUR);
  20.  
  21.         // Take samples
  22.         if(fread($fha, 16) != fread($fhb, 16)) {
  23.             return false;
  24.         }
  25.     }
  26.  
  27.     // Close streams
  28.     fclose($fha);
  29.     fclose($fhb);
  30.  
  31.     // All tests passed
  32.     return true;
  33. }
  34.  
May 6 '08 #8
TheServant
1,168 Expert 1GB
Very nice! Just wondering, what does offset mean in that code?
May 6 '08 #9
Atli
5,058 Expert 4TB
Very nice! Just wondering, what does offset mean in that code?
It's the amount of bytes between the samples.
In a 1000byte file, using 10 samples, it would jump 90 bytes forward into the files before taking each sample.
May 6 '08 #10
TheServant
1,168 Expert 1GB
It's the amount of bytes between the samples.
In a 1000byte file, using 10 samples, it would jump 90 bytes forward into the files before taking each sample.
So it takes a single byte as a sample? So for your example, why is it not an offset of 100 for 10 samples? For a 90 byte offset it would take samples at:
90, 180, 270, 360, 450, 540, 630, 720, 810, 900
Where as if it did 100 it would be:
100, 200, 300, 400, 50, 600, 700, 800, 900, 1000?
May 6 '08 #11
Markus
6,050 Expert 4TB
Why would an image get uploaded multiple times?
Hi to All,

Here i have the same image stored in the db with different id's and also exist some other images.
He said it, not me.

And i like your way of scanning the images.
May 6 '08 #12
coolsti
310 100+
I think everyone is making a mountain out of a molehill here :)

I understand what the OP was asking for because I had a similar setup on a small application I made. On the top was a banner with a background image instead of a solid color. To give some flare to the page, I did not use the same image as background image each time, but rather used a random one out of a total choice of 5 images. It was not important whether, randomly, the same image appeared twice in a row. But statistically, each page request would show a different image out of the 5, randomly.

And this is sooooo easy to implement, as I described above. I think the confusing issue is the OP talking about storing the images in a database, but if I understand correctly what is wanted to be done, storing them in a database is not necessary. But it could be done, just that the database index of the image to display is selected (with PHP) randomly, rather than just creating a string for the image name.
May 6 '08 #13
hsriat
1,654 Expert 1GB
You could use the Filesystem functions.

Somewhat like this:
Expand|Select|Wrap|Line Numbers
  1. function files_compare($file1, $file2, $numSamples=10) 
  2. {
  3.     //......................
  4. }
  5.  
If the images are already stored in the database, why should one need to compare files?

Expand|Select|Wrap|Line Numbers
  1. SELECT `image` FROM `image_db` WHERE `image`!=`".$previous_image."` ORDER BY RAND() LIMIT 1
Only thing one should think is how to get $previous_image. May be, store the id in session and fetch the corresponding image from the db next time and save it as $previous_image.

This may help the OP, if he ever returns back.
May 6 '08 #14
coolsti
310 100+
Come to think about it (now that the OP is probably far far away and unaware of all these discussions), I don't think there is an issue of absolutely needing to avoid having the same image randomly being selected twice in a row.

But what may confuse the OP is the database issue. If the OP is actually storing the image bytes in the database rather than just the name of the image file, then it may not be clear how to actually get these image bytes out of the database and over to the user's browser. Because the image tag on the user's browser is not going to be able to access image bytes stored in a database directly.

If I were to use a database for this, I would store the image files within the document tree on the server, and just store the image file names in the database, and then use a query similar to that shown by the previous poster to randomly select the image file name, and then use that as the src attribute in the image tag on the page sent to the user.
May 6 '08 #15
hsriat
1,654 Expert 1GB
I guess the OP found it so confusing in PHP that now he has switched over to ASP for this issue. (All the moderators and experts discussing on this issue, and could not find a solution... I'm ashamed... what about all of you?)
May 6 '08 #16
coolsti
310 100+
I guess the OP found it so confusing in PHP that now he has switched over to ASP for this issue. (All the moderators and experts discussing on this issue, and could not find a solution... I'm ashamed... what about all of you?)
Hmmm, could be we can find the same question in the ASP forum.

But "... and could not find a solution ...." ??? But that we have, that we have.
May 6 '08 #17
Markus
6,050 Expert 4TB
OP Said:
Here the issue is though if i have same image stored in the db many times i have to display unique image on every refresh of the page.( means previously displayed image should not display if i refresh the page instead fresh image should appear.)
The same image is stored in the database multiple times.

I am not making a mountain out of a molehill.

Thank you very much.
May 6 '08 #18
Atli
5,058 Expert 4TB
The same image is stored in the database multiple times.

I am not making a mountain out of a molehill.

Thank you very much.
After reading the original question again (a few times :P) I must agree with this.

The solution is simple then... remove the duplicates from the database!
I can't imagine why the OP would add the same image multiple times... This is meant to be the top banner, so I assume this is not something the public can mess with.
So the question becomes, why are there multiple copies of the same image in the database?

I guess the OP found it so confusing in PHP that now he has switched over to ASP for this issue. (All the moderators and experts discussing on this issue, and could not find a solution... I'm ashamed... what about all of you?)
I think the problem is not that we couldn't find a solution, but rather that we didn't understand the question.
The speculation probably caused some confusion tho, your right :/
May 6 '08 #19
hsriat
1,654 Expert 1GB
I think we should close this thread or move this to Cafe section so that everyone may comment on this :D
May 6 '08 #20
ronverdonk
4,258 Expert 4TB
Since OP has opened this thread at May 2, has logged in since then and shows no interest in this discussion nor does he/she dp anuthing to clear the confusion, I will close ths thread.

moderator
May 6 '08 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ron Lounsbury | last post by:
We have 2 copies of our web application - Development and Checkout. We have noticed that when the QA quys go through our QA version, they will sometimes find that pages are missing the banners. ...
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
1
by: Terry Haufler | last post by:
I am trying to swap/rotate random flash banners using the following Javascript code. I have 3 flash headers/banners. I can get it to open a page with a random header using...
0
by: Lampa Dario | last post by:
I have developed this simple script that display a banner amond others in a random way. Here is the page http://www.teachingonline.it/articoli.php?IdArticolo=55 Francesco
2
by: Novice Computer User | last post by:
If you can help me, I'd be grateful. Long story short... I am looking for cut/paste html code (that even a dummy like me can't screw up). Here is what I want: I want a web page to display ten...
1
by: sven.daems | last post by:
Hy I want to add a sort of news service to my site. I've a number of messages, wich I want to be shown in a <marquee> tag. I've found a simple scrit that generates an random message (wich I've...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.