$result = mysql_query("SELECT col FROM table ORDER BY col");
$lastFoundLetter = '';
while($row = mysql_fetch_array($result)) {
//get the first letter of the current record
$firstLetter = substr($row['col'], 0, 1);
if ($firstLetter != $lastFoundLetter) {
//you've started a new letter category
if ($lastFoundLetter != '') {
//if there's a real value in $lastFoundLetter, we need to close the previous div
echo "</div>";
}
echo "<div id='" . strtoupper($firstLetter) . "'>";
echo strtoupper($firstLetter) . "<br/>";
$lastFoundLetter = $firstLetter;
}
echo $row['col']. "<br/>";
}
//close the last div
if ($lastFoundLetter != '') {
echo "</div>";
}