Sort table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

rash28
Forum Commoner
Posts: 38
Joined: Wed Aug 01, 2007 1:21 am

Sort table

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Look into the "ORDER BY" clause in SQL.
(#10850)
tansoft
Forum Newbie
Posts: 12
Joined: Sun Jul 29, 2007 1:04 am

Post by tansoft »

please post your query here so that we can see that and help you out in that .....
rash28
Forum Commoner
Posts: 38
Joined: Wed Aug 01, 2007 1:21 am

Post by rash28 »

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 table

Code: 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]
tansoft
Forum Newbie
Posts: 12
Joined: Sun Jul 29, 2007 1:04 am

Post 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
rash28
Forum Commoner
Posts: 38
Joined: Wed Aug 01, 2007 1:21 am

Post 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
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post 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.
Last edited by iknownothing on Wed Aug 01, 2007 10:34 pm, edited 2 times in total.
rash28
Forum Commoner
Posts: 38
Joined: Wed Aug 01, 2007 1:21 am

Post 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&
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post 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
}
tansoft
Forum Newbie
Posts: 12
Joined: Sun Jul 29, 2007 1:04 am

Post by tansoft »

Yeah, iknownothing is saying right ...... try this what he is saying i think that should work for you.
rash28
Forum Commoner
Posts: 38
Joined: Wed Aug 01, 2007 1:21 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post 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] ;
}
Last edited by iknownothing on Wed Aug 01, 2007 11:05 pm, edited 1 time in total.
rash28
Forum Commoner
Posts: 38
Joined: Wed Aug 01, 2007 1:21 am

Post by rash28 »

feyd wrote:array_multisort().
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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That's an odd sorting mechanism. :? I think you'll need separate buckets for each "column" so they can remain independent.
Post Reply