我有一个会员表,我正在存储有关会员的所有详细信息,包括其名称,电子邮件,电话等。我希望该名称将以字母顺序显示。如示例所示。
A
Alan
Alex
Amar
Andy
B
Bob
Brad
C
Calvin
Clerk
D
E
我可以按照ASC的顺序按字母顺序对字段进行排序,但如何以字母表的形式获取它们。
欢迎提出任何建议。我正在使用php。
答案 0 :(得分:4)
这可能是一种糟糕的方式,但它现在可以帮助你。
<?php
$aGroup = array(
"A" => array(),
"B" => array(),
"C" => array(),
"D" => array(),
"E" => array(),
// until "Z"
);
$result = mysql_query("SQL as you want");
while ($row = mysql_fetch_array($result)) {
$letter = strtoupper($row[0][0]); // I supose that the first column is the name
$aGroup[$letter][] = $row;
}
?>
答案 1 :(得分:2)
如果您想在SQL中执行此操作;
SELECT SUBSTRING(name, 1, 1) as alpha, name from 'user' GROUP BY SUBSTRING(name, 0, 2), name order by 'alpha', 'name'
并在php中
<?php
$temp = array(); // would also generate a dynamic array
$result = mysql_query("SELECT SUBSTRING(name, 1, 1) as alpha, name from 'user' GROUP BY SUBSTRING(name, 0, 2), name order by 'alpha', 'name'"
while ($row = mysql_fetch_array($result)) {
$temp[$row['alpha']][] = $row['name'];
}
/* this would create array such as;
'A'
--> 'Adam'
--> 'Apple'
'B'
--> 'Ba...'
--> 'Be...'
*/
?>
希望这有帮助。
答案 2 :(得分:1)
这个问题应该有答案 - How to display an Array under alphabetical letters using PHP?
选择回答:
$previous = null;
foreach($array as $value) {
$firstLetter = substr($value, 0, 1);
if($previous !== $firstLetter) echo "\n".$firstLetter."\n---\n\n";
$previous = $firstLetter;
echo $value."\n";
}
答案 3 :(得分:0)
try this
/* Get the letter user clicked on and assign it a variable called $sort */
$sort = $_REQUEST['letter'];
/* Let's check if variable $sort is empty. If it is we will create a query to display all customers alphabetically ordered by last name. */
if($sort == ""){
$qry= "SELECT * FROM tbl_customers ORDER BY lastname ASC ";
}else{
/* if varible $sort is not empty we will create a query that sorts out the customers by their last name, and order the selected records ascendingly. */
$qry = "SELECT * FROM tbl_customers WHERE lastname LIKE '$sort%' ORDER BY lastname ASC";
}
/* Notice the use of '%' wilde card in the above query "LIKE '$sort%'". */
//next step is to execute the query.
$execute = mysql_query($qry) or die(mysql_error());
/* Before we display results let's create our alphabetical navigation. The easiest way to create the navigation is to use character codes and run them through the "for" loop. */
echo "<p>";
for ($i = 65; $i < 91; $i++) {
printf('<a href="%s?letter=%s">%s</a> | ',
$PHP_SELF, chr($i), chr($i));
}
echo "</p>";
/* now we are ready to display the results. Since out tbl_customers table has only three fileds we will display the results in a paragraphs. In the real world you may need to display the results in a table.
To display the results we will use "do while" loop to fetch the results. If no customers are found we will display an error message. */
if(mysql_num_rows($execute)>0){
do{
echo "<p>" .$result['id']. " " .$result['firstname']. " " .$result['lastname']. "</p>";
}while($result = mysql_fetch_assoc($execute));
}else{
echo "<p>No customer found.</p>";
}
答案 4 :(得分:-1)
你可以使用你的选择查询来做这个例子:
$query = "SELECT * FROM `user` ORDER BY `name` ASC ;";