Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Sorting Mysql results

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Sorting Mysql results

Postby juansoul » Sun Jul 13, 2003 7:47 pm

I have a Database table with 3 keys
FirstName, LastName, EmailAddress

Im using a while loop to display the contents of the database on a page and it works just fine , but how do I display this results in alphabetical order???? this is the cide I have so far


Code: Select all
<?php
$Host = "localhost";
$User = "username";
$Password = "password";
$DBName = "dbname";
$TableName = "mailinglist";

$Link = mysql_connect ($Host, $User, $Password);
$Query = "SELECT * from $TableName";
$Result = mysql_db_query ($DBName, $Query, $Link);

print ("<table width=\"500\" border=\"1\">
  <tr>
    <td bgcolor=\"#CCCCCC\">First Name</td>
    <td bgcolor=\"#CCCCCC\">Last Name</td>
    <td bgcolor=\"#CCCCCC\">E-mail Address</td>
   <td bgcolor=\"#CCCCCC\">Delete</td>
  </tr>");

while ($Row = mysql_fetch_array ($Result)){
print ("<tr>");
print ("<td>$Row[FirstName]</td>");
print ("<td>$Row[LastName]</td>");
print ("<td>$Row[EmailAddress]</td>");
print ("<td><a href=\"mailinglistdeletesubscribersproccess.php?emailtodelete=$Row[EmailAddress]\">delete</a></td>");
print ("</tr>");
}


print ("</table>");

mysql_close ($Link);
?>


I need to display the results in alphabetical roder how Do I do that?
juansoul
New php-forum User
New php-forum User
 
Posts: 29
Joined: Tue Oct 08, 2002 6:33 pm

Postby Joel » Sun Jul 13, 2003 10:53 pm

Put it in your mysql query, and put it in the right forum.

Code: Select all
$Query = "SELECT * from $TableName ORDER BY firstname";


This is assuming you want to sort by last name, it is easily changeable as you will see.
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand

Postby bezmond » Sun Jul 13, 2003 11:51 pm

Joel wrote:Put it in your mysql query, and put it in the right forum.

Code: Select all
$Query = "SELECT * from $TableName ORDER BY firstname";


This is assuming you want to sort by last name, it is easily changeable as you will see.


that code isn't quite perfect,
Code: Select all
$Query = "SELECT * from $TableName ORDER BY firstname ASC";
would sert by the first name, going through the alphabet (A-Z).
User avatar
bezmond
Moderator
Moderator
 
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK

Postby Joel » Mon Jul 14, 2003 1:24 am

Isn't ASC by default?
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand

Postby Neil Hillman » Mon Jul 14, 2003 4:54 am

That's what I thought, it's always ascending unless specifide otherwise as in: "ORDER BY firstname DESC"
Neil Hillman
New php-forum User
New php-forum User
 
Posts: 4
Joined: Mon Jul 14, 2003 3:58 am
Location: Geneva, Switz.

Postby bezmond » Mon Jul 14, 2003 5:21 am

really? DIdn't know that :oops:
User avatar
bezmond
Moderator
Moderator
 
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK

Postby juansoul » Mon Jul 14, 2003 5:41 am

thanx A lot people! Sorry about posting this in the wrong forum
juansoul
New php-forum User
New php-forum User
 
Posts: 29
Joined: Tue Oct 08, 2002 6:33 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron