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

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.