SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Cape Cod
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem ordering a list with numbers and letters

    Hello,
    I have built a site for a client which is a auction house. When I first took the site on he told me that the lot numbers for the auction would be numbers but he said nothing about "sub-lots" so today I get an email that the lots are not in order basically what happened is this

    29, 30, 31, 32, 33, 34, 36, 37, 38, 39, 35a, 35b, 35c, 35d

    So my question is is there a way to get these values to go in numerical order even with the letters on the end? And if by chance there is will the letters be in order?

    I have not been able to find a solution except for actually have the DB set up for sub lots which I think would be a pain since the whole thing is already set up.

    Thanks for any help
    Don

  2. #2
    I Never Give Up roosevelt's Avatar
    Join Date
    May 2005
    Posts
    515
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can store the data in an array and then sort it, and process them.

    PHP Code:
    <?php

    //mysql_connect()
    //select_db etc..

    //$sql = mysql_query('SELECT * FROM table');
    //$list = mysql_fetch_array($sql);

    $list = array('29'' 30'' 31'' 32'' 33'' 34'' 36'' 37'' 38'' 39'' 35a'' 35b'' 35c'' 35d');

    sort($list);

    foreach(
    $list as $lot)
        echo 
    $lot.'<br />';

    ?>

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Cape Cod
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so I got it working just wondering if this is a ok way of doing it.

    I have a function that gets all of the lots for an auction and puts them into an array.

    $lots = get_all_lots($auction_id);

    I changes the function so it uses the lot number as the key

    Then I do this to get it in order and print what I want

    PHP Code:
        foreach($lots as $lot) {
        
    $lot_array[] = $lot[lot_number];
        }
        
    natsort($lot_array);

        foreach(
    $lot_array as $key => $value) {

        echo 
    $lots[$value][title];
           } 
    Does this look reasonable or is there a better way.

    Thanks
    Don

  4. #4
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have any insight into how you've structured your database, but why not just order the results when getting them from the database?
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  5. #5
    I Never Give Up roosevelt's Avatar
    Join Date
    May 2005
    Posts
    515
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    I don't have any insight into how you've structured your database, but why not just order the results when getting them from the database?
    Imagine does make a valid point. FYI, you can use ORDER BY col_name ASC to order the items. (lol, i totally missed dat, too much blogging lately)

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Cape Cod
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried that at first but it orders by the first number so it would look like this

    1, 13, 123, 2, 23, 25, 3, 35...

    The problem is I can not have this as a int field as I did in the beginning because my client decided to change the way he wanted it to work after it was done and it had to have some letters also so the data type had to be changed. This is how I ended up in this mess. If I had known these issues in the beginning I could have planned for them but in the beginning he said they would all be numbers.

    But it is working now

  7. #7
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can always use this function to sort your array of values:

    http://www.php.net/manual/en/function.natsort.php

    HTH.
    Yours truely
    Mário Ramos

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    I don't know how this would stand up with large data sets, but you could use:
    Code SQL:
    ORDER BY CAST(lot AS UNSIGNED INT) ASC, lot ASC
    or some variant thereof.
    Salathe
    Software Developer and PHP Manual Author.

  9. #9
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Cape Cod
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mjpr View Post
    You can always use this function to sort your array of values:

    http://www.php.net/manual/en/function.natsort.php

    HTH.
    I am actually using this function But what I did was make another array out of just what I wanted to sort then use this to order the multidimensional array for the data. I could not figure out how to use the natsort() function on a multidimensional array without doing this. If someone does know how I would love to learn it.

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    usort($rows'cmp_col_nat');

    function 
    cmp_col_nat($a$b) {
        return 
    strnatcasecmp($a['col'], $b['col']); // or strnatcmp()

    Use $b, $a to sort other way.

    You could use an object as a callback comparator if you want a more generic solution where you can set sort order or which array indice to compare. Or use create_function() for a quick n dirty way to do that stuff.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •