Bhaal Posted March 26, 2009 Share Posted March 26, 2009 Scenario: table with artist names (and other fields); some names have "The" (as in 'The Dumb Koders'), others have "A" ('A Wilhelm Scream') but most are just a name ('Joe Smith'). I'm using eregi to take the 'The' and place it after the actual name - so it changes 'The Beatles' to 'Beatles, The' - for the purpose of sorting. Except now I don't know how to sort these results - since they are strings. So the sort() function doesn't work. The entire code: <? $sql = "select distinct artistName, song_file_name1, song_file_name2, song_file_name3, song_title1, song_title2, song_title3, desc, sync from indies where ((artistName like '%$srch%')) order by indies.artistName ASC"; $result_artist = mysql_query($sql) or die ($sql . mysql_error()); while ($row = mysql_fetch_array($result_artist)) { $artName = eregi ("^(The|An|A)+ (.*)$", $row['artistName'], $regs) ? $regs[2].", ".$regs[1] : $row['artistName']; print $artName . "<br /> "; } mysql_close(); ?> How can I 're-sort' $artName after running an eregi on it? As I understand it, $artName is not an array therefore sort() doesn't work. Is there a 'simple' way of resorting the eregi results before printing to the page? (Otherwise, the eregi is pretty much useless.) Thanks in advance. Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted March 26, 2009 Share Posted March 26, 2009 Put it in an array first, don't display right away. After placing everything in an array, sort it. After sorting, that's the time you display it. Quote Link to comment Share on other sites More sharing options...
Bhaal Posted March 26, 2009 Author Share Posted March 26, 2009 Put it in an array first, don't display right away. After placing everything in an array, sort it. After sorting, that's the time you display it. Would you mind explaining further? Put 'it' in an array 'first'. Is 'it' $artName? Is 'first' before the print statement - but still inside the while? Or is 'first' before the eregi function? ('Cause I've tried all those: $artName = array(); ) Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted March 26, 2009 Share Posted March 26, 2009 Do this: <?php // all other code and note the changes below $artNameList = array(); while ($row = mysql_fetch_array($result_artist)) { $artName = eregi ("^(The|An|A)+ (.*)$", $row['artistName'], $regs) ? $regs[2].", ".$regs[1] : $row['artistName']; $artNameList[] = $artName; } // do the sorting here using PHP sorting function sort() sort($artNameList); foreach($artNameList as $key => $item) { print $item. "<br /> "; } ?> hope this helps. Jay, Quote Link to comment Share on other sites More sharing options...
Bhaal Posted March 26, 2009 Author Share Posted March 26, 2009 Mark it done! Thank you Jay. Now how do I mark this 'solved'? Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted March 26, 2009 Share Posted March 26, 2009 lol... that one am not familiar about. I think there's a button somewhere you can click. Your welcome. ^^ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.