Schlo_50 Posted May 22, 2007 Share Posted May 22, 2007 I have produced a noteboard style calendar where all events of a particular month are called and then number representing the date is shown and then the event after it. I'd like to know if possible the code which will order the numbers from the lowest to highest number. <?php function getevents($eventmonth){ $file = file("data.txt"); foreach($file as $key => $val){ $data[$key] = explode("|", $val); $day = $data[$key][0]; $month = $data[$key][1]; $event = $data[$key][2]; if($month == $eventmonth){ $out = "$day - $event<br>\n"; print $out; } } } getevents("Jan"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52575-putting-lists-in-order/ Share on other sites More sharing options...
corbin Posted May 22, 2007 Share Posted May 22, 2007 Look into ksort. http://php.net/ksort (Also, you could maybe use sort, but ksort looks more suitable for what you're trying to sort.) Quote Link to comment https://forums.phpfreaks.com/topic/52575-putting-lists-in-order/#findComment-259407 Share on other sites More sharing options...
Schlo_50 Posted May 22, 2007 Author Share Posted May 22, 2007 So how would i successfully add this: (I know its not complete for each date) <?php $date = array("1st"=>"1st", "5th"=>"5th", "16th"=>"16th", "2nd"=>"2nd"); ksort($date); foreach ($date as $key => $val) { echo "$key = $val\n"; } ?> Into this: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title></title> <link href="rotary.css" rel="stylesheet" type="text/css" /> <style type="text/css"> <!-- body { background-color: #6191c2; } .style1 {color: #990000} .style2 { color: #990000; font-weight: bold; font-size: 10px; font-family: verdana; } --> </style></head> <body> <script language=JavaScript> <!-- var message="Right click disabled!"; /////////////////////////////////// function clickIE4(){ if (event.button==2){ alert(message); return false; } } function clickNS4(e){ if (document.layers||document.getElementById&&!document.all){ if (e.which==2||e.which==3){ alert(message); return false; } } } if (document.layers){ document.captureEvents(Event.MOUSEDOWN); document.onmousedown=clickNS4; } else if (document.all&&!document.getElementById){ document.onmousedown=clickIE4; } document.oncontextmenu=new Function("alert(message);return false") // --> </script> <p align="center" class="rotary"><u>Calendar of Events</u></p> <table width="342" border="1" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="338"><div align="left"><span class="rotary style1">January</span></div></td> </tr> <tr> <td><div align="left"><span class="rotary"> <?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. $dat = array("1"=>"1st", "5"=>"5th", "16"=>"16th", "2"=>"2nd"); ksort($dat); foreach ($dat as $key => $val) { echo "$key = $val\n"; } $out = "$day - $event<br>\n"; print $out; } } } //Using the function getevents("Jan"); ?> </span></div></td> </tr> </table><br /> <table width="342" border="1" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="338"><div align="left" class="style2">February</div></td> </tr> <tr> <td><div align="left"><span class="rotary"> <?php getevents("Feb"); ?> </span></div></td> </tr> </table><br /> <table width="342" border="1" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="338"><div align="left" class="style2">March</div></td> </tr> <tr> <td><div align="left"><span class="rotary"> <?php getevents("Mar"); ?> </span></div></td> </tr> </table><br /> <table width="342" border="1" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="338"><div align="left" class="style2">April</div></td> </tr> <tr> <td><div align="left"><span class="rotary"> <?php getevents("Apr"); ?> </span></div></td> </tr> </table><br /> <table width="342" border="1" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="338"><div align="left" class="style2">May</div></td> </tr> <tr> <td><div align="left"><span class="rotary"> <?php getevents("May"); ?> </span></div></td> </tr> </table><br /> <table width="342" border="1" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="338"><div align="left" class="style2">June</div></td> </tr> <tr> <td><div align="left"><span class="rotary"> <?php getevents("Jun"); ?> </span></div></td> </tr> </table><br /> <table width="342" border="1" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="338"><div align="left" class="style2">July</div></td> </tr> <tr> <td><div align="left"><span class="rotary"> <?php getevents("Jul"); ?> </span></div></td> </tr> </table><br /> <table width="342" border="1" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="338"><div align="left" class="style2">August</div></td> </tr> <tr> <td><div align="left"><span class="rotary"> <?php getevents("Aug"); ?> </span></div></td> </tr> </table><br /> <table width="342" border="1" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="338"><div align="left" class="style2">September</div></td> </tr> <tr> <td><div align="left"><span class="rotary"> <?php getevents("Sep"); ?> </span></div></td> </tr> </table><br /> <table width="342" border="1" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="338"><div align="left" class="style2">October</div></td> </tr> <tr> <td><div align="left"><span class="rotary"> <?php getevents("Oct"); ?> </span></div></td> </tr> </table><br /> <table width="342" border="1" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="338"><div align="left" class="style2">November</div></td> </tr> <tr> <td><div align="left"><span class="rotary"> <?php getevents("Nov"); ?> </span></div></td> </tr> </table><br /> <table width="342" border="1" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="338"><div align="left" class="style2">December</div></td> </tr> <tr> <td><div align="left"><span class="rotary"> <?php getevents("Dec"); ?> </span></div></td> </tr> </table> </body> </html> The dates are stored as 1st, 2nd-->31st in the .txt file. Quote Link to comment https://forums.phpfreaks.com/topic/52575-putting-lists-in-order/#findComment-259420 Share on other sites More sharing options...
Barand Posted May 22, 2007 Share Posted May 22, 2007 with those date try natsort(); <?php $date = array("1st"=>"1st", "5th"=>"5th", "16th"=>"16th", "2nd"=>"2nd"); natsort($date); foreach ($date as $key => $val) { echo "$key = $val\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52575-putting-lists-in-order/#findComment-259442 Share on other sites More sharing options...
Schlo_50 Posted May 22, 2007 Author Share Posted May 22, 2007 How do i edit that into the original script so that it works though? Quote Link to comment https://forums.phpfreaks.com/topic/52575-putting-lists-in-order/#findComment-259444 Share on other sites More sharing options...
Barand Posted May 22, 2007 Share Posted May 22, 2007 $file = file("data.txt"); natsort($file); Quote Link to comment https://forums.phpfreaks.com/topic/52575-putting-lists-in-order/#findComment-259455 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.