rapids Posted November 21, 2009 Share Posted November 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
cags Posted November 21, 2009 Share Posted November 21, 2009 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); Quote Link to comment Share on other sites More sharing options...
salathe Posted November 21, 2009 Share Posted November 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
rapids Posted November 21, 2009 Author Share Posted November 21, 2009 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 Quote Link to comment Share on other sites More sharing options...
cags Posted November 21, 2009 Share Posted November 21, 2009 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; } Quote Link to comment Share on other sites More sharing options...
rapids Posted November 22, 2009 Author Share Posted November 22, 2009 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 Quote Link to comment Share on other sites More sharing options...
cags Posted November 22, 2009 Share Posted November 22, 2009 Instead of $array, you should be passing $items as thats where your storing your items. Also you should include the second parameters in quotes (yes, I realise I forgot on my first post). usort($items, 'by_price_asc'); Quote Link to comment Share on other sites More sharing options...
rapids Posted November 24, 2009 Author Share Posted November 24, 2009 Cags, sorry for the late answer The code is working now Thank you so much...you've helped me a lot Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.