我有一个查询,该查询获取由last_name排序的所有记录。现在,我想创建一个循环,将这些结果按姓氏的第一个字母分组,并在该组上方显示字母,即
A ----------- Albert Alfred C ----------- Charles D ----------- Delta etc...
谢谢!
按顺序对结果lastname进行MySQL排序,并跟踪第一个字母的变化PHP:
lastname
MySQL
PHP
<?php $rs = mysql_query("SELECT * FROM mytable ORDER BY lastname"); while ($rec = mysql_fetch_assoc($rs)) { if ($initial !== strtoupper(substr($rec['lastname'], 0, 1)) { $initial = strtoupper(substr($rec['lastname'], 0, 1)); print "$initial\n"; } print $rec['lastname'] . "\n"; } ?>