Web Analytics Made Easy -
StatCounter Random Picture Change in PHP - CodingForum

Announcement

Collapse
No announcement yet.

Random Picture Change in PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Random Picture Change in PHP

    I was wondering if there was a way for me to do a random picture change on a web page load event. I was going to put two pictures on either side of the logo on my main page, and then from there I was going to let the PHP code choose randomly what pictures it wanted to load. I did'nt know if there was some help out on the net, or some simple code that I can put in to make it do it. I can maybe do it in java, but the problem is not everyone has java enabled browsers on their computers. If someone could help I would greatly appreciate it...Thanks a lot for all of the help
    thanks
    bri

  • #2
    It is SOOO ironic that you should ask that. I just finished my own version of, and am right in the middle of writing a new one...until I stumbled upon this:

    http://www.alistapart.com/d/randomizer/rotate.txt


    That's a great one. simply tell the script what directory to find the images, and then <img src="roate.php"> and you're good to go!

    Comment


    • #3
      Below is a code I developed for my site. I have 6 images that are rotated every 10 seconds. The date line reads the seconds of the server. If its less than 10 seconds 01.jpg will show, less than 20 seconds 02.jpg will show and so on. You can also change the Alt for each image

      Go to http://www.stubby.ca and you will see it in action. But you have to re-load the page for the new image, it not automatic.


      PHP Code:
      <?php
          
          $n 
      date('s');

          
          if  (
      $n 10)  {
          echo 
      '<table><td><img border="2" src="images/banner/01.jpg" 
          width="346" height="150" alt = "image 1 of 6"></td></table>'

          } 
          
          elseif (
      $n 20 )  {
          echo 
      '<table><td><img border="2" src="images/banner/02.jpg" 
          width="155" height="150" alt = "image 2 of 6"></td></table>'
      ;
          }  

          elseif (
      $n 30 )  {
          echo 
      '<table><td><img border="2" src="images/banner/03.jpg" 
          width="305" height="150" alt = "image 3 of 6"></td></table>'
      ;
          }  

          elseif (
      $n 40 )  {
          echo 
      '<table><td><img border="2" src="images/banner/04.jpg" 
          width="412" height="150" alt = "image 4 of 6"></td></table>'
      ;
          }  

          elseif (
      $n 50 )  {
          echo 
      '<table><td><img border="2" src="images/banner/05.jpg" 
          width="365" height="150" alt = "image 5 of 6"></td></table>'
      ;
          }  

          else {
          echo 
      '<table><td><img src="images/banner/canada.gif" 
          alt = "Canadian flag - 1967"></td></table>'
      ;
          
      }

      ?>

      PS....You will notice that the script above has an image border of 2px but on my website it doesn't show up. I just changed all borders to the same color as the background for a cleaner look.


      Leonard Whistler
      http://www.stubby.ca
      Last edited by Len Whistler; Feb 9, 2004, 01:40 AM.
      Leonard Whistler

      Comment


      • #4
        I would recommend just using the srand() function in conjunction with an array that stores each value/picture/string you want output.

        Comment


        • #5
          Ok thanks Doagie.....So I assume that my code can get shortened by using srand() function. I will check out a php book for more info.

          Leonard Whistler
          http://www.stubby.ca
          Leonard Whistler

          Comment


          • #6
            the link I posted above is probably the best I've seen....

            here's the code I originally wrote, but it was before I discovered the pathinfo() function.. lol


            PHP Code:
            function random_image() {
               
            $path "images/mypic";
               
            $d dir($path);
               
            $files= array();
               
            $index0;
               while (
            false !== ($entry $d->read())) {
                  if ( 
            substr($entry01)=='.' ) continue;
                     
            $dotposstrrpos($entry'.');
                     if (
            $dotpos) {
                      
            $extension substr($entry$dotpos+1);
                      if (
            $extension == "jpg") {
                         
            $files[]= $entry;
                      }
                    }
               }
               
            $d->close();
               return 
            $path ."/" $files[array_rand($files)];
            }
            ?> 

            Comment

            Working...
            X