Page 1 of 1

how to alphabitical...

Posted: Thu Feb 24, 2005 8:08 pm
by Smackie
Can someone help me on making a code that list users alphabitical(or however you spell it)?

Posted: Thu Feb 24, 2005 8:10 pm
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.

Posted: Thu Feb 24, 2005 8:12 pm
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...

Posted: Thu Feb 24, 2005 8:18 pm
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...

Posted: Thu Feb 24, 2005 8:21 pm
by feyd
time to read your database's manual on ordering results... hint: ORDER BY

Posted: Thu Feb 24, 2005 9:00 pm
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.

Posted: Thu Feb 24, 2005 9:16 pm
by Smackie
For some reason it just keeps telling me
Unable to select requested database


why is that?

Posted: Thu Feb 24, 2005 9:18 pm
by feyd
did you set the database variable?

Posted: Thu Feb 24, 2005 9:20 pm
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.