On Fri, 7 Nov 2003 13:52:35 +0100, "somaBoy MX" <no**@nonesuch.net>
wrote:
This may be a stupid question, but what is the easiest way to sort a mysql
resultset?
For example I have a resulset containing a colomn 'product_name_english' and
'product_name_french' and want to sort the result on one of these colums
based on a language parameter.
I know how to do this in a query ("ORDER BY") but how can I do this after
the query has been executed?
You're always best to get the DBMS to do the sorting if you can, as it
is optimised for that sort of thing (no pun intended :-) :
$sql = "select column from table where condition";
switch ( $lang )
{
case "english":
$orderby = "product_name_english";
break;
case "french":
$orderby = "product_name_french";
break;
default:
$orderby = "product_name_english";
break;
}
$sql .= $sql." order by ".$orderby;
Although from what you have described it sounds like your database
isn't fully normalised. If you decide to add another language later on
you'll need to alter your database schema and update your code in the
relevant places.
Create a product table, a language table and a productlanguage table
that links product and language, e.g.
PRODUCT
pid, price, etc.
LANGUAGE
lid, name
1 English
2 French
PRODUCTLANGUAGE
pid, lid, description
1 1 Widget
1 2 French Widget
2 1 Gizmo
2 2 French Gizmo
and so on.
--
David ( @priz.co.uk )
The Internet Prisoner Database:
http://www.priz.co.uk/ipdb/
The Tarbrax Chronicle:
http://www.tarbraxchronicle.com/