2013-07-16 18 views
9

Ho una tabella membro in cui sto memorizzando tutti i dettagli su un membro compreso il suo nome, email, telefono ecc. Voglio che il nome apparirà come un gruppo alfabetico saggio. Come mostrato nell'esempio.Elenco alfabetico di gruppo in ordine alfabetico in php

Posso ordinare il campo in ordine alfabetico utilizzando l'ordine di ASC ma come ottenerli in un gruppo di alfabeto.

Tutti i suggerimenti sono i benvenuti. Sto usando php.

+2

Quali gruppi? Vuoi raggrupparli nel tuo database, in un array, nell'output? Hai già un codice? – GolezTrol

+1

I problemi di visualizzazione dei dati sono spesso gestiti al meglio a livello di applicazione - ad es. con un semplice loop che agisce su una lista ordinata. – Strawberry

+0

Questo livello di applicazione o livello di presentazione? @Strawberry –

risposta

-1

si può fare con la query di selezione come in questo esempio:

$query = "SELECT * FROM `user` ORDER BY `name` ASC ;"; 
+0

Ho già fatto che –

+0

OP desidera il gruppo di risultato alfabetico e 'ORDER ASC' restituiscono tutti i dati raccolti non raggruppati. –

+0

puoi var_dump il tuo risultato e postarlo qui e posso darti una sceneggiatura. –

4

forse è un brutto modo di farlo, ma può aiutare a ora.

<?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; 
    } 

?> 
+0

In $ letter = strtoupper ($ row [0] [0]); Prendo il nome e la prima lettera, quindi posso raggrupparlo in ordine alfabetico. Spero di averti aiutato! – Lompa

+0

Non sono un gran programmatore ma, personalmente, penso che sia un buon modo! – Strawberry

+0

Era per tenerlo semplice e facile da leggere. Ci sono altri modi per farlo meglio, ma è un modo più veloce per farlo, credo. – Lompa

2

Se si vuole fare questo, all'interno del vostro SQL;

SELECT SUBSTRING(name, 1, 1) as alpha, name from 'user' GROUP BY SUBSTRING(name, 0, 2), name order by 'alpha', 'name' 

e in 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...' 
    */ 

?> 

Spero che questo aiuti.

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>"; 
} 
Problemi correlati