use php to randomally display files

Discussion in 'PHP' started by mbaldwin, Aug 31, 2009.

  1. #1
    Hi,
    I was wondering if there was a script, or if someone could point me in the right direction. I am looking for a way to display html files randomally, that are located in a particular folder.

    Or, if I can store html in a mysql database, i know how to get php to randomally display that info.
    Thanks,
     
    mbaldwin, Aug 31, 2009 IP
  2. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
  3. HivelocityDD

    HivelocityDD Peon

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    One way is you can get the files inside the particular directory which ends with .html extension. and then select a random one. You can call the require_once() function in php to call the random file selected or can use include() function.

    If you are trying to set the html data inside DB make sure you save the content as text or blob.
     
    HivelocityDD, Sep 1, 2009 IP
  4. mbaldwin

    mbaldwin Active Member

    Messages:
    215
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    95
    #4
    Thanks for the link and suggestions, that was very helpful. is there a performance difference by using either a random file, or storing it in a DB?

    I might just try it both ways and see which works better for me.
    Thanks,
     
    mbaldwin, Sep 1, 2009 IP
  5. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    function randomFile() {
      $files = glob("/path/to/dir/*.*");
      return $files[array_rand($files, 1)];
    }
    
    PHP:
    is more compact than the solution in the above URL.

    However, it would perhaps be best if you stored it in the DB and used order by rand() since the file based solutions require an array with all the filenames.. if you have loads of items it could eat up quite a bit of memory per request.
     
    premiumscripts, Sep 1, 2009 IP
  6. mbaldwin

    mbaldwin Active Member

    Messages:
    215
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    95
    #6
    Well, i am not planning on having a lot of items, but I would like to plan for the future. Now that I annalize what I want to do, a db might be best, then I can display the same html file for lets say 3 days, and then display a different randomally selected one for another 3 days.
    Thanks all for the help. i will be in need of it again for other projects.
     
    mbaldwin, Sep 1, 2009 IP
  7. picos

    picos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    81
    #7
    Our you can use :
    
    $file=array();
    $file['1']="link to file 1";
    .
    .
    .
    $file['n']="link to file n";
    $random=random(1,n);
    $file_random=$file[$random];
    
    PHP:
     
    picos, Sep 1, 2009 IP