Jump to content

Sort order by last date modified


jimboppin

Recommended Posts

 

You may find it better to store the date and contents in the array (with filename as the key). In which case a simple asort() will not work, you will need a custom sort function.

 

For example

$files['file1.txt']  = [    'date' => '2017-01-03',
                            'content' => 'aaa'
                            ];
$files['file2.txt']  = [    'date' => '2017-01-01',
                            'content' => 'bbb'
                            ];
$files['file3.txt']  = [    'date' => '2017-01-02',
                            'content' => 'ccc'
                            ];
$files['file4.txt']  = [    'date' => '2017-01-04',
                            'content' => 'ddd'
                            ];

uasort($files, function ($a, $b) {
                    return strcmp ($a['date'], $b['date']);
                });

foreach ($files as $filename => $fdata) {
    // process the output
}

i did try and implement this, but its not what im looking for.. i just need to read the date and time from the first line and use that line to order my files, i dont see the problem. im not here to argue ill just move on then.

Link to comment
Share on other sites

hi im trying to implement this to my code as i found out that if i backup my site then upload it all my news is reset and gets mixed up..

how do i format my txt file so this can read it and sort it out?

 

i imagin somthing like this...

date>27/02/2017 6:48pm
anything after the above line is content


some contentsome contentsomesome contentsome
some contentsome contentsome
some contentsome content

some contentsome content
some contentsome content


some contentsome contentsome content

 heres the function...

function get_media($media_cat, $display_recent) {
	global $Parsedown, $media_id;
	if (isset($media_id)) {
		if ($media_id == $media_cat) {
			$display_recent = null;
		}
	}
	$directory = MEDIA_BASEDIR.$media_cat.'/';
	$files = array();
	$files = glob($directory.'*.txt');
	
	uasort($files, function ($a, $b) {
			return strcmp ($a['date'], $b['date']);
		});


	$recent_content = array_slice($files, 0, $display_recent);
	foreach ($recent_content as $media => $fdata) {
		$content = file_get_contents($fdata);
		$media_title = basename($fdata);
		$media_title = basename($fdata, '.txt');
		if (file_exists($fdata)) {
			$date_time = date ('F d Y H:i:s', filectime($fdata));
		}
		THEMEMEDIATOP($media_title, $media_cat);
		echo $Parsedown->text($content);
		THEMEMEDIABOTTOM($date_time);
	}
}

thanks again

heres what i did.. obviousley its wrong.

Link to comment
Share on other sites

sorry i see how that works now, i was hopeing to be able to just upload a txt file no worries, and to do that the only thing i can think of is to put the date and time in the first line of the text file and then read the first line to sort by date

 

first line being line[0]

 

i know how to read the first line im just stuck how to use it in the uasort function

 

thanks

then i noticed it was wrong...

Link to comment
Share on other sites

hi, im still trying to get my function to work.. i have done this so far its not working the way i want but im getting closer i just cant get it to work properly.

function get_media($media_cat, $display_recent) {
    global $Parsedown, $media_id;
    if (!empty($media_id)) {
        if ($media_id == $media_cat) {
            $display_recent = null;
        }
    }
    $directory = MEDIA_BASEDIR.$media_cat.'/';
    $files = array();
    $files = glob($directory.'*.txt');
    foreach ($files as $file) {
        $line = file($file);
        $date_time = $line[0];
        $date_time = $Parsedown->text($date_time);
        
        uasort($files, function ($a, $b) {
            global $date_time;
                return strcmp ($a[$date_time], $b[$date_time]);
            });
        
        $recent_content = array_slice($files, 0, $display_recent);
        foreach ($recent_content as $media) {
            $content = file_get_contents($media);
            $media_title = basename($media);
            $media_title = basename($media, '.txt');
            
            thememediatop($media_title, $media_cat);
            echo $Parsedown->text($content);
            thememediabottom($date_time);
        }
    }
}

here is the txt file

 

this is a example..

02/03/2017 5:12pm
everything from here on is content
some content
more content

i think its got somthing to do with

return strcmp ($a[$date_time], $b[$date_time]);

because i get this error

String offset cast occurred

please help me with what i want here.

thanks

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.