473,569 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random display of banners using php?

23 New Member
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 2488
coolsti
310 Contributor
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 Recognized Expert Specialist
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 Recognized Expert Expert
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 Recognized Expert Expert
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 Recognized Expert Expert
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 Recognized Expert Top Contributor
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 Recognized Expert Expert
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 Recognized Expert Top Contributor
Very nice! Just wondering, what does offset mean in that code?
May 6 '08 #9
Atli
5,058 Recognized Expert Expert
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

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

Similar topics

0
1169
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. The banners are created by VI using the themes and layouts. All of the other theme/layout information is fine, and both sites are using identical...
10
2490
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 spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone...
1
5273
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 document.write(randomHeader) in the chooseHeader() function, but the rest of the page doesn't get displayed with that approach. Instead, what I would like to do is...
0
1116
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
2016
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 (10) different text ads and/or banner ads on the page. However, I want the page to change the order of the ten different ads each time it is...
1
4268
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 putted in an array) and the message is shown in an marquee tag. The problem is that for changing the message, a reload of the page is required....
4
2739
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. It should put a blue and yellow box on the page with "This is a test" as part of the picture. But what I get is a broken Gif. The other problem is...
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7673
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.