Random display of banners using php?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thulaseeram
    New Member
    • Sep 2006
    • 23

    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
  • coolsti
    Contributor
    • Mar 2008
    • 310

    #2
    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.

    Code:
    $imagename = 'bimg' . $randomnumber . '.jpg';
    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.

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      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

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        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:
        [code=php]
        <?php
        // Get the last number used
        // @ silences the warning displayed if the session is empty
        $last = @$_SESSION['lastNumber'];

        // Generate random number that is not the same as the last number
        do {
        $next = mt_rand(0, 10);
        } while ($next == $last);

        // Add the new number to the session and show an image.
        $_SESSION['lastNumber'] = $next;
        echo "<img src='image_nr_{ $next}.jpg' />";
        ?>
        [/code]

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          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?

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Originally posted by markusn00b
            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.

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              Originally posted by Atli
              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.

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Originally posted by TheServant
                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:
                [code=php]
                function files_compare($ file1, $file2, $numSamples=10)
                {
                // Check file size
                if(filesize($fi le1) != filesize($file2 )) {
                return false;
                }

                // Open streams
                $fha = fopen($file1, "r");
                $fhb = fopen($file2, "r");

                // Calculate offset
                $offset = (filesize($file 1) / ($numSamples + 1));

                // Get and check samples
                for($i = 0; $i < $numSamples; $i++) {
                // Seek forward
                fseek($fha, $offset, SEEK_CUR);
                fseek($fhb, $offset, SEEK_CUR);

                // Take samples
                if(fread($fha, 16) != fread($fhb, 16)) {
                return false;
                }
                }

                // Close streams
                fclose($fha);
                fclose($fhb);

                // All tests passed
                return true;
                }
                [/code]

                Comment

                • TheServant
                  Recognized Expert Top Contributor
                  • Feb 2008
                  • 1168

                  #9
                  Very nice! Just wondering, what does offset mean in that code?

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    Originally posted by TheServant
                    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.

                    Comment

                    • TheServant
                      Recognized Expert Top Contributor
                      • Feb 2008
                      • 1168

                      #11
                      Originally posted by Atli
                      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?

                      Comment

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #12
                        Originally posted by Atli
                        Why would an image get uploaded multiple times?
                        Originally posted by thulaseeram
                        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.

                        Comment

                        • coolsti
                          Contributor
                          • Mar 2008
                          • 310

                          #13
                          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.

                          Comment

                          • hsriat
                            Recognized Expert Top Contributor
                            • Jan 2008
                            • 1653

                            #14
                            Originally posted by Atli
                            You could use the Filesystem functions.

                            Somewhat like this:
                            [code=php]
                            function files_compare($ file1, $file2, $numSamples=10)
                            {
                            //............... .......
                            }
                            [/code]
                            If the images are already stored in the database, why should one need to compare files?

                            Code:
                            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.

                            Comment

                            • coolsti
                              Contributor
                              • Mar 2008
                              • 310

                              #15
                              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.

                              Comment

                              Working...