如何以第一个字母作为选择选项按字母顺序列出mysql数据

时间:2019-05-21 11:39:00

标签: php html mysql

我有一个登录页面,并希望显示学校作为选择选项。现在,学校的名称存储在mysql数据库中,我想用字母的第一个字母按字母顺序回显该学校的名称,但是每次尝试时,我的第一个字母都会自我重复

我试图去上学,然后得到第一个字母并同时回显两者,但不起作用

<?php

$queryy = SELECT * FROM school ORDER BY school;
$result = mysqli_query($connection,$queryy);
while ($row = mysqli_fetch_array($result)) {
$letter = strtoupper($row['school'][0]);
$aGroup[$letter][] = $row;
echo $aGroup[$letter][0]."<br />";
}
?>

我希望这些选项采用这种形式

A
Adajesus primary school
Adams secondary school
B
bubenduf boys school
benbee primary school
best inland school
C
child care school
L
life Streams College
love and hope school

但实际输出是

A
A
A
B
B
B
B
B
C
C
L
L
L

1 个答案:

答案 0 :(得分:0)

Change mysqli_query format
<select class="show-tick" required="" id="school_name" name="school_name" >
              <option value="">Choose School</option>
              <?php
              $queryy = "SELECT school_name FROM schools ORDER BY school_name";
              $result = mysql_query($queryy);
              while ($row = mysql_fetch_array($result)) {
                  ?>
                  <option value="<?php echo $row['school_id'] ?>"><?php echo $row['school_name'] ?></option>  
              <?php }
              ?>
          </select>