在PHP中将MySQL结果分组为A-Z块

在PHP中将MySQL结果分组为A-Z块,php,mysql,split,grouping,alphanumeric,Php,Mysql,Split,Grouping,Alphanumeric,我有一个数千个结果的列表,我想用字母数字对它们进行分组,例如,我需要这样: 0-9 011示例 233例 65例 A. 阿尔伯特 阿尔弗雷德 安妮 B 本 伯蒂 伯特 按姓名排序,并逐个处理每个字母 $get_az = mysql_query("SELECT custom_22 FROM db_table1 ORDER BY custom_22 ASC"); $current_first_letter = null; while ($row = mysql_fetch_assoc($

我有一个数千个结果的列表,我想用字母数字对它们进行分组,例如,我需要这样:

0-9
  • 011示例
  • 233例
  • 65例
A.
  • 阿尔伯特
  • 阿尔弗雷德
  • 安妮
B
  • 伯蒂
  • 伯特

按姓名排序,并逐个处理每个字母

$get_az = mysql_query("SELECT custom_22 FROM db_table1 ORDER BY custom_22 ASC");
$current_first_letter = null;
while ($row = mysql_fetch_assoc($get_az)) { 
    $first_letter = strtolower(substr($row['custom_22'], 0, 1));
    if (preg_match("/[0-9]/", $first_letter)) { // detect digits
        $first_letter = "0-9";
    }
    if ($first_letter !== $current_first_letter) {
        if ($current_first_letter !== null) {
            echo '</ul>';
        }
        echo '<h1>' . $first_letter . '</h1>';
        echo '<ul>';
    }
    $current_first_letter = $first_letter;
    echo '<li>' . htmlentities($row['custom_22']) . '</li>';
}
if ($current_first_letter !== null) {
    echo '</ul>';
}
$get_az=mysql_query(“按自定义_22 ASC从db_表1中选择自定义_22”);
$current\u first\u letter=null;
而($row=mysql\u fetch\u assoc($get\u az)){
$first_letter=strtolower(substr($row['custom_22'],0,1));
如果(preg_匹配(“/[0-9]/”,$first_letter)){//检测数字
$first_letter=“0-9”;
}
如果($first_letter!=$current_first_letter){
if($current\u first\u letter!==null){
回声“”;
}
回显“.$first_字母”;
回声“
    ”; } $current\u first\u letter=$first\u letter; echo“
  • ”.htmlentities($row['custom_22'])。
  • ; } if($current\u first\u letter!==null){ 回声“
”; }
按姓名排序,并逐个处理每个字母

$get_az = mysql_query("SELECT custom_22 FROM db_table1 ORDER BY custom_22 ASC");
$current_first_letter = null;
while ($row = mysql_fetch_assoc($get_az)) { 
    $first_letter = strtolower(substr($row['custom_22'], 0, 1));
    if (preg_match("/[0-9]/", $first_letter)) { // detect digits
        $first_letter = "0-9";
    }
    if ($first_letter !== $current_first_letter) {
        if ($current_first_letter !== null) {
            echo '</ul>';
        }
        echo '<h1>' . $first_letter . '</h1>';
        echo '<ul>';
    }
    $current_first_letter = $first_letter;
    echo '<li>' . htmlentities($row['custom_22']) . '</li>';
}
if ($current_first_letter !== null) {
    echo '</ul>';
}
$get_az=mysql_query(“按自定义_22 ASC从db_表1中选择自定义_22”);
$current\u first\u letter=null;
而($row=mysql\u fetch\u assoc($get\u az)){
$first_letter=strtolower(substr($row['custom_22'],0,1));
如果(preg_匹配(“/[0-9]/”,$first_letter)){//检测数字
$first_letter=“0-9”;
}
如果($first_letter!=$current_first_letter){
if($current\u first\u letter!==null){
回声“”;
}
回显“.$first_字母”;
回声“
    ”; } $current\u first\u letter=$first\u letter; echo“
  • ”.htmlentities($row['custom_22'])。
  • ; } if($current\u first\u letter!==null){ 回声“
”; }
按姓名排序,并逐个处理每个字母

$get_az = mysql_query("SELECT custom_22 FROM db_table1 ORDER BY custom_22 ASC");
$current_first_letter = null;
while ($row = mysql_fetch_assoc($get_az)) { 
    $first_letter = strtolower(substr($row['custom_22'], 0, 1));
    if (preg_match("/[0-9]/", $first_letter)) { // detect digits
        $first_letter = "0-9";
    }
    if ($first_letter !== $current_first_letter) {
        if ($current_first_letter !== null) {
            echo '</ul>';
        }
        echo '<h1>' . $first_letter . '</h1>';
        echo '<ul>';
    }
    $current_first_letter = $first_letter;
    echo '<li>' . htmlentities($row['custom_22']) . '</li>';
}
if ($current_first_letter !== null) {
    echo '</ul>';
}
$get_az=mysql_query(“按自定义_22 ASC从db_表1中选择自定义_22”);
$current\u first\u letter=null;
而($row=mysql\u fetch\u assoc($get\u az)){
$first_letter=strtolower(substr($row['custom_22'],0,1));
如果(preg_匹配(“/[0-9]/”,$first_letter)){//检测数字
$first_letter=“0-9”;
}
如果($first_letter!=$current_first_letter){
if($current\u first\u letter!==null){
回声“”;
}
回显“.$first_字母”;
回声“
    ”; } $current\u first\u letter=$first\u letter; echo“
  • ”.htmlentities($row['custom_22'])。
  • ; } if($current\u first\u letter!==null){ 回声“
”; }
按姓名排序,并逐个处理每个字母

$get_az = mysql_query("SELECT custom_22 FROM db_table1 ORDER BY custom_22 ASC");
$current_first_letter = null;
while ($row = mysql_fetch_assoc($get_az)) { 
    $first_letter = strtolower(substr($row['custom_22'], 0, 1));
    if (preg_match("/[0-9]/", $first_letter)) { // detect digits
        $first_letter = "0-9";
    }
    if ($first_letter !== $current_first_letter) {
        if ($current_first_letter !== null) {
            echo '</ul>';
        }
        echo '<h1>' . $first_letter . '</h1>';
        echo '<ul>';
    }
    $current_first_letter = $first_letter;
    echo '<li>' . htmlentities($row['custom_22']) . '</li>';
}
if ($current_first_letter !== null) {
    echo '</ul>';
}
$get_az=mysql_query(“按自定义_22 ASC从db_表1中选择自定义_22”);
$current\u first\u letter=null;
而($row=mysql\u fetch\u assoc($get\u az)){
$first_letter=strtolower(substr($row['custom_22'],0,1));
如果(preg_匹配(“/[0-9]/”,$first_letter)){//检测数字
$first_letter=“0-9”;
}
如果($first_letter!=$current_first_letter){
if($current\u first\u letter!==null){
回声“”;
}
回显“.$first_字母”;
回声“
    ”; } $current\u first\u letter=$first\u letter; echo“
  • ”.htmlentities($row['custom_22'])。
  • ; } if($current\u first\u letter!==null){ 回声“
”; }
我会这样做:

将结果分组

while ($row = mysql_fetch_assoc($get_az)) {
    $firstLetter = strtolower(substr($row['custom_22'], 0, 1));
检查数字

    if (is_numeric($firstLetter)) {
        $firstLetter = '0-9';
    }

    if (isset($groups[$firstLetter]) === false) {
        $groups[$firstLetter] = array();
    }

    $groups[$firstLetter][] = $row['custom_22'];
}
只需在组上迭代并响应即可

foreach ($groups as $h1 => $items) {
    echo sprintf('<h1>%s</h1>', strtoupper(htmlspecialchars($h1)));
    echo '<ul>';
        foreach ($items as $item) {
            echo sprintf('<li>%s</li>', htmlspecialchars($item));
        }
    echo '</ul>';
}
foreach($h1=>$items的组){
echo sprintf(“%s”,strtoupper(htmlspecialchars($h1));
回声“
    ”; foreach($items作为$item){ echo sprintf(“
  • %s
  • ”,htmlspecialchars($item)); } 回声“
”; }
我会这样做:

将结果分组

while ($row = mysql_fetch_assoc($get_az)) {
    $firstLetter = strtolower(substr($row['custom_22'], 0, 1));
检查数字

    if (is_numeric($firstLetter)) {
        $firstLetter = '0-9';
    }

    if (isset($groups[$firstLetter]) === false) {
        $groups[$firstLetter] = array();
    }

    $groups[$firstLetter][] = $row['custom_22'];
}
只需在组上迭代并响应即可

foreach ($groups as $h1 => $items) {
    echo sprintf('<h1>%s</h1>', strtoupper(htmlspecialchars($h1)));
    echo '<ul>';
        foreach ($items as $item) {
            echo sprintf('<li>%s</li>', htmlspecialchars($item));
        }
    echo '</ul>';
}
foreach($h1=>$items的组){
echo sprintf(“%s”,strtoupper(htmlspecialchars($h1));
回声“
    ”; foreach($items作为$item){ echo sprintf(“
  • %s
  • ”,htmlspecialchars($item)); } 回声“
”; }
我会这样做:

将结果分组

while ($row = mysql_fetch_assoc($get_az)) {
    $firstLetter = strtolower(substr($row['custom_22'], 0, 1));
检查数字

    if (is_numeric($firstLetter)) {
        $firstLetter = '0-9';
    }

    if (isset($groups[$firstLetter]) === false) {
        $groups[$firstLetter] = array();
    }

    $groups[$firstLetter][] = $row['custom_22'];
}
只需在组上迭代并响应即可

foreach ($groups as $h1 => $items) {
    echo sprintf('<h1>%s</h1>', strtoupper(htmlspecialchars($h1)));
    echo '<ul>';
        foreach ($items as $item) {
            echo sprintf('<li>%s</li>', htmlspecialchars($item));
        }
    echo '</ul>';
}
foreach($h1=>$items的组){
echo sprintf(“%s”,strtoupper(htmlspecialchars($h1));
回声“
    ”; foreach($items作为$item){ echo sprintf(“
  • %s
  • ”,htmlspecialchars($item)); } 回声“
”; }
我会这样做:

将结果分组

while ($row = mysql_fetch_assoc($get_az)) {
    $firstLetter = strtolower(substr($row['custom_22'], 0, 1));
检查数字

    if (is_numeric($firstLetter)) {
        $firstLetter = '0-9';
    }

    if (isset($groups[$firstLetter]) === false) {
        $groups[$firstLetter] = array();
    }

    $groups[$firstLetter][] = $row['custom_22'];
}
只需在组上迭代并响应即可

foreach ($groups as $h1 => $items) {
    echo sprintf('<h1>%s</h1>', strtoupper(htmlspecialchars($h1)));
    echo '<ul>';
        foreach ($items as $item) {
            echo sprintf('<li>%s</li>', htmlspecialchars($item));
        }
    echo '</ul>';
}
foreach($h1=>$items的组){
echo sprintf(“%s”,strtoupper(htmlspecialchars($h1));
回声“
    ”; foreach($items作为$item){ echo sprintf(“
  • %s
  • ”,htmlspecialchars($item)); } 回声“
”; }
尝试在选择中使用
按自定义顺序\u 22
尝试在选择中使用
按自定义顺序\u 22
尝试在选择中使用
按自定义顺序\u 22
尝试在选择中使用
按自定义顺序\u 22
这看起来不错!我得到的唯一错误是
/[0-9]/
应该是
'/[0-9]/'
。但是它会导致区分大小写的字母出现问题,因此我在
$first\u letter
中添加了
strtolower
。谢谢这看起来不错!我得到的唯一错误是
/[0-9]/
应该是
'/[0-9]/'
。但是它会导致区分大小写的字母出现问题,因此我在
$first\u letter
中添加了
strtolower
。谢谢这看起来不错!我得到的唯一错误是
/[0-9]/
应该是
'/[0-9]/'
。但是它会导致区分大小写的字母出现问题,因此我在
$first\u letter
中添加了
strtolower
。谢谢这看起来不错!我得到的唯一错误是
/[0-9]/