|
 |
|
 |
10-06-2005, 02:46 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Aug 2003
Location: SF
Posts: 45
|
sorting question
I am using a character field in one of my tables(MySQL) and using numbers in that field also how can make the #'s come out in numerical order?
ex.
cnfg1, cnfg2, cnfg3...cnfg10, cnfg11, cnfg12
my query sorts them like
cnfg1, cnfg10, cnfg11, cnfg12, cnfg2, cnfg20, cnfg21...
i want them like
cnfg1, cnfg2, cnfg3, cnfg4,...cnfg11, cnfg12, cnfg13
is there a way for me to do that or will i have to have seperate fields?
|
|
|
10-06-2005, 03:00 PM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,475
|
i don't think mysql has a way to sort results like that, however you can make php do the work with natsort()
PHP Code:
<?
$array = array();
$result = mysql_query("select configname from configs");
while($row = mysql_fetch_assoc($result)) {
$array[] = $row['configname'];
}
natsort($array);
foreach($array as $each) {
echo $each . "<br />\n";
}
?>
__________________
Mike
|
|
|
10-06-2005, 03:28 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Aug 2003
Location: SF
Posts: 45
|
dude,
sde, you are the man! thank you so much!
|
|
|
10-06-2005, 10:02 PM
|
#4 (permalink)
|
|
Registered User
Join Date: Aug 2003
Location: SF
Posts: 45
|
one more question, how can i display the entire row?
|
|
|
10-06-2005, 10:16 PM
|
#5 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
|
If this were perl, it'd be a job for the Schwartzian Transform, but it's not so it depends on your data.
If the numbers you have are unique, you can build an array that has the field to sort on as the keys, and the rows of data as the values, sort the numbers and then iterate over the array of sorted numbers as keys to the array, pulling out the now sorted data.
There are a few implementations of this here . Otherwise you may have to add a decimal place or something to each number to ensure the keys are unique.
__________________
Stop intellectual property from infringing on me
|
|
|
10-07-2005, 08:15 AM
|
#6 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,475
|
PHP Code:
<?
$row_array = array();
$key_array = array();
$result = mysql_query("select configname from configs");
while($row = mysql_fetch_assoc($result)) {
// create an array of cofig names so you can
// use the natsort later
$key_array[] = $row['configname'];
// create an array of the rows using the
// config name as the key
$row_array[$row['configname']] = $row;
}
natsort($key_array);
foreach($key_array as $key) {
// now you can access the row
// by using the $key var
$config = $row_array[$key];
}
?>
__________________
Mike
|
|
|
10-07-2005, 02:25 PM
|
#7 (permalink)
|
|
Registered User
Join Date: Aug 2003
Location: SF
Posts: 45
|
i found a query that worked it out for me.
Code:
$query = 'SELECT * FROM memfiles WHERE `file` LIKE \'%psms_cnfg%\''
. ' ORDER BY LPAD(field,7,"0")';
|
|
|
10-10-2005, 06:58 AM
|
#8 (permalink)
|
|
Centurion Nova Prime
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
|
A different question is do you really need the character portion in the field? If this is a table storing multiple sets of type codes, for example, it makes sense. Anyway, the other way around the LPAD in the sort is to store the data with leading zeros out to the maximum length you'll ever see in your lifetime. You get the same result, but the ORDER by will do the sort without a function on the field.
Just my 2 cents.
__________________
It takes 2 points to draw a straight line, but at least 3 points to draw a conclusion.
|
|
|
10-10-2005, 07:36 AM
|
#9 (permalink)
|
|
Registered User
Join Date: Aug 2003
Location: SF
Posts: 45
|
thats how i was originally going to do it, with the leading zeroes. however i have a page where all of my cnfgs are listed in a table. then i have another area where users can do a search on a particular cnfg. the user knows the cnfg as cnfg1 and not cnfg001. and, yes, i am using multiple sets of type codes besides for just cnfg(i don't know if that was what you were asking).
|
|
|
10-10-2005, 02:52 PM
|
#10 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
|
Well, if you still want to store items with leading zeroes but not show them to the user, you could transform the field before the user sees it.
PHP Code:
preg_replace( "/([A-Za-z]+)0*(\d+)/", "$1$2", $configname);
__________________
Stop intellectual property from infringing on me
|
|
|
10-10-2005, 03:05 PM
|
#11 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,475
|
i'm wondering why there is a number in the name anyway. why don't you just use a field in the table for a config number?
__________________
Mike
|
|
|
10-10-2005, 03:15 PM
|
#12 (permalink)
|
|
Registered User
Join Date: Aug 2003
Location: SF
Posts: 45
|
i could do that but was trying to avoid it. you see there is more than just "cnfg" in that table. i also have prcnfg, nwcnfg, etc...so there would prcnfg1, cnfg1, nwcnfg1 and so on, all in the same table. and i want it to be unique so that there cannot be two cnfg1 entries in the table. does it make any sense?
|
|
|
10-12-2005, 08:47 AM
|
#13 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,475
|
i need schema to know what you're talking about. i don't know if you're storing just names of configs, or the text of the configs in ths table. you say prcnfg1, cnfg1, nwcfng1 in the same table.. do you mean in the same row of the table?
__________________
Mike
|
|
|
10-12-2005, 11:09 AM
|
#14 (permalink)
|
|
Registered User
Join Date: Aug 2003
Location: SF
Posts: 45
|
my columns are as follows:
field name-stores the name of the config(ex. cnfg1)
definition-stores the defintion of the file(ex. .T. = credit cards are on .F. = credit cards are off)
file-store the name of the file where the config is held(ex. psms_cnfg.mem)
its a pretty basic little table, but to answer your question no two configs are ever in the same row.(obvisouly!)
|
|
|
10-12-2005, 12:10 PM
|
#15 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
|
I think sde is suggesting that you could rearrange your table to look like:
name - cnfg, prcnfg, etc
number - 1, 2, 3, ... 8000, etc
definition
file
Such that to generate the full name cnfg1 it's a combination of 2 fields. THat way you can let the database handle storing the numbers and sorting properly, and also easily get all of a specific type of cnfg file.
__________________
Stop intellectual property from infringing on me
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 10:23 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|