Here is my code:
What would be the best way to do this?
If you are fetching row from the database why can not rather GROUP BY there and get the records. That's the preferred way of doing it.
Assuming that the column name is first_name
group by substr(first_name,1,1)
$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 "<br>";
}
}