Jump to content

Recommended Posts

Here is the code i am working on, which parse and outputs a xml feed:

$SafeQuery = urlencode($resturl);
$pxml = simplexml_load_file($SafeQuery);
foreach ($pxml as $product) {
        $link  = $product->linkurl;
        $title = $product->productname;
        $price = $item->price;
        $imgURL = $product->imageurl;
        $results .="<div><a href=\"$link\"> $title<IMG
border=\"0\" src=\"$imgURL\"></a></br>Price:$price</div>";

 

What i want to do is to sort products by the string price , and also define sort order (asc/desc).

I tried to convert to an array then use asort but it's not working for me.

Any ideas are greatly appreciated

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/182420-sort-simplexml-output-by-price/
Share on other sites

Using a usort should work. Completely untested, but should give you an idea even if it's not perfect.

 

function by_price_asc($a, $b) {
    if ($a['price'] == $b['price']) {
        return 0;
    }
    return ($a['price'] < $b['price']) ? -1 : 1;
}

function by_price_desc($a, $b) {
    if ($a['price'] == $b['price']) {
        return 0;
    }
    return ($a['price'] < $b['price']) ? 1 : -1;
}

usort($array, by_price_asc);

Since we're playing with SimpleXML, things aren't quite so simple (oh the irony).  You'll have to pull out the products to an array, then sort that array on price (probably using usort as cags mentioned). You say that you've already tried to "convert to an array", that's a good starting point.

Cags, Salathe, thanks for your fast answers.

 

Cags, your solution looks great , but for that i will need to output it into an array and i'm  having some problems with the simplexml object conversion into an array.

 

This is what i already tried and it's not working...it may be stupid, i'm pretty noob at this part :)

 

$sortarray = array()

foreach ($sortarray as $product) {

$sortarray[] = $product->price;

...and then sorting

 

 

 

 

Actually my code doesn't require an array of prices, it requires an array of arrays (which can be equated to objects) that have a price item in them. Which you should be able to generate using something like...

 

$items = array();
foreach ($pxml as $product) {
   $item = array();
   $item['linkurl'] = $link = $product->linkurl;
   $item['title'] = $title = $product->productname;
   $item['price'] = $price = $item->price;
   $item['imgURL'] = $imgURL = $product->imageurl;
   $items[] = $item;
}

Cags, thanks again for your help

..I'm still lost in this thing

 

I'm using this code

$items = array();
foreach ($pxml as $product) {
   $item = array();
   $item['linkurl'] = $link = $product->linkurl;
   $item['title'] = $title = $product->productname;
   $item['price'] = $price = $item->price;
   $item['imgURL'] = $imgURL = $product->imageurl;
   $items[] = $item;
}

function by_price_asc($a, $b) {
    if ($a['price'] == $b['price']) {
        return 0;
    }
    return ($a['price'] < $b['price']) ? -1 : 1;
}

function by_price_desc($a, $b) {
    if ($a['price'] == $b['price']) {
        return 0;
    }
    return ($a['price'] < $b['price']) ? 1 : -1;
}

usort($array, by_price_asc);

 

...and i'm getting the error usort() [function.usort]: The argument should be an array  on line

usort($array, by_price_asc);

 

I made a dozen of changes without success and I'm sure i'm doing something wrong..

Please, Cags or anybody who could clarify what should i do.

Thank you guys, you're awesome

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.