Group by first letter, alphabetically, best way?

前端 未结 2 942
悲&欢浪女
悲&欢浪女 2021-01-21 12:41

Here is my code:

相关标签:
2条回答
  • 2021-01-21 12:55

    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)
    
    0 讨论(0)
  • 2021-01-21 12:59
        $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>";
           }
        }
    
    0 讨论(0)
提交回复
热议问题