ofs Posted July 3, 2008 Share Posted July 3, 2008 I've got a script selecting two columns (name, url) from a database, and then using while(mysql_fetch_assoc()) to output each row. I want to know how to order the output alphabetically by the NAME column, but ignore the word "The" before any of the names. For example: Banana The Apple Orange The Lemon Will be re-arranged to: The Apple Banana The Lemon Orange It doesn't matter to me whether this sort is done in the MySQL query, or after the fact. Any ideas? Thanks. Link to comment https://forums.phpfreaks.com/topic/113010-solved-ignore-quotthequot-while-alphabetically-sorting-an-associative-array-from-an-sql-query/ Share on other sites More sharing options...
vbnullchar Posted July 3, 2008 Share Posted July 3, 2008 well i have a workaround but its working SELECT name, url, SUBSTRING(name,3) as name2 FROM table ORDER BY SUBSTRING(name,3) ASC [code] [/code] Link to comment https://forums.phpfreaks.com/topic/113010-solved-ignore-quotthequot-while-alphabetically-sorting-an-associative-array-from-an-sql-query/#findComment-580513 Share on other sites More sharing options...
ofs Posted July 3, 2008 Author Share Posted July 3, 2008 I appreciate the reply, but the problem with that is that not all of the names start with the word "The", only a few of them. Link to comment https://forums.phpfreaks.com/topic/113010-solved-ignore-quotthequot-while-alphabetically-sorting-an-associative-array-from-an-sql-query/#findComment-580525 Share on other sites More sharing options...
miracle_potential Posted July 3, 2008 Share Posted July 3, 2008 $query = mysql_query("SELECT * FROM table ORDER BY name DESC") or die(mysql_error()); while($row = mysql_fetch_assoc($query){ echo str_replace('the ', ' ', $row['name']); } --EDIT-- Oops sorry misunderstood the question hang on Link to comment https://forums.phpfreaks.com/topic/113010-solved-ignore-quotthequot-while-alphabetically-sorting-an-associative-array-from-an-sql-query/#findComment-580532 Share on other sites More sharing options...
miracle_potential Posted July 3, 2008 Share Posted July 3, 2008 $query = mysql_query("SELECT * FROM table NOT RLIKE '^[the ]' AS c.name ORDER BY c")or die("error"); while($row = mysql_fetch_assoc($query){ echo $row['c']; } Not sure if this is going to work to be totally honest because I havnt tested it and I'm not a Regex programmer but its deffo a hint as to where you should be looking. Hope ya get the idea I'm shattered! :-X :-\ Link to comment https://forums.phpfreaks.com/topic/113010-solved-ignore-quotthequot-while-alphabetically-sorting-an-associative-array-from-an-sql-query/#findComment-580543 Share on other sites More sharing options...
ofs Posted July 3, 2008 Author Share Posted July 3, 2008 I tried that and it didn't work, but you helped me in finding a way to do it: SELECT name, url FROM shows ORDER BY REPLACE(name, 'The ', '') I never knew that function existed, but it works perfectly. And since it's only in the ORDER BY clause, it doesn't output the replaced name. Thanks! Link to comment https://forums.phpfreaks.com/topic/113010-solved-ignore-quotthequot-while-alphabetically-sorting-an-associative-array-from-an-sql-query/#findComment-580556 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.