Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 18
  1. #1
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,688
    Member #
    5580
    Liked
    717 times
    Requirements:

    1) Your webhost allows PHP scripting.
    2) Your web page(s) have the extension .php
    3) You have a directory with all of your photos uploaded
    by number ... example: photo_1.jpg, photo_2.jpg, photo_3.jpg

    At the beginning of your web page, right after <body>, insert this script:

    Code:
    <?php
    srand(time());
    $max=50; // This is the number of photo you have in your directory (eg. numbered 1-50)
    $arr = array();
    while ( count($arr) < $max ) {
    $x = mt_rand(1,$max);
    if ( !in_array($x,$arr) ) { $arr[] = $x; }
    } 
    $p1 = "photo_".$arr[0].".jpg";
    $p2 = "photo_".$arr[1].".jpg";
    $p3 = "photo_".$arr[2].".jpg";
    ?>
    In the script above, you have defined the number of photos you have in
    your directory, in this case, there are 50 of them numbered 1-50 (photo_1.jpg, photo_2.jpg)

    You will have 3 randomly picked photos ready to be used at any time.

    You can display them by using any of the three $p variables ...
    Giving the directory path, and then the PHP variable (which is the filename of your photo)...

    <img src="./myphotos/<?=$p1?>" alt="my title">

    or
    <img src="./myphotos/<?=$p2?>" alt="my title">

    Those three photos created in the script will all be different and random.
    If you need more than 3 photos, add more:
    $p4 = "photo_".$arr[3].".jpg";
    $p5 = "photo_".$arr[4].".jpg";
    $p6 = "photo_".$arr[5].".jpg";

    Each time the page is refreshed or displayed, the 3 photos will randomly change.

    Here's a working example:
    http://www.pierreandclaudia.com


  2.  

  3. #2
    Senior Member
    Join Date
    Apr 2006
    Posts
    146
    Member #
    13110
    Liked
    1 times
    nice tut, easy to follow. Cool

  4. #3
    Senior Member hagen's Avatar
    Join Date
    Aug 2005
    Posts
    408
    Member #
    10882
    Liked
    1 times
    Though it's probably more than yo uare trying to demonstrate here, I do stuff like that like this:


    $d = dir("gallery/$folder");
    while (false !== ($entry = $d->read())) {
    if (preg_match('/jpg/',$entry)) {
    array_push($files, $entry);
    }
    }
    $d->close();

    IE get a list of the .jpg files in a folder then open them randomly as you have done above... you dont need to worry about the names then...

    If you only wanted a specific number you could jump out of the loop...

    :-)

    -Hagen
    Hagen Rose: hagen(at)jxwd(dot)co(dot)uk
    JX Web Development, Bournemouth, Dorset...JXWD.co.uk

  5. #4
    Senior Member
    Join Date
    May 2005
    Location
    Essex, United Kingdom
    Posts
    200
    Member #
    10131
    Hagen: That is a good idea in theory, probably won't work for many sites as it is very processor intensive, getting every file from a folder.

  6. #5
    Senior Member hagen's Avatar
    Join Date
    Aug 2005
    Posts
    408
    Member #
    10882
    Liked
    1 times
    Hi, dchesterton intresting... :-)

    I guess you appreciate I am not getting the files but a file list....?

    It's not something I had thought chould be significant...

    Is this really going to be slow compared with connecting to a database for example?

    Or with the internet latency itself?

    Cheers

    -H
    Hagen Rose: hagen(at)jxwd(dot)co(dot)uk
    JX Web Development, Bournemouth, Dorset...JXWD.co.uk

  7. #6
    Senior Member hagen's Avatar
    Join Date
    Aug 2005
    Posts
    408
    Member #
    10882
    Liked
    1 times
    system architecture

    Here is a largish example with 70ish files....

    http://www.taichiclub.co.uk/tai-chi-gallery.php

    would you not expect the processor to make a single IO request to the "storage device"?

    I guess it could be waiting around unable to do anything until the results come back...?

    But then servers are multithreaded arn't they so would just run another job in the case of multiple users?

    Let me know what you think! I know plenty about processors having worked on the design of a number of them, DSP's and microcontrollers over the years, but very little about system atchitecture... :nervous:

    Cheers all


    -Hagen
    Hagen Rose: hagen(at)jxwd(dot)co(dot)uk
    JX Web Development, Bournemouth, Dorset...JXWD.co.uk

  8. #7
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,688
    Member #
    5580
    Liked
    717 times
    This is really a bandwidth issue?

    If any PHP (or Perl) scripts are a burden, wouldn't the webhosts
    find a way to charge the bandwidth ... that being my limitation to
    how much system use I'm allowed?

    Unless I'm amazon.com, or ebay, my site is probably not much of
    a burden to a server, even if I get 1000 unique visitors per day.

    If it was difficult for me to name my .jpegs in sequence, I would think
    using Hagen's 'read directory' snippet would be a great way to grab
    the list of .jpegs.

    But then, that's what makes this forum the best ... an exchange of
    ideas that get people thinking (and learning some new things).
    This never ceases to amaze me.


  9. #8
    Senior Member
    Join Date
    May 2005
    Location
    Essex, United Kingdom
    Posts
    200
    Member #
    10131
    I'm not completely sure if there is a bandwidth issue. I was just trying to put ideas out there, I would guess it could get quite processor intensive with a lot of requests, to be completely honest i'm not sure but I would suspect it would be. Maybe someone knows for sure

  10. #9
    Senior Member hagen's Avatar
    Join Date
    Aug 2005
    Posts
    408
    Member #
    10882
    Liked
    1 times
    well it got me thinking!! :-)

    ...and if things started to grind to a halt and I had this kind of structure it would probably be one of the first places I could check now...

    Cheers -H

    P.S. I hope we will be designing sites on the same scale as eBay and friends very soon! :-)
    Hagen Rose: hagen(at)jxwd(dot)co(dot)uk
    JX Web Development, Bournemouth, Dorset...JXWD.co.uk

  11. #10
    Member
    Join Date
    Apr 2006
    Posts
    45
    Member #
    13115
    You could also store the names of the images in your database and pick one randomly using this mysql statement:
    select * from my_image_table order by rand() limit 1


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

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

Search tags for this page

php code for random image display

Click on a term to search for related topics.
All times are GMT -6. The time now is 10:40 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com