Alphabetical Ordering from a database

Discussion in 'PHP' started by travist6983, Feb 17, 2010.

  1. travist6983

    travist6983 New Member

    Joined:
    Feb 17, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I am trying to figure out how to alphabetize a client list that draws from my data base here is the code i am playing with if any one could help nudge me ;) in the right direction i would be forever grateful

    PHP:
     $query "SELECT id, firstName, email, lastName, LEFT(lastName, 1) AS first_char FROM userAccounts WHERE UPPER(LEFT(lastName, 1)) BETWEEN 'A' AND 'Z' OR LEFT(lastName, 1) BETWEEN '0' AND '9' ORDER BY lastName WHERE assignedAgent = ".$_SESSION['agent']['agentID'];
                                    
    $clients mysql_query($query);
                                    
    $current_char '';
                                    if( 
    mysql_num_rows($clients) > )
                                    {
                                        while( 
    $client mysql_fetch_array($clients) )
                                        {
                                            if (
    $client['first_char'] != $current_char) {
                                          
    $current_char $client['first_char'];
                                             echo 
    '<br />' strtoupper($current_char) . '<br />-----<br />';
                                        }
                                            echo 
    "<tr>";
                                            echo 
    "    <td>".$current_char['lastName']."</td>";
                                            echo 
    "    <td>".$client['firstName']."</td>";
                                            echo 
    "    <td>".$client['email']."</td>";
                                            echo 
    "    <td>".$client['group']."</td>";
                                            echo 
    "    <td align='center'><a href=\"editClient.php?clientID=".$client['id']."\">Edit Client</a></td>";        
                                            
    //echo "    <td><a href='clientWebforms.php?clientID=".$client['id']."'>View Webforms</a></td>";
                                            
    echo "</tr>";    
                                        }
                                    }
                                    else
                                        echo 
    "<tr><td colspan='3' align='center'><font color='red'>You have no clients assigned!  To have your clients assigned to you search for them using the 'Find Clients' button on the left navigation bar.</font></td>";
                                
    Thanks for any and all comments

    Travis
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I am still trying to understand what you are trying to do? I guess you need to know that SQL as a language is case insensitive and so order by clause will always work.
     
  3. travist6983

    travist6983 New Member

    Joined:
    Feb 17, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I am not really a php programmer but figured i would give this a try to eliminate some work from my co-workers.

    basically what i was trying to do was the order the clients alphabetically by last name instead of by there database ID this was the old statement that i am trying to edit
    $query = "SELECT lastName, id, firstName, email FROM userAccounts WHERE assignedAgent = ".$_SESSION['agent']['agentID'];

    and you obviously can see the new statement that i created from reading a bunch of forum posts.

    Any help would be greatly appreciated

    thanks

    Travis
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice