Sort table
Moderator: General Moderators
Sort table
hi,
I have a table I need to sort it ascending and descending The table contains two columns and Sorting should ve independent of each other.
The data is passed into table from txt file
Please help me with this
I have a table I need to sort it ascending and descending The table contains two columns and Sorting should ve independent of each other.
The data is passed into table from txt file
Please help me with this
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
feyd | Please use
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
[quote="tansoft"]please post your query here so that we can see that and help you out in that .....[/quote]
I am using the files for storing the data.
From files how to sort ascending and descending.
If I sort the $data as sort($data);
In column 2 of the table also gets altered.
I just wnat to sort the column 1 of the table.
I want to sort the single column ascendng or descending independent of the column from the tableCode: Select all
$data=file("db.txt");
foreach($data as $line){
$lines=explode("|",$line);
echo "$lines[0]";
}feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]- iknownothing
- Forum Contributor
- Posts: 337
- Joined: Sun Dec 17, 2006 11:53 pm
- Location: Sunshine Coast, Australia
You should beable to sort it fine if you do it before exploding the line. That way all data in Column 2, will stay with its linked data in Column 1, I assume thats what you want?
Eg.
Or do you want to completely scramble your table? If so, explode before sort.
Eg.
Code: Select all
//db.txt
sokodoku | halli
aaa | bbb
nuk | uuu
$data=file("db.txt");
foreach($data as $line){
sort($line);
$lines=explode("|",$line);
echo $lines[0]; //COLUMN 1
echo $lines[1]; //COLUMN 2
}
//result
aaa | bbb
nuk | uuu
sokodoku | halli
Last edited by iknownothing on Wed Aug 01, 2007 10:34 pm, edited 2 times in total.
I should use sort($data).iknownothing wrote:You should beable to sort it fine if you do it before exploding the line. That way all data in Column 2, will stay with its linked data in Column 1, I assume thats what you want?
Eg.
Or do you want to completely scramble your table? If so, explode before sort.Code: Select all
sokodoku | halli aaa | bbb nuk | uuu would turn into aaa | bbb nuk | uuu sokodoku | halli
If I use sort($data) 2nd column of table also gets altered
So How to sort only one column each time.
How to pass the query string in <a href=a.php?id=name&
- iknownothing
- Forum Contributor
- Posts: 337
- Joined: Sun Dec 17, 2006 11:53 pm
- Location: Sunshine Coast, Australia
so you want to split corresponding values?? In that case, explode the line first, then sort each column
To Sort 1 Column:
To Sort 1 Column:
Code: Select all
$data=file("db.txt");
foreach($data as $line){
$lines=explode("|",$line);
sort($lines[0]);
echo $lines[0]; //COLUMN 1 - SORTED
echo $lines[1]; //COLUMN 2 - NOT SORTED
}iknownothing wrote:so you want to split corresponding values?? In that case, explode the line first, then sort each column
To Sort 1 Column:
Code: Select all
$data=file("db.txt"); foreach($data as $line){ $lines=explode("|",$line); sort($lines[0]); echo $lines[0]; //COLUMN 1 - SORTED echo $lines[1]; //COLUMN 2 - NOT SORTED }
I am getting an warning message that sort() expects parameter 1 to be array
- iknownothing
- Forum Contributor
- Posts: 337
- Joined: Sun Dec 17, 2006 11:53 pm
- Location: Sunshine Coast, Australia
Try this:
Edit: Tested, It works, just needs HTML formatting
Edit: Tested, It works, just needs HTML formatting
Code: Select all
$data=file("db.txt");
$count = 0;
foreach($data as $line){
list($column1[$count ], $column2[$count ]) =explode("|",$line);
$count = $count + 1;
}
sort($column1);
for ( $i = 0; $i<3; $i++ ) {
echo $column1[$i] ;
echo $column2[$i] ;
}
Last edited by iknownothing on Wed Aug 01, 2007 11:05 pm, edited 1 time in total.
Is it not possible without using multisort..feyd wrote:array_multisort().
I need the sort of table independent of the columns.
When I sort the 1st column 2nd column should not change