sandy1028 Posted August 1, 2007 Share Posted August 1, 2007 How to sort in ascending and descending order for the particular column when clicked on header...... The data is fetched from files $id = $_GET['id']; $lines = file('phonedb.txt'); echo "<ul id=\"containerul\"><li class=\"connection\">View Tree"; echo "<ul><li class=\"connection\"><a href=\"tree.php?id=name\">Name</a>"; echo "<ul>"; for($i=0;$i<sizeof($text_array); $i++) { echo "<li>--".$text_array[$i]['name']."</li>"; } echo "</ul></li>"; echo "<li class=\"connection\"><a href=\"tree.php?id=address\">Address</a>"; echo "<ul>"; for($i=0;$i<sizeof($text_array); $i++) { echo "<li>--".$text_array[$i]['address']."</li>"; } echo "</ul></li>"; Link to comment https://forums.phpfreaks.com/topic/62778-asc-and-desc-for-column/ Share on other sites More sharing options...
btherl Posted August 1, 2007 Share Posted August 1, 2007 You can sort using helper functions, like this: function cmp_name($a, $b) { return stricmp($a['name'], $b['name']); } usort($array, 'cmp_name'); This particular comparison function does a case insensitive comparison of the names in the array, and sorts in that order. Link to comment https://forums.phpfreaks.com/topic/62778-asc-and-desc-for-column/#findComment-312560 Share on other sites More sharing options...
sandy1028 Posted August 1, 2007 Author Share Posted August 1, 2007 Hi, I just want how to sort the column without multisort.... Individual column I should sort... But if I use the code in this way <?php $id = $_GET['id']; echo "<html>"; echo "<body bgcolor=\"#FFF0FF\" text=\"#000000\">"; $lines = file('phonedb.txt'); echo "<ul id=\"containerul\"><li class=\"connection\">View Tree"; echo "<ul><li class=\"connection\"><a href=\"tree.php?id=name\">Name</a>"; echo "<ul>"; if($id=="") { foreach($lines as $line) { $text_line=explode(":",$line); echo "<li>--$text_line[0]</li>"; } } if($id="name") { sort($lines); foreach($lines as $line) { $text_line=explode(":",$line); $text=explode(" ",$text_line[0]); for($i=0;$i<count($text);$i++) { echo "<li>--$text[$i]</li>"; } } } echo "</ul></li>"; echo "<li class=\"connection\"><a href=\"tree.php?id=address\">Address</a>"; echo "<ul>"; if($id=="") { foreach($lines as $line) { $text_line=explode(":",$line); echo "<li>--$text_line[1]</li>"; } } if($id="address") { sort($lines); foreach($lines as $line) { $text_line=explode(":",$line); $text=explode(" ",$text_line[1]); for($i=0;$i<count($text);$i++) { echo "<li>--$text[$i]</li>"; } } } ?> I am not able to sort the particular column Link to comment https://forums.phpfreaks.com/topic/62778-asc-and-desc-for-column/#findComment-312571 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.