It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
我在mysql表中有几千行
在按升序选择记录后,我想按字母顺序将结果的每个部分格式化为这种格式

a
ab
abc
abcd
abcde
abcdef
-------------------------------------
b
bab
babc
babcd
babcdef
-------------------------------------
c
ca
cab
cabc
cabcd
cabcde
cabcdef
-------------------------------------


我需要做的是用线条将字母的每个部分抹掉。

最佳答案

在php(以及大多数其他语言)中,所有字符串都是完整的字符数组。因此$ string [0]将保存字符串的第一个字符。

<?php
$rows = get_rows_from_mysql();

$section = false;

foreach ($rows as $row) {

  if ($section != strtolower($row['name'][0])) {
    if ($section != false) echo "------------------------";
    $section = strtolower($row['name'][0]);
  }

  echo $section['name'];
}
?>


上面的代码无法处理数字或特殊字符(如!“#)的分组,但您应该明白这一点。

关于php - mysql对数据结果A-Z进行排序,并用行分割每个部分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16543672/

10-13 01:45