Jump to content

[SOLVED] Sorting results of a query


Bhaal

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/151178-solved-sorting-results-of-a-query/
Share on other sites

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(); )

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,

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.