Page 1 of 2
Sort table
Posted: Wed Aug 01, 2007 1:29 am
by rash28
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
Posted: Wed Aug 01, 2007 2:41 am
by Christopher
Look into the "ORDER BY" clause in SQL.
Posted: Wed Aug 01, 2007 3:50 am
by tansoft
please post your query here so that we can see that and help you out in that .....
Posted: Wed Aug 01, 2007 10:07 pm
by rash28
feyd | Please use 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 table
Code: Select all
$data=file("db.txt");
foreach($data as $line){
$lines=explode("|",$line);
echo "$lines[0]";
}
feyd | Please use 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]
Posted: Wed Aug 01, 2007 10:16 pm
by tansoft
Can you also post the sample data which is present in .txt file so that we can see in which form data is coming and how we are parsing that in the code.
Thanks
Posted: Wed Aug 01, 2007 10:22 pm
by rash28
tansoft wrote:Can you also post the sample data which is present in .txt file so that we can see in which form data is coming and how we are parsing that in the code.
Thanks
This is the text file
sokodoku | halli
aaa | bbb
nuk | uuu
Posted: Wed Aug 01, 2007 10:24 pm
by iknownothing
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.
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
Or do you want to completely scramble your table? If so, explode before sort.
Posted: Wed Aug 01, 2007 10:29 pm
by rash28
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.
Code: Select all
sokodoku | halli
aaa | bbb
nuk | uuu
would turn into
aaa | bbb
nuk | uuu
sokodoku | halli
Or do you want to completely scramble your table? If so, explode before sort.
I should use sort($data).
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&
Posted: Wed Aug 01, 2007 10:33 pm
by iknownothing
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
}
Posted: Wed Aug 01, 2007 10:37 pm
by tansoft
Yeah, iknownothing is saying right ...... try this what he is saying i think that should work for you.
Posted: Wed Aug 01, 2007 10:46 pm
by rash28
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
Posted: Wed Aug 01, 2007 10:54 pm
by feyd
Posted: Wed Aug 01, 2007 11:00 pm
by iknownothing
Try this:
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] ;
}
Posted: Wed Aug 01, 2007 11:01 pm
by rash28
Is it not possible without using multisort..
I need the sort of table independent of the columns.
When I sort the 1st column 2nd column should not change
Posted: Wed Aug 01, 2007 11:22 pm
by feyd
That's an odd sorting mechanism.

I think you'll need separate buckets for each "column" so they can remain independent.