Search Result Ordering

Pete Nice
Pete Nice
GorzillaPosts: 5,230 in Help Me!
I need to add an ordering option to a set of search results which are based on the title of each result.

The tile is made up of three elements: YEAR/ SUPPLIER / ITEM NO

E.G. 15SUP003

What I need to do is have the year part descending and the supplier & item no ascending.

No sure how I would implement that in PHP - any ideas would be greatly welcomed.

Comments

  • calder12
    calder12
    Senior Member Posts: 13,506
    Is it always the exact same number of characters? You're looking at a bit of complicated code even if it is, but I can give it some thought.
  • pmek
    pmek
    Banned Posts: 9,297
    Easiest way to do this would be to have 3 separate fields for the parameters.
  • Pete Nice
    Pete Nice
    Gorzilla Posts: 5,230
    Yeah there's always the same number of characters. There's over a 1000 items in the database so it would mean having to apply those 3 fields to the existing items.

    This type of shit makes my head hurt.
  • calder12
    calder12
    Senior Member Posts: 13,506 edited August 2016
    <?php
      $myArray = array(
      array('ID' => 1, 'title' => '15SUP003', 'product' => 'car'),
      array('ID' => 2, 'title' => '15SUP004', 'product' => 'hat'),
      array('ID' => 3, 'title' => '14SUP003', 'product' => 'bunny'),
      array('ID' => 4, 'title' => '15KTR003', 'product' => 'frog'),
      array('ID' => 4, 'title' => '15KTR012', 'product' => 'widget'),
      array('ID' => 4, 'title' => '14KTR103', 'product' => 'train'),
      array('ID' => 4, 'title' => '10SUP093', 'product' => 'bucket')
    );
    
    $newArray = $myArray;
    $i = 0;
    foreach( $myArray as $array ) {
      $newArray[$i]['year'] = substr($array['title'], 0, 2);
      $newArray[$i]['supplier'] = substr($array['title'], 2, 3);
      $newArray[$i]['item'] = substr($array['title'], 5, 3);
      $i++;
    }
    
    $sort = array();
    foreach($newArray as $k=>$v) {
        $sort['year'][$k] = $v['year'];
        $sort['supplier'][$k] = $v['supplier'];
        $sort['item'][$k] = $v['item'];
    }
    
    array_multisort($sort['year'], SORT_DESC, $sort['supplier'], SORT_ASC, $sort['item'], SORT_ASC, $newArray);
    
    echo '<xmp>';
    print_r($newArray);
    

    This should do it. Having the fields separate saves a bit of the code, doubt it's much of a hit on performance though.

    This outputs the following, which as far as I can tell is right:
    rray
    (
        [0] => Array
            (
                [ID] => 4
                [title] => 15KTR003
                [product] => frog
                [year] => 15
                [supplier] => KTR
                [item] => 003
            )
    
        [1] => Array
            (
                [ID] => 4
                [title] => 15KTR012
                [product] => widget
                [year] => 15
                [supplier] => KTR
                [item] => 012
            )
    
        [2] => Array
            (
                [ID] => 1
                [title] => 15SUP003
                [product] => car
                [year] => 15
                [supplier] => SUP
                [item] => 003
            )
    
        [3] => Array
            (
                [ID] => 2
                [title] => 15SUP004
                [product] => hat
                [year] => 15
                [supplier] => SUP
                [item] => 004
            )
    
        [4] => Array
            (
                [ID] => 4
                [title] => 14KTR103
                [product] => train
                [year] => 14
                [supplier] => KTR
                [item] => 103
            )
    
        [5] => Array
            (
                [ID] => 3
                [title] => 14SUP003
                [product] => bunny
                [year] => 14
                [supplier] => SUP
                [item] => 003
            )
    
        [6] => Array
            (
                [ID] => 4
                [title] => 10SUP093
                [product] => bucket
                [year] => 10
                [supplier] => SUP
                [item] => 093
            )
    
    )
    
  • Pete Nice
    Pete Nice
    Gorzilla Posts: 5,230 edited August 2016
    Fuck me thanks Calder that's awesome.
  • calder12
    calder12
    Senior Member Posts: 13,506
    You're quite welcome :)

    Slightly less convoluted code:
    <?php
      $myArray = array(
      array('ID' => 1, 'title' => '15SUP003', 'product' => 'car'),
      array('ID' => 2, 'title' => '15SUP004', 'product' => 'hat'),
      array('ID' => 3, 'title' => '14SUP003', 'product' => 'bunny'),
      array('ID' => 4, 'title' => '15KTR003', 'product' => 'frog'),
      array('ID' => 5, 'title' => '15KTR012', 'product' => 'widget'),
      array('ID' => 6, 'title' => '14KTR103', 'product' => 'train'),
      array('ID' => 7, 'title' => '10SUP093', 'product' => 'bucket')
    );
    
    $sort = array();
    foreach($myArray as $k=>$v) {
        $sort['year'][$k] = substr($v['title'], 0, 2);
        $sort['supplier'][$k] = substr($v['title'], 2, 3);
        $sort['item'][$k] = substr($v['title'], 5, 3);
    }
    
    array_multisort($sort['year'], SORT_DESC, $sort['supplier'], SORT_ASC, $sort['item'], SORT_ASC, $myArray);
    
    echo '<xmp>';
    print_r($myArray);
    

    Only returns the original array:
    Array
    (
        [0] => Array
            (
                [ID] => 4
                [title] => 15KTR003
                [product] => frog
            )
    
        [1] => Array
            (
                [ID] => 5
                [title] => 15KTR012
                [product] => widget
            )
    
        [2] => Array
            (
                [ID] => 1
                [title] => 15SUP003
                [product] => car
            )
    
        [3] => Array
            (
                [ID] => 2
                [title] => 15SUP004
                [product] => hat
            )
    
        [4] => Array
            (
                [ID] => 6
                [title] => 14KTR103
                [product] => train
            )
    
        [5] => Array
            (
                [ID] => 3
                [title] => 14SUP003
                [product] => bunny
            )
    
        [6] => Array
            (
                [ID] => 7
                [title] => 10SUP093
                [product] => bucket
            )
    
    
  • Pete Nice
    Pete Nice
    Gorzilla Posts: 5,230
    Nice ;)
Sign In or Register to comment.
© Copyright 2003 - 2016 - DT by Kooc Media