Schlo_50 Posted July 3, 2007 Share Posted July 3, 2007 I have developed a script (with help) that tries to order events stored in my .txt file by date. The .txt file looks like this: 1st|Jan|Meeting at House| 15th|Feb|Meeting at your House etc The script orders the dates from lowest to highest but does not order dates before the 10th. Apparently 1st - 9th comes after 31st.. Here is a picture to explain: This is the script: <?php function getevents($eventmonth) { $file = file("data.txt"); //puts each line of the file into an array //loop through array foreach($file as $key => $val) { $data[$key] = explode("|", $val); //breaks up each line into seperate arrays, values seperated by | $day = $data[$key][0]; $month = $data[$key][1]; $event = $data[$key][2]; //will only list month you specify in function. if($month == $eventmonth) { $events[$day] = $event; } } ksort($events); foreach($events as $day => $event) { echo "$day - $event<br />\n"; } } //Using the function getevents("Jan"); ?> Can someone revise the code so that the order goes for example: 1st-> 3rd-> 8th-> 11th-> 24th and not: 11th-> 24th-> 1st-> 3rd Thanks in advance guys!! Quote Link to comment https://forums.phpfreaks.com/topic/58197-ordering-lists/ Share on other sites More sharing options...
rcorlew Posted July 3, 2007 Share Posted July 3, 2007 instead of: ksort use natsort It will make it human readable results Quote Link to comment https://forums.phpfreaks.com/topic/58197-ordering-lists/#findComment-288629 Share on other sites More sharing options...
Schlo_50 Posted July 3, 2007 Author Share Posted July 3, 2007 BUMP natsort didn't work, the output still orders the dates 10th to the 31st properly but anything lower than the 10th gets pushed to the end of the list as shown in the supplied picture. Quote Link to comment https://forums.phpfreaks.com/topic/58197-ordering-lists/#findComment-288931 Share on other sites More sharing options...
AndyB Posted July 3, 2007 Share Posted July 3, 2007 The real problem lies with your data. Sorting occurs by ASCII value of each character in a string so logically 9th would come after 31st (because 9 comes after 3). If instead your dates were simply 01, 02, ... 31 then you could sort properly - and then if you want to display the day numbers in a more convenient manner. Quote Link to comment https://forums.phpfreaks.com/topic/58197-ordering-lists/#findComment-288937 Share on other sites More sharing options...
wildteen88 Posted July 3, 2007 Share Posted July 3, 2007 Hi Schlo_50 I helped you last time and provided you the code for this. Could you post an example of how data.txt is formatted. The way I have it coded is when the days are pulled form the file I save the day inro the array index and then assign the event to that index. ksort is then used to sort the array indexes into numerical order, eg 1, 8, 11, 21. This how I get the events to show up in order of day. The code should be doing the trick. EDIT: I see what is wrong now. Before (when I coded this script for you) you didn't have the date suffix in data.txt, eg: st, nd, rd etc. That is what is throwing the list out of order. If you remove the suffix you'll see it'll display in the correct order. Quote Link to comment https://forums.phpfreaks.com/topic/58197-ordering-lists/#findComment-288944 Share on other sites More sharing options...
wildteen88 Posted July 3, 2007 Share Posted July 3, 2007 Ok Schlo_50 try this: <?php // get the suffix for the date function get_suffix($day) { // create a unix timestamp of the day $time = mktime(0, 0, 0, 0, $day); // using the unix timestamp get the date // suffix using PHP's built in date function $suffix = date('S', $time); // return the day with suffix // eg 1st, 3rd, 20th return $day . $suffix; } function getevents($event_month) { $file = file("data.txt"); //puts each line of the file into an array //loop through array foreach($file as $key => $val) { $data[$key] = explode("|", $val); //breaks up each line into seperate arrays, values seperated by | $day = $data[$key][0]; $month = $data[$key][1]; $event = $data[$key][2]; //will only list month you specify in function. if($month == $event_month) { $events[$day] = $event; } } ksort($events); foreach($events as $day => $event) { echo get_suffix($day) . " - $event<br />\n"; } } //Using the function getevents("Jan"); ?> This time save the data.txt in this formatt: 3|Jan|What to do.. 10|Jan|Birthday ... The code will now take care of getting the correct suffix for the day and your list will be in the correct numerical order. Quote Link to comment https://forums.phpfreaks.com/topic/58197-ordering-lists/#findComment-288971 Share on other sites More sharing options...
Schlo_50 Posted July 3, 2007 Author Share Posted July 3, 2007 I shall try that, thanks. I'll keep you posted. Quote Link to comment https://forums.phpfreaks.com/topic/58197-ordering-lists/#findComment-289033 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.