Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random readdir php to XML

    Thanks for viewing my thread. I would like to create a random list of files from a directory. The use of this would be to supply a Flash flv player with a shuffled playlist. Please have a look at this code and maybe you guys can figure out a way to make this php file output a random list of my directory into XML..
    The code works great now but Id like it to be random or shuffled on each visit.
    THANKS!


    <?php

    $filter = ".flv";

    // path to the directory you want to scan

    $directory = "../vids";

    // read through the directory and filter files to an array

    @$d = dir($directory);

    if ($d) {

    while($entry=$d->read()) {

    $ps = strpos(strtolower($entry), $filter);

    if (!($ps === false)) {

    $items[] = $entry;

    }
    }

    $d->close();
    sort($items);

    }
    // we'll first add an xml header and the opening tags ..
    header("content-type:text/xml;charset=utf-8");

    echo "<ratings>\n";
    // .. then we loop through the mysql array ..
    for($i=0; $i<sizeof($items); $i++) {
    echo "<video file=\"$dir$items[$i]\" >\n";
    echo " <title>".str_replace(".flv","",$items[$i])."</title>\n";
    echo " <runtime>auto</runtime>\n";
    echo " </video>\n";
    }
    echo "</ratings>\n";

    ?>



    THE OUTPUT LOOKS LIKE THIS:


    <ratings>

    <video file="video1.flv">
    <title>video1</title>
    <runtime>auto</runtime>
    </video>

    <video file="video2.flv">
    <title>video2</title>
    <runtime>auto</runtime>
    </video>

    <video file="video3.flv">
    <title>video3</title>
    <runtime>auto</runtime>
    </video>

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,169
    Thanks
    19
    Thanked 65 Times in 64 Posts
    Since you've already got your stuff in an array just shuffle() instead of sort()

    http://uk2.php.net/manual/en/function.shuffle.php

  • #3
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by NancyJ View Post
    Since you've already got your stuff in an array just shuffle() instead of sort()

    http://uk2.php.net/manual/en/function.shuffle.php
    Great, so you think this is possible? How should I code this to make it shuffle? Can you help me get started on where to add this code?

    Thanks!

  • #4
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sort($items);

    Too easy, thanks for the kick start! Problem solved


  •  

    Posting Permissions

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