Jump to content

[SOLVED] Sorting a table by filemtime...


whatap

Recommended Posts

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

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);

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  :'(

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.