Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-21-2006, 09:38 AM   PM User | #1
Robbie59
New to the CF scene

 
Join Date: Dec 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Robbie59 is an unknown quantity at this point
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>
Robbie59 is offline   Reply With Quote
Old 12-21-2006, 02:38 PM   PM User | #2
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
Since you've already got your stuff in an array just shuffle() instead of sort()

http://uk2.php.net/manual/en/function.shuffle.php
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 12-21-2006, 04:43 PM   PM User | #3
Robbie59
New to the CF scene

 
Join Date: Dec 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Robbie59 is an unknown quantity at this point
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!
Robbie59 is offline   Reply With Quote
Old 12-21-2006, 05:17 PM   PM User | #4
Robbie59
New to the CF scene

 
Join Date: Dec 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Robbie59 is an unknown quantity at this point
sort($items);

Too easy, thanks for the kick start! Problem solved
Robbie59 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:50 PM.


Advertisement
Log in to turn off these ads.