SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    new to Javascript...can i randomize images from database???

    Hi,

    I found this javascript to randomize an image on a page...

    Code:
    <script language="JavaScript" type="text/javascript">
    <!-- 
    function random_img(){
    var ranimage = new Array();
    ranimage[1] = "photo1.jpg";
    ranimage[2] = "photo2.jpg";
    ranimage[3] = "photo3.jpg";
    
    var ry = Math.floor(Math.random()*ranimage.length)
    if (ry==0)
    ry=1
    document.write('<img src="'+ranimage[ry]+'" border=1>');
    }
    random_img();
    // -->
    </script>
    I have some images that are dynamically generated and i want these images to randomize every few seconds (at the moment the images change when the user goes to a different page on the site)...can anyone give me any ideas as to whether this is possible??

    The PHP code i'm using is below...

    Code:
    <?php
    		     
    	     // read image dir
    $dir="../../admin/uploads/";
    // Open directory, and proceed to read its contents
    if (is_dir($dir)) {
       if ($dh = opendir($dir)) {
           while (($file = readdir($dh)) !== false) {
    	   if (filetype($dir . $file)== "file"){
              
    $counter++;
    		
    $selectSQL = "SELECT id, url, imagefile from images";
    $result=mysql_query($selectSQL);
    #print $result . " - " . mysql_error();
    
    $nameStack=array();
    $urlStack=array();
    
    while ($row = mysql_fetch_assoc($result)){
    	$thisurl= $row['url'];
    	$thisid= $row['id'];
    
    array_push($nameStack, $row['imagefile']);
    array_push($urlStack, $row['url']);
    
    }
    		 
     
      }
           }
           closedir($dh);
       }
    }
    
    
    		     // load each image into array echo rand(5, 15);
    		     // get count of total no. of images
    		     $lastNo=sizeof($urlStack)-1;
    		     $imageToUse=rand(0,$lastNo);
    		     #print "using image $imageToUse of $lastNo";
    		     $imageName=$nameStack[$imageToUse];
    		     $imageUrl=$urlStack[$imageToUse];
    
    		     #print "got $imageUrl and $imageName\n";
    		     
    		     //
    		     
    		     ?><a href="<?php echo $imageUrl; ?>"><img src="http://mywebsite.com/admin/uploads/<?php echo $imageName; ?>" alt="click here for our special offers" border=0 /></a>
    Thanks for any help.

    G

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Just use PHP to generate this portion of your JavaScript code:

    Code:
    ranimage[1] = "photo1.jpg";
    ranimage[2] = "photo2.jpg";
    ranimage[3] = "photo3.jpg";

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for your reply.
    You make it sound easy!!!

    Could you elaborate please?

    Thank you.

    G

  4. #4
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Sure,

    On the page(s) that this effect will occur use your PHP to create the javascipt values for you like this:

    Code:
    ranimage[1] = '<?php echo $nameStack[0]; ?>';
    ranimage[2] = '<?php echo $nameStack[1]; ?>';
    ranimage[3] = '<?php echo $nameStack[2]; ?>';
    This is assuming the nameStack array is holding the path to the images.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks i'll have a play about and see if i can get it working.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    'm afraid i'm not getting it.
    I think my javascript and php knowledge may be too lacking for this.

    i've tried:
    ranimage[1] = '<?php echo $imageName[0]; ?>';

    and:
    ranimage[1] = '<?php echo $dir="../../admin/uploads/image1.gif"[0];?>';

    but neither seem to work.

    Is this totally down the wrong route?

  7. #7
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    You seem to have the right idea. Did you get it to work without the PHP first? If not, do that. Then adding the PHP will be easy.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •