easy question. here is a snippit of code $res = $db->query('select * from '.$dtable.' where id > 1 order by time DESC'); PHP: I want to change this so it queries a field called word. but i want it listed so it will show words in a list by how many characters there is in the word. i think its string length? soo... wwwww rrr yyyyyyyyyyy uuuu pppppppppp would show up rrr uuuu wwwww pppppppppp yyyyyyyyyyy Thanks
save the result you get form the query in an array and use natsort($array) function on it. http://www.php.net/manual/en/function.natsort.php That should do the trick.
Actually ignore my first response. I wrote this function for you which will do the trick. $arr = array ("wwwww", "rrr", "yyyyyyyyyyy", "uuuu", "pppppppppp"); function compare_elements($a, $b) { if (strlen($a) == strlen($b)) { return 0; } return (strlen($a) < strlen($b)) ? -1 : 1; } usort($arr, compare_elements); print_r($arr); PHP: The output is what you expected Array ( [0] => rrr [1] => uuuu [2] => wwwww [3] => pppppppppp [4] => yyyyyyyyyyy ) PHP:
Hey, Can't you do it with a simple extra ORDER BY clause in the SQL? $res = $db->query('select * from '.$dtable.' where id > 1 order by time DESC, word ASC '); PHP: That ought to do it. ~ Thomas
Yea .. you are right .. How about: $res = $db->query('select * from '.$dtable.' where id > 1 order by time DESC, LENGTH(word) ASC ') PHP: ? Well.. doing something in the SQL query itself is faster than using extra PHP ode. ~ Thomas
That would work. Will not make much of difference if the list is not huge. Besides, its good to exercise your brain buy doing a bit of problem solving in PHP
I was looking for super simple. the array thing is a good reverence but im liking the query bit. Ill use that. 1+ for everyone if i never gave you one yet. Just give me a few to test this
KABLAMMMOOOOO. worked EXACTLY how i wanted. i edited it a bit. i just wanted it to show words by length, didnt matter what combo. THANK YOU SOOOOO MUCH (its the simple things that amuse php noobs...you remember how it was). 1+ for all :
Yup, always seemed to happen to me sometime ago. I made looong function with many loops to parse a hostname or something and after I finally got it to work, .. I found out about the parse_url function!! Great huh? That happened to me another time too when I was trying to read in a CSV file. Now .. I check for inbuilt functions for whatever I want to do, before I re-invent the wheel . ~ Thomas