It doesn't seem to have a problem with sorting, but it does column format and headings for each new letter.
Suppose $ arr contains your alphabetically sorted list with number keys. each element has index names' and 'link'. This should be a pretty safe guess for the data from the SQL query.
$firstLetter = -1; $desiredColumns = 4; //you can change this! $columnCount = (count($arr)+27)/$desiredColumns+1; echo "<table><tr><td>"; foreach($arr as $key => $cur) { if ($key != 0 && $key % desiredColumns == 0) echo "</td><td>"; if ($cur['name'][0] !== $firstLetter) { echo "<strong>$firstLetter</strong> <br />"; $firstLetter = $cur['name'][0]; } echo "<a href=".$cur['link'].">".$cur['name']."</a><br />"; } echo "</td><tr></table>";
You will have to treat the numbers as a special case, but this is an idea. If you use the template engine, obviously this is better, but I think you would mention it. This is a rough sketch, which makes pretty HTML not my thing.
--Query-- get table into $arr. I can't see your tables obviously, Im making assumptions if names nad stuff so you'll need to verify or change them $sql = "SELECT * FROM table T ORDER BY name"; $conn = //you should have this $res = mysql_query($sql, $conn); $arr = array(); while($row = mysql_fetch_assc($res) $arr[] = $row; // start above code here. This isn't safe for empty query responses or other error but it works
source share