ORDER BY length(field) ?

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

Post Reply
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

ORDER BY length(field) ?

Post by The Monkey »

How would I do a mysql_query that ordered by the length of text in a particular field?

I have a submenu script that displays all the pages in a directory of my website, however, for stylistic reasons, I want it to be ordered by the length of the title for each page on the submenu.

Any 'relatively simple' code that would work?

- Monkey
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

i'm pretty sure ORDER BY LENGTH(field) will work.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

Or it might be ORDER BY LEN(field)
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

I'm not getting LENGTH(field) to work.

Could someone give me the whole $query var?

Thank you.
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Post by bla5e »

ORDER BY <feild> LENGTH
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Post by bla5e »

$sql = "SELECT * FROM <table> ORDER BY <feild> LENGTH";
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

bla5e wrote:$sql = "SELECT * FROM <table> ORDER BY <feild> LENGTH";
That code does not work.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"SELECT * FROM `test` ORDER BY LENGTH( `something` )"

works perfectly for me...
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

As Feyd said, LENGTH() will work just fine - if you're using multi-byte characters (i.e. a foreign language character set) then use CHAR_LENGTH() instead, as LENGTH gives you an incorrect reading in this instance.
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

Ok: It works now.

Thank you. Now I must figure how to deal with the i character being narrower than the o character... it's messing up the way I WANT my site to work... :(
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Welcome to non-proportional fonts. Try using PRE tags if you absolutely must have fixed-width characters (or set the font to be something like Courier or Terminal but realise you'll exclude Mac users, etc).
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

Ah... ok.

I have a sorta-solution. In the db, I put ? after the titles to "jack" up the character length. Then, after Ordering By the length of characters, I strip out the ?.

It doesn't always work, but it does make it look a LOT better, and by golly it's better than nothing!
Post Reply