Show by alphabetical order table

1

I want to show in alphabetical order the contents of a column of a table in SQL. For example I have tried this but only two return me, whereas I have a lot more, the nomeid contains the COUNT of the database.

$y='a';     
    //for table users
    for($i=1;$i<=$nombreid;$i++)
    {       
        $requete    =   "SELECT * FROM contact WHERE (id=$i) AND nom LIKE '$y%'";   
        $resultat   =   $base_hndl->query($requete);    // 
        $affiche    =   $resultat->fetchArray();//  tableau 'affiche'

        if($affiche['id']!=0)
        {
            //write data 

            echo "<tr class=event bgcolor=$couleur align=left style='font-size:12px;font-family:helvetica'>\n";

            echo "<td title=\"$lib_id\"><a href=_compil_vcf.php?id=$affiche[id]>$affiche[id]</a></td>\n";
            echo "<td title=\"$lib_nom\">$affiche[nom]</td>\n";
            echo "<td title=\"$lib_prenom\">$affiche[prenom]</td>\n";
            echo "<td title=\"$lib_fonction\">$affiche[fonction]</td>\n";
            echo "<td title=\"$lib_societe\">$affiche[societe]</td>\n";
            echo "<td title=\"$lib_mobile\">$affiche[mobile]</td>\n";
            echo "<td title=\"$lib_mail\"><a href=mailto:$affiche[mail] title=\"$clic_for_mail\" >$affiche[mail]</a></td>\n";
            echo "</tr>\n"; 

            $couleur = get_couleur_fond($couleur,$couleur1,$couleur2);// change de couleur de fond
            $y++;
        }
    
asked by anonymous 01.12.2014 / 15:22

3 answers

3

The right thing would be to sort before, in the list that takes the IDs.

But if you use XGH technique to solve the problem, you can save the data in an array and sort it to show:

$affiche_tab = array();
$affiche_ord = array();

// Pega todos e reserva

for($i=1;$i<=$nombreid;$i++)
{       
    $requete    =   "SELECT * FROM contact WHERE (id=$i) AND nom LIKE '$y%'";   
    $resultat   =   $base_hndl->query($requete);    // 
    $affiche    =   $resultat->fetchArray();//  tableau 'affiche'

    if($affiche['id']!=0)
    {
        $affiche_tab[] = $affiche;
        $affiche_ord[] = $affiche['prenom']; //Aqui voce escolhe a ordem
    }
}

// Agora ordena
array_multisort( $affiche_ord, SORT_ASC, $affiche_tab ); 

// E mostra
foreach( $affiche_tab as $affiche ){
    echo "<tr class=event bgcolor=$couleur align=left style='font-size:12px;font-family:helvetica'>\n";
    echo "<td title=\"$lib_id\"><a href=_compil_vcf.php?id=$affiche['id']>$affiche['id']</a></td>\n";
    echo "<td title=\"$lib_nom\">$affiche['nom']</td>\n";
    echo "<td title=\"$lib_prenom\">$affiche['prenom']</td>\n";
    echo "<td title=\"$lib_fonction\">$affiche['fonction']</td>\n";
    echo "<td title=\"$lib_societe\">$affiche['societe']</td>\n";
    echo "<td title=\"$lib_mobile\">$affiche['mobile']</td>\n";
    echo "<td title=\"$lib_mail\"><a href=mailto:$affiche['mail'] title=\"$clic_for_mail\" >$affiche['mail']</a></td>\n";
    echo "</tr>\n"; 
    $couleur = get_couleur_fond($couleur,$couleur1,$couleur2);// change de couleur de fond
}
    
01.12.2014 / 17:15
1

Try using the query this way:

$requete= "SELECT * FROM contact WHERE (id=$i) ORDER BY prenom ASC";

In this way you will sort by the prenom field in ascending order, if you want descending, just change by:

$requete= "SELECT * FROM contact WHERE (id=$i) ORDER BY prenom DESC";
    
01.12.2014 / 16:39
0

Try something like this:

<?php
    //retorna a consulta ordenada já
    $sql = "SELECT * FROM contact ORDER BY prenom";

    //executa a query
    $query = mysql_query($sql);

    //percorre as posições da consulta
    while($row = mysql_fetch_array($query)){
        echo "<td title=\"$lib_nom\">$row[nom]</td>\n";
    }
?>
    
01.12.2014 / 17:05