PHP/MySQL sort question
I have a MySQL database, reading it from a PHP page.
I need to sort on $lastname so the list that it returns is in alphabetical order. However, the way it's set up now (by the original programmer who seems to have crapped out on us) is that it reads directly from the db and echoes to the page, one line at a time. Obviously, it's going to be bloody hard to sort it that way.
So is there a way in PHP (which I am wholly unfamiliar with) to read into a temporary location, sort the damn stuff, and echo from there?
Please advise!
while ($myrow = mysql_fetch_array($result2)) {
$firstname = $myrow["firstname"];
$lastname = $myrow["lastname"];
$custcompany = $myrow["custcompany"];
$companyurl = $myrow["companyurl"];
$datetrans = $myrow["datetrans"];
$membershiplevel = $myrow["membershiplevel"];
if ($datetrans){
echo "<tr>
<td valign=\"top\"><b>$lastname</b>, $firstname</td>
<td valign=\"top\">$custcompany</td>
<td valign=\"top\">";
if ($companyurl != "http://"){
echo "<a href=\"$companyurl\" target=\"_blank\">website</a>";
}// end nested if
echo "</td>
<td valign=\"top\">$membershiplevel</td>
</tr>
";
}// end if
}//end while
Thanks!
Suzanne
Mark Hensler posted this at 07:32 — 9th August 2001.
He has: 4,048 posts
Joined: Aug 2000
Can you post the query for $result2?
You should be able to append the query with "ORDER BY field_name ASC"
ASC=ascending
DESC=descending
Suzanne posted this at 08:17 — 9th August 2001.
She has: 5,507 posts
Joined: Feb 2000
$usertable = 'members';
$query2 = "SELECT * FROM $usertable";
$result2 = MYSQL_QUERY($query2);
Is this what I'm looking to append? Sorry to be so daft but an example would be much appreciated.
Learning MySQL and PHP is on the "when I'm on mat leave" list...
Suzanne
Edited to add: I've tried the suggested commands (and looked them up in my books, thank you! it's nice to have at least a rough idea of what I'm looking for) and it doesn't seem to matter where I put it, it causes some weird nonvalid error report.
I'll keep trying, but a good example would be much appreciated!
Suzanne posted this at 09:04 — 9th August 2001.
She has: 5,507 posts
Joined: Feb 2000
Thanks to knowing what to look for, I went over to http://www.php.net and searched for "ORDER BY" and in the results was:
$result = mysql_query ("select * from phpcal where approved=0 order by sdato");
Which I adapted to:
$result2 = MYSQL_QUERY("SELECT * FROM members ORDER BY lastname, firstname");
Which WORKS!! I was missing the right way to type the darn thing, I guess.
WHEE HA! Thank you, Mark!
Suzanne
Mark Hensler posted this at 16:00 — 9th August 2001.
He has: 4,048 posts
Joined: Aug 2000
Another source to look at is http://www.mysql.com
They have pretty good documentation and examples.
Suzanne posted this at 00:45 — 10th August 2001.
She has: 5,507 posts
Joined: Feb 2000
This is pretty intuitive, once I get the syntax conventions sorted out.
Thanks!
Suzanne
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.