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

Link to comment
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);

 

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.