0

Im trying to print a names from sql on php page and sort it in categoris
My rows:

1 anna
2 alena
3 benny
4 dolev
5 niv

I want to print it like this:

A
alena
anna

B
benny

D
dolev

N
niv

EDIT:
I tried

while($row_keke = mysql_fetch_array($result_skey002))
{
    $name = $row_keke['name2'];

    if (in_array($name[0], $heb_a_z)) {
    echo $name[0]."<br />".$name."<br /><br />";
    }
}

But you see what the problem with this ..
Thanks for everybody .. ! :)

4

1 回答 1

0

可以尝试类似:

$lastFirst = '';

while($row_keke = mysql_fetch_array($result_skey002))
{

  $name = $row_keke['name2'];

  if($name[0] != $lastFirst){
     echo "<br />".strtoupper($name[0])."<br />";
     $lastFirst = $name[0];
  }

  echo $name."<br />";

}

我没有对此进行测试,但它应该可以达到您正在寻找的结果。

于 2013-07-20T17:47:19.587 回答