How do I sort this alphabeticly?

Discussion in 'PHP' started by Imozeb, May 26, 2010.

  1. #1
    I am querying data from a database. All of the data is stored in one column so I have:

    $data = $row['names'];

    So now $data contains the data from the `names` column. Is there anyway I could sort the data in the $data variable alphabeticly from A to Z?

    Thank you.
     
    Imozeb, May 26, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    SELECT * FROM table ORDER BY names DESC
    Code (markup):
     
    MyVodaFone, May 26, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm sorry but you did not understand my question. The data is all in one column in one row. Like if the database is a 5 X 5 grid, location 1 x 5 contains: bob, jill, randy, rick, john, abby. I need to sort that data (the names are separated by commas) alphabeticly. Is this possible?
     
    Imozeb, May 26, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    Checkout the php function sort();

    An example:

    
    <?php
    
    $data = array(q,w,x,e,r,t,y,u,i,o,p,g,f,d,h,s,a,j,k,l,z,c,v,b,n,m);
    sort($data);
    foreach ($data as $key => $val) {
        echo "" . $val . "\n";
    }
    
    ?>
    
    PHP:
    Will echo out: a b c d e f g h i j k l m n o p q r s t u v w y z

    So you could try this:
    
    $data = array($row['names']);
    sort($data);
    foreach ($data as $key => $val) {
        echo "" . $val . "\n";
    }
    
    PHP:
     
    MyVodaFone, May 26, 2010 IP
  5. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks. I'll try out your code soon.
     
    Imozeb, May 26, 2010 IP
  6. kidatum

    kidatum Peon

    Messages:
    61
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You'll have to store the result in a variable. Break it down using the coma and a space as the delimeter. Store each word in an array. Then sort that.
     
    kidatum, May 26, 2010 IP