whatap Posted February 16, 2007 Share Posted February 16, 2007 Okay, so I have an upload script that touches the torrent files as soon as they are uploaded as an easier workaround for displaying upload time via displaying filemtime. Everything is working fine and dandy, except I can't for the life of me figure out how to sort all the links listed by filemtime (I'm not very good with php at all). I've been playing around and reading the php manual but nothing seems to work. Can anyone help me out with this? Code I'm using: $dirpath = "./torrents"; $dh = opendir($dirpath); while (false !== ($file = readdir($dh))) { if (!is_dir("$dirpath/$file")) { $fileend = substr($file, -; $fileend = strtolower($fileend); $lm = date("Y-m-d H:i", filemtime("$dirpath/$file")); if ( $fileend == ".torrent" ) { $find = array('&', '+'); $replace = array ('%26', '%2b'); $filename = str_replace($find,$replace, $file); echo "<table width=\"60%\" cellspacing=\"0\" cellpadding=\"2\"><tr><td class=\"reply\" align=\"left\"><A HREF=\"$dirpath/$filename\">$file</A></td><td class=\"reply\" align=\"right\"><A HREF=\"parse.php?filename=$filename\">details</A></td></tr><tr><td class=\"reply\" align=\"left\"><font size=\"2\">uploaded " . $lm . " CST.</td><td align=\"right\" class=\"reply\">thread</td></tr>"; } } } closedir($dh); Eeh, I imagine this code looks very nasty to you experience coders. Thanks Link to comment https://forums.phpfreaks.com/topic/38712-solved-sorting-a-table-by-filemtime/ Share on other sites More sharing options...
jwk811 Posted February 16, 2007 Share Posted February 16, 2007 i dont understand, what are you trying to do exactly? Link to comment https://forums.phpfreaks.com/topic/38712-solved-sorting-a-table-by-filemtime/#findComment-185977 Share on other sites More sharing options...
hitman6003 Posted February 16, 2007 Share Posted February 16, 2007 It's not graceful, but this should do what you want... <?php $dirpath = "./torrents"; $dh = opendir($dirpath); while (false !== ($file = readdir($dh))) { if (!is_dir("$dirpath/$file")) { $fileend = substr($file, -; $fileend = strtolower($fileend); $mod_time = filemtime("$dirpath/$file"); $lm = date("Y-m-d H:i", $mod_time); if ( $fileend == ".torrent" ) { $find = array('&', '+'); $replace = array ('%26', '%2b'); $filename = str_replace($find,$replace, $file); $html = " <table width=\"60%\" cellspacing=\"0\" cellpadding=\"2\"> <tr> <td class=\"reply\" align=\"left\"><A HREF=\"$dirpath/$filename\">$file</A></td> <td class=\"reply\" align=\"right\"><A HREF=\"parse.php?filename=$filename\">details</A></td> </tr> <tr> <td class=\"reply\" align=\"left\"><font size=\"2\">uploaded " . $lm . " CST.</td> <td align=\"right\" class=\"reply\">thread</td> </tr> </table>"; if (!$files[$mod_time]) { $files[$mod_time] = $html; } else { $files[$mod_time][] = $html; } } } } ksort($files); foreach ($files as $file) { if (is_array($file) { foreach ($file as $fil) { echo $fil; } } else { echo $files; } } closedir($dh); Link to comment https://forums.phpfreaks.com/topic/38712-solved-sorting-a-table-by-filemtime/#findComment-185982 Share on other sites More sharing options...
whatap Posted February 16, 2007 Author Share Posted February 16, 2007 It's not graceful, but this should do what you want... <?php $dirpath = "./torrents"; $dh = opendir($dirpath); while (false !== ($file = readdir($dh))) { if (!is_dir("$dirpath/$file")) { $fileend = substr($file, -; $fileend = strtolower($fileend); $mod_time = filemtime("$dirpath/$file"); $lm = date("Y-m-d H:i", $mod_time); if ( $fileend == ".torrent" ) { $find = array('&', '+'); $replace = array ('%26', '%2b'); $filename = str_replace($find,$replace, $file); $html = " <table width=\"60%\" cellspacing=\"0\" cellpadding=\"2\"> <tr> <td class=\"reply\" align=\"left\"><A HREF=\"$dirpath/$filename\">$file</A></td> <td class=\"reply\" align=\"right\"><A HREF=\"parse.php?filename=$filename\">details</A></td> </tr> <tr> <td class=\"reply\" align=\"left\"><font size=\"2\">uploaded " . $lm . " CST.</td> <td align=\"right\" class=\"reply\">thread</td> </tr> </table>"; if (!$files[$mod_time]) { $files[$mod_time] = $html; } else { $files[$mod_time][] = $html; } } } } ksort($files); foreach ($files as $file) { if (is_array($file) { foreach ($file as $fil) { echo $fil; } } else { echo $files; } } closedir($dh); This just brings up a blank page, I'll try looking for typos or anything... Honestly this seems too simple but its difficult for me to grasp how to implement :'( Link to comment https://forums.phpfreaks.com/topic/38712-solved-sorting-a-table-by-filemtime/#findComment-185991 Share on other sites More sharing options...
hitman6003 Posted February 16, 2007 Share Posted February 16, 2007 I missed a closing parenthesis on this line: if (is_array($file) { It should be: if (is_array($file)) { Link to comment https://forums.phpfreaks.com/topic/38712-solved-sorting-a-table-by-filemtime/#findComment-185992 Share on other sites More sharing options...
whatap Posted February 16, 2007 Author Share Posted February 16, 2007 Yeah I caught that and corrected, now just " ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray" shows up Link to comment https://forums.phpfreaks.com/topic/38712-solved-sorting-a-table-by-filemtime/#findComment-185994 Share on other sites More sharing options...
hitman6003 Posted February 16, 2007 Share Posted February 16, 2007 change: echo $files; to echo $file; Link to comment https://forums.phpfreaks.com/topic/38712-solved-sorting-a-table-by-filemtime/#findComment-186006 Share on other sites More sharing options...
whatap Posted February 16, 2007 Author Share Posted February 16, 2007 change: echo $files; to echo $file; It works! Thank you so much! Link to comment https://forums.phpfreaks.com/topic/38712-solved-sorting-a-table-by-filemtime/#findComment-186025 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.