Here is my code:
$res = array();
while ($r = mysql_fetch_array($q)){
$currentletter = substr($name,0,1);
$res[$currentletter][] = $name;
}
foreach($res as $key=>$val){
/// here you add your li
/// here $key will give you letter and $val will give you name
echo $key;
foreach($val as $reqvals){
echo $reqvals;
echo "
";
}
}