Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    74
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Sorting an array/foreach statement

    Bit of a novice - sorry.

    I have a script that includes a foreach statement
    Code:
    foreach($products as $i => $f)
    The products are then printed into a table.

    $f is an array of variables obtained from the database. Currently it is sorted by the $f[id] value. I presume this is by default as there is no logic in the script for sorting the array.

    How can I sort $f by one of the other values, such as $[price]

    Thanks
    Rob

  • #2
    New Coder
    Join Date
    Aug 2003
    Location
    Derby, UK
    Posts
    97
    Thanks
    0
    Thanked 14 Times in 14 Posts
    The best way would be to modify the query that originally fills this array so that it fills it in the correct order. If that is not possible there are functions for sorting arrays with a user-defined sort function, see http://www.php.net/usort and particularly example #2

    HTH,

    Dai

  • Users who have thanked DaiWelsh for this post:

    robbiez (02-18-2010)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    The easiest way would be to just sort you're SQL when you're querying it.
    Aside from that, write a comparator and run it through a sort:
    PHP Code:
    function cmp($a$b)
    {
        return 
    $a['price'] - $b['price'];
    }

    uasort($products'cmp');
    foreach (
    $products AS $i => $f)
    {
    ..... 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    robbiez (02-18-2010)

  • #4
    New Coder
    Join Date
    Aug 2003
    Location
    Derby, UK
    Posts
    97
    Thanks
    0
    Thanked 14 Times in 14 Posts
    lol, same server time, but mine came first, so do I win, or are we both jinxed?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    o.O
    I got chown'd
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    New Coder
    Join Date
    Jul 2008
    Posts
    74
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thanks to both you. It has sorted my problem.

    Would it be possible to point me in the right direction to take it one step further.

    How would I go about implementing search options:
    Lowest Price First|Smallest item First , etc.

    I am not sure how to call an PHP function using an onclick event - is that even possible?? or will I have to use javascript??

    Rob

  • #7
    New Coder
    Join Date
    Aug 2003
    Location
    Derby, UK
    Posts
    97
    Thanks
    0
    Thanked 14 Times in 14 Posts
    You could do a javascript sort using AJAX to reload the data from the server and repopulate the table (or even do the sort within javascript) but more normally you would make the sort link reload the page from the server and then make the sort order field in the SQL query depend on the variable passed e.g.

    results.php?sort=price
    results.php?sort=size

    and then in your PHP check $_GET['sort'] and choose the sort field in the SQL query accordingly. If you are paginating or filtering or for some other reason need to retain more variables it gets more complex, then you can either tack all necessary vars to rebuild the results onto the url or probably simpler keep the results state in session and only change the sort order then regenerate when they click the link.

    HTH,

    Dai


  •  

    Posting Permissions

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