cmgmyr Posted July 24, 2006 Share Posted July 24, 2006 1. Sorry for the kinda stupid question, but I have been coding all weekend and I can't really think very well any more...so here it goes.I have a database of users that I want an alphabetically separated list of...0-9-----namenameA-----namenameand so on... What would the best thing to do this with? I was thinking with an array of 0-Z and seeing if the name of the user falls under it, but I don't know.Any ideas? Thanks,-Chris Link to comment https://forums.phpfreaks.com/topic/15439-alphabetical-lists/ Share on other sites More sharing options...
scs Posted July 24, 2006 Share Posted July 24, 2006 Well when you get data from your database it is in an array. So just use a array sorting function. checking it out here:http://us3.php.net/manual/en/function.sort.php Link to comment https://forums.phpfreaks.com/topic/15439-alphabetical-lists/#findComment-62582 Share on other sites More sharing options...
trq Posted July 24, 2006 Share Posted July 24, 2006 [quote]So just use a array sorting function[/quote]You can do the sort in the query. Its defining where the headings are that will cause problems. Maybe something like.....[code=php:0]<?php $sql = "SELECT * FROM names ORDER BY `name` DESC"; if ($result = mysql_query($sql)) { $header = FALSE; while ($row = mysql_fetch_assoc($result)) { if (!$header) { $header = substr($row['name'],0,1); $prevheader = substr($row['name'],0,1); } if ($header <> $prevheader) { echo strtoupper($header)."<br />"; echo "--------<br />"; $prevheader = $header; } echo $row['name']."<br />"; } }?>[/code]Not tested, probably a bit messy, but should give you the idea. Link to comment https://forums.phpfreaks.com/topic/15439-alphabetical-lists/#findComment-62586 Share on other sites More sharing options...
cmgmyr Posted July 24, 2006 Author Share Posted July 24, 2006 Thanks guys I will try that out. Here is my query if that helps any.[code]$query = "SELECT users.userid, users.name, profile.type, profile.url, profile.url_description, profile.image, profile.image_description, profile.mp3 FROM users INNER JOIN profile on (users.userid = profile.userid) ORDER BY users.name ASC";[/code]Thanks,-Chris Link to comment https://forums.phpfreaks.com/topic/15439-alphabetical-lists/#findComment-62588 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.