how to alphabitical...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

how to alphabitical...

Post by Smackie »

Can someone help me on making a code that list users alphabitical(or however you spell it)?
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Please be more specific. Are the users listed in database? Is so, show the table structure and I am sure someone can help.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

if they're not in a db, you could always put the list thre and then do a select and order by name...
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

see im not done making the script yet im just trying to put a table together that when they register for my poetry site and submit a poem it will save there poem under there username in alphabitical order.. and yes there username will be in database...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

time to read your database's manual on ordering results... hint: ORDER BY
Wldrumstcs
Forum Commoner
Posts: 98
Joined: Wed Nov 26, 2003 8:41 pm

Post by Wldrumstcs »

Should be something like

Code: Select all

<?
mysql_connect("localhost","$username","$password") or die ("Unable to connect to MySQL server."); 
$db = mysql_select_db("$database") or die ("Unable to select requested database.");

$query=mysql_query("SELECT * FROM TABLENAME ORDER BY SUBSTRING_INDEX(username, ' ', -1),SUBSTRING_INDEX(username, ' ',1)");

while ($row = mysql_fetch_array($query)) &#123; 

$username=$row&#1111;"username"];

echo "
<tr>
<td>$username</td>
</tr>
";

$row_count++; 
&#125;

?>
It will go in alphabetical order by last name. It will take into consideration the first name as well if the last names are the same. You can change 'username' to whatever the column is called in your DB.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

For some reason it just keeps telling me
Unable to select requested database


why is that?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

did you set the database variable?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

mysql_connect("localhost","username","password)
or die(mysql_error());
mysql_select_db("database")
or die(mysql_error());

if you have something like that and all of your smurf is correct and you're still getting an error, it's likely that you don't have permissions set up correctly for the database.
Post Reply