Jump to content


Photo

Ordering results - all of them not just 1


  • Please log in to reply
10 replies to this topic

#1 Anidazen

Anidazen
  • Members
  • PipPipPip
  • Advanced Member
  • 79 posts

Posted 20 July 2006 - 09:17 AM

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?

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 July 2006 - 09:25 AM

Where is your data comming from? If you're pulling from a database, you can order it at that level:
SELECT * FROM `table` ORDER BY `age` DESC, `surname` ASC, `forename` ASC


#3 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 20 July 2006 - 09:25 AM

I beleave you should simply be able to use somthing like this..


SELECT * FROM table ORDER BY price, quantity DESC


Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#4 Anidazen

Anidazen
  • Members
  • PipPipPip
  • Advanced Member
  • 79 posts

Posted 20 July 2006 - 09:32 AM

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.

#5 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 July 2006 - 09:33 AM

Could you give us some sample data?

#6 Anidazen

Anidazen
  • Members
  • PipPipPip
  • Advanced Member
  • 79 posts

Posted 20 July 2006 - 09:50 AM

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.

#7 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 July 2006 - 10:06 AM

Hows this?
<?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>";
?>


#8 Anidazen

Anidazen
  • Members
  • PipPipPip
  • Advanced Member
  • 79 posts

Posted 20 July 2006 - 01:21 PM

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!

#9 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 July 2006 - 01:36 PM

Heres a commented version:
<?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>";
?>
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.

#10 Anidazen

Anidazen
  • Members
  • PipPipPip
  • Advanced Member
  • 79 posts

Posted 20 July 2006 - 11:31 PM

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.


$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];



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

#11 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 21 July 2006 - 06:39 AM

<?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];
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users