Jump to content

Archived

This topic is now archived and is closed to further replies.

Anidazen

Ordering results - all of them not just 1

Recommended Posts

Hello.

I haven't been able to find any information on this, yet I would have thought it to be a common topic. Anyway hopefully someone can help me

I want to order some generated input for example by price.


All I have seen on the net how to sort an associative array. I definitely need to store and sort more than two fields. Surely there must be a way to order results?

Share this post


Link to post
Share on other sites
Where is your data comming from? If you're pulling from a database, you can order it at that level:
[sql]SELECT * FROM `table` ORDER BY `age` DESC, `surname` ASC, `forename` ASC[/sql]

Share this post


Link to post
Share on other sites
I beleave you should simply be able to use somthing like this..


SELECT * FROM table ORDER BY price, quantity DESC


Regards
Liam

Share this post


Link to post
Share on other sites
Sorry, I should have been more specific. The data is generated inside the PHP script, there's no database involved in that way.

I need to take several bits of information and order it, keeping it all together intact.

Share this post


Link to post
Share on other sites
Could you give us some sample data?

Share this post


Link to post
Share on other sites
Sure.

Basically, I am trying to produce like a mini price comparison project.


I need to store an URL, seller name, price, stock status, amount - and then order it by price.

Share this post


Link to post
Share on other sites
Hows this?
[code]<?php
$array = array(array("ashop.com","A Shop","1.20","In Stock","5"),
array("bshop.com","B Shop","3.20","In Stock","3"),
array("cshop.com","C Shop","2.20","In Stock","10"),
array("dshop.com","D Shop","0.20","Out of Stock","8")
);

$tmp = array();

foreach($array as $k => $v) {
$tmp[$k] = $v[2];
}

asort($tmp);
$new = array();

foreach($tmp as $k => $v) {
$new[] = $array[$k];
}

echo "<pre>"; print_r($new); echo "</pre>";
?>[/code]

Share this post


Link to post
Share on other sites
Looks great and thanks very much for the help!

That went slightly over my head - not 100% sure I can follow it, but I will trust it works.

How do I get back the input? ie. first place match full set of data?


Thanks very much again!

Share this post


Link to post
Share on other sites
Heres a commented version:
[code]<?php
//An array of arrays containing our data
$array = array(array("ashop.com","A Shop","1.20","In Stock","5"),
array("bshop.com","B Shop","3.20","In Stock","3"),
array("cshop.com","C Shop","2.20","In Stock","10"),
array("dshop.com","D Shop","0.20","Out of Stock","8")
);

$tmp = array(); //A temporary array we will be using

//Loop through our main array filling our temp array with just the prices,
//setting the temp array keys the same as the keys in our main array.
foreach($array as $k => $v) {
$tmp[$k] = $v[2];
}

//Sort our temp array (of prices) from lowest to highest,
asort($tmp);
$new = array(); //An array that will contain all of the data in order

//Loop through our temp array of prices, filling our new array with the full contents of our original array,
//this time ordered by price
foreach($tmp as $k => $v) {
$new[] = $array[$k];
}

//Show the contents of our final array
echo "<pre>"; print_r($new); echo "</pre>";
?>[/code]
Here, $new[0] will allways hold the details of the entery with the cheapest price, so $new[0][1] holds the shop name, the shop URL is in $new[0][0] and $new[0][2] holds the price.

Share this post


Link to post
Share on other sites
I couldn't get this to work. Probably just me messing it up.

Here's a snippet of code incase it helps further. I did $pricearray = array(); at the top, and then tried filling it up like this. Don't see why this failed.


[code]$pricearray[7] = array("Store Name", "http://www.storeurl.com/", $useprice, $useamount, $priceper, "In Stock");


$tmp = array();

foreach($pricearray as $k => $v) {
$tmp[$k] = $v[2];
}

asort($tmp);

$new = array();

foreach($tmp as $k => $v) {
$new[] = $array[$k];
}


echo "Best price: <a href='" . $new[0][1] . "'>" .  $new[0][0] . "</a> (" . $new[0][3] . " for \$" . $new[0][2] . ") - " . $new[0][5];
[/code]



Output I get is all fields I tried there blank. Any ideas? :/

Share this post


Link to post
Share on other sites
[code]<?php
//You forgot to edit this line when you changed the arrays name:
foreach($tmp as $k => $v) {
    $new[] = $array[$k];
}

//It needs to be:
foreach($tmp as $k => $v) {
    $new[] = $pricearray[$k];
}
?>[/code]

Share this post


Link to post
Share on other sites

×

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.