I am successfully returning results from my database using the following code, but I want the results to be listed alphabetically by surname.
I have tried to add ORDER BY surname to $query without success.
I am now stuck as I'm not sure if the SORT function should be used somewhere in the while loop.
Any help would be much appreciated......
$query = "SELECT name.surname, name.forename, name.extention
FROM name
WHERE name.departmentID = ".$dept ;
$result = mysql_query($query);
while ($record = mysql_fetch_assoc($result)) {
while (list ($fieldname, $fieldvalue)=each($record)){
echo "$fieldvalue ";
}
echo"<BR>";
Sorting alphabetically
Moderator: General Moderators
This should work:
Code: Select all
$query="SELECT surname, forename, extenction FROM name WHERE departmentID = ".$dept." ORDER BY 1";Thanks very much.
It's interesting it sorts from 1 and not 0.
Do you have any suggestions as to the best approach for formatting the results?
I want to put a comma after the surname to make it more readable.
Because the whole result is being returned by $fieldvalue I'm not sure what approach I should be looking towards.
Thanks again.
It's interesting it sorts from 1 and not 0.
Do you have any suggestions as to the best approach for formatting the results?
I want to put a comma after the surname to make it more readable.
Because the whole result is being returned by $fieldvalue I'm not sure what approach I should be looking towards.
Thanks again.
There are as many good suggestions as you can possibly imagine.
If you want to pot comma after the surrname try this:
BTW: Read this ->viewtopic.php?t=21171
If you want to pot comma after the surrname try this:
Code: Select all
while ($record = mysql_fetch_assoc($result)) {
foreach($record as $fname => $fvalue) {
echo $fvalue.($fname == 'surname' ? ", ":"");
}
echo"<br />";
}BTW: Read this ->viewtopic.php?t=21171