Barand Posted February 28, 2017 Share Posted February 28, 2017 I tried helping you with advice on writing a custom sort function and how it should return values, but you chose to totally ignore it. Therefore I am not inclined to waste any more of my time. Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/page/2/#findComment-1543520 Share on other sites More sharing options...
jimboppin Posted February 28, 2017 Author Share Posted February 28, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/page/2/#findComment-1543521 Share on other sites More sharing options...
jimboppin Posted February 28, 2017 Author Share Posted February 28, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/page/2/#findComment-1543522 Share on other sites More sharing options...
jimboppin Posted February 28, 2017 Author Share Posted February 28, 2017 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... Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/page/2/#findComment-1543523 Share on other sites More sharing options...
jimboppin Posted March 2, 2017 Author Share Posted March 2, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/303294-sort-order-by-last-date-modified/page/2/#findComment-1543649 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.