1

I am using a bespoke module (which has been inherited with the site) and the module sorts the articles shown based on the selected sort order from the XML params:

<field name="sortopt" type="list" default="none" label="Sort Options">
    <option value="none">Select a Sort option</option>
    <option value="priceasc">Price Ascending</option>
    <option value="pricedesc">Price Descending</option>
    <option value="titleasc">Title Ascending</option>
    <option value="titledesc">Title Descending</option>
    <option value="priceascfeatured">Price Ascending - Featured First</option>
    <option value="pricedescfeatured">Price Descending - Featured First</option>
</field>

I then create the products array:

$products[] = array('id' => $id, 'title' => $title, 'image' => $image,
'product_url' => $product_url, 'region' => $region, 
'featured' => $article->featured, 'product_price' => $product_price);

Then the sort order is:

    if($sortopt == 'priceasc') {

        array_multisort($sort['product_price'], SORT_NUMERIC, SORT_ASC, $products);

    // Sort Price DESC
    } elseif($sortopt == 'pricedesc') {

        array_multisort($sort['product_price'], SORT_NUMERIC, SORT_DESC, $products);

    // Sort Title ASC   
    } elseif($sortopt == 'titleasc') {

        array_multisort($sort['title'], SORT_NUMERIC, SORT_ASC, $products);

    // // Sort Title DESC
    } elseif($sortopt == 'titledesc') {

        array_multisort($sort['title'], SORT_NUMERIC, SORT_DESC, $products);

    // Sort region ASC (Default)
    } elseif($sortopt == 'none') {

        array_multisort($sort['title'], SORT_NUMERIC, SORT_ASC, $products);

    }

If an article is set to featured, I need to show this first in all order scenarios. I know I can use array_multisort to sort using featured and price but the following is showing the featured first, but not then putting them into the price sort order.

    // Sort Price ASC - Featured First
    elseif($sortopt == 'priceascfeatured') {
        array_multisort($sort['product_price'], SORT_NUMERIC, SORT_ASC, $products,
                        $sort['featured'], SORT_NUMERIC, SORT_DESC, $products);
    }
    // Sort Price DESC - Featured First
    elseif($sortopt == 'pricedescfeatured') {
        array_multisort($sort['product_price'], SORT_NUMERIC, SORT_DESC, $products,
                        $sort['featured'], SORT_NUMERIC, SORT_DESC, $products);
    }
2
  • I think you want $sort['region'] for the none condition. You might consider this slight change: <option value="regionasc">Select a Sort option</option> for consistency. Might you (in the future) consider a separate checkbox that offers "Featured First" separate from the select field? or are you happy to leave as is? Sep 25, 2018 at 23:09
  • I'm not a big fan of switch-case blocks, but because every condition is comparing $sortopt, it is a perfect candidate for single comparison handling. Sep 25, 2018 at 23:16

1 Answer 1

1

I've resolved this by doing the following for the sort order:

    // Sort Price ASC - Featured First
    elseif($sortopt == 'priceascfeatured') {
        array_multisort($sort['featured'], SORT_NUMERIC, SORT_DESC, $sort['product_price'], SORT_NUMERIC, SORT_ASC,$products);
    }
    // Sort Price DESC - Featured First
    elseif($sortopt == 'pricedescfeatured') {
        array_multisort($sort['featured'], SORT_NUMERIC, SORT_DESC, $sort['product_price'], SORT_NUMERIC, SORT_DESC,$products);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.