Jump to content

Asc and desc for column


sandy1028

Recommended Posts

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

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.

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

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.