Schlo_50 Posted June 9, 2007 Share Posted June 9, 2007 Hey guys, i have this script where my .txt file loads events and displays the ones relevant to the month they belongs in. My .txt file stores data in this format. --> ('Date|'Month'|'Message') Practical E.G 1st|Jan|Test1 If i have multiple events happening in Jan i want to order the events so that they run in the order of 1st, 2nd etc through to 31st when all events in 'Jan' are called. At the moment if i enter an event for the 2nd of Jan, then the 10th of Jan and finally the 7th of Jan they will be displayed in that order and not date order! Please can someone help me? Maybe i just need a small piece of code to order the called results? I hope you can understand what i am trying to do, thanks uin advance! <?php //Use the following function to retrive data for each month. //Look at the data structure. function getevents($eventmonth){ $file = file("data.txt"); //puts each line of the file into an array foreach($file as $key => $val){ //loop through array $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]; if($month == $eventmonth){ //will only list month you specify in function. $out = "$day - $event<br>\n"; print $out; } } } //Using the function getevents("Jan"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54859-solved-i-need-to-order-my-txt-file/ Share on other sites More sharing options...
Schlo_50 Posted June 9, 2007 Author Share Posted June 9, 2007 BUMP Quote Link to comment https://forums.phpfreaks.com/topic/54859-solved-i-need-to-order-my-txt-file/#findComment-271339 Share on other sites More sharing options...
wildteen88 Posted June 9, 2007 Share Posted June 9, 2007 Rather than echo'ing out the events when you find them. Save them to a variable which is an array call this variable, $events and then change this portion of code: if($month == $eventmonth){ //will only list month you specify in function. $out = "$day - $event<br>\n"; print $out; } To this: //will only list month you specify in function. if($month == $eventmonth) { // save each event for the month to the an array called events // we'll use the day as the key for the variable // the key will be used to sort the events in ascending order $events[$day] = $event; } Now after the foreach loop have this code: // order the keys in ascending order, remember the day is the key for the $events variable ksort($events); // display the events foreach($events as $day => $event) { echo "$day - $event<br />\n"; } Put it altogether an you get this: <?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; } } // order by day ksort($events); foreach($events as $day => $event) { echo "$day - $event<br />\n"; } } //Using the function getevents("Jan"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54859-solved-i-need-to-order-my-txt-file/#findComment-271372 Share on other sites More sharing options...
Schlo_50 Posted June 12, 2007 Author Share Posted June 12, 2007 Thank Christ for you! rofl Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/54859-solved-i-need-to-order-my-txt-file/#findComment-273223 Share on other sites More sharing options...
trq Posted June 12, 2007 Share Posted June 12, 2007 I take your problem is solved then? Quote Link to comment https://forums.phpfreaks.com/topic/54859-solved-i-need-to-order-my-txt-file/#findComment-273225 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.