1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to query words and have them ordered by how many characters there is

Discussion in 'PHP' started by Dirty-Rockstar, Aug 12, 2007.

  1. #1
    easy question. here is a snippit of code

    $res = $db->query('select * from '.$dtable.' where id > 1 order by time DESC');
    PHP:
    I want to change this so it queries a field called word. but i want it listed so it will show words in a list by how many characters there is in the word. i think its string length?

    soo...

    wwwww
    rrr
    yyyyyyyyyyy
    uuuu
    pppppppppp

    would show up

    rrr
    uuuu
    wwwww
    pppppppppp
    yyyyyyyyyyy


    Thanks :D
     
    Dirty-Rockstar, Aug 12, 2007 IP
  2. web_dev

    web_dev Peon

    Messages:
    230
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    web_dev, Aug 12, 2007 IP
  3. web_dev

    web_dev Peon

    Messages:
    230
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Actually ignore my first response. I wrote this function for you which will do the trick.

    $arr = array ("wwwww", "rrr", "yyyyyyyyyyy", "uuuu", "pppppppppp");
    
    
    function compare_elements($a, $b)
    {
        if (strlen($a) == strlen($b)) {
            return 0;
        }
        return (strlen($a) < strlen($b)) ? -1 : 1;
    }
    
    usort($arr, compare_elements);
    
    print_r($arr);
    PHP:
    The output is what you expected

    Array ( [0] => rrr [1] => uuuu [2] => wwwww [3] => pppppppppp [4] => yyyyyyyyyyy )
    PHP:
     
    web_dev, Aug 12, 2007 IP
  4. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hey,
    Can't you do it with a simple extra ORDER BY clause in the SQL?

    
    $res = $db->query('select * from '.$dtable.' where id > 1 order by time DESC, word ASC ');
    
    PHP:
    That ought to do it.

    ~
    Thomas
     
    coderlinks, Aug 12, 2007 IP
  5. web_dev

    web_dev Peon

    Messages:
    230
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Order by clause will order by their ascii values not by the length of the string.
     
    web_dev, Aug 12, 2007 IP
  6. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yea .. you are right .. How about:
    
    $res = $db->query('select * from '.$dtable.' where id > 1 order by time DESC, LENGTH(word) ASC ')
    
    PHP:
    ?
    Well.. doing something in the SQL query itself is faster than using extra PHP ode.

    ~
    Thomas
     
    coderlinks, Aug 12, 2007 IP
    Dirty-Rockstar likes this.
  7. web_dev

    web_dev Peon

    Messages:
    230
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    That would work.

    Will not make much of difference if the list is not huge. Besides, its good to exercise your brain buy doing a bit of problem solving in PHP:)
     
    web_dev, Aug 12, 2007 IP
    Dirty-Rockstar likes this.
  8. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I was looking for super simple. the array thing is a good reverence but im liking the query bit. Ill use that. 1+ for everyone if i never gave you one yet. Just give me a few to test this :p
     
    Dirty-Rockstar, Aug 12, 2007 IP
  9. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9

    KABLAMMMOOOOO. worked EXACTLY how i wanted. i edited it a bit. i just wanted it to show words by length, didnt matter what combo. THANK YOU SOOOOO MUCH :D

    (its the simple things that amuse php noobs...you remember how it was). 1+ for all :D: :D
     
    Dirty-Rockstar, Aug 12, 2007 IP
  10. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Yup, always seemed to happen to me sometime ago. I made looong function with many loops to parse a hostname or something and after I finally got it to work, .. I found out about the parse_url function!! Great huh? That happened to me another time too when I was trying to read in a CSV file. Now .. I check for inbuilt functions for whatever I want to do, before I re-invent the wheel :).

    ~
    Thomas
     
    coderlinks, Aug 13, 2007 IP