Jump to content


Photo

Petsite


  • Please log in to reply
6 replies to this topic

#1 ItsWesYo

ItsWesYo
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 27 July 2006 - 08:31 PM

foreach($data as $name => $num){
	$num = number_format($num);
	$name = ucfirst(strtolower($name));
	$html .="{$name}: {$num}
";

Now. That code is for the total amount of pets. It displays each pet name with the total of each.

Example: Pet 1 - 5,000 .... Pet 2 - 7,500

How would I order the numbers from GREATEST to LEAST?

#2 gluck

gluck
  • Members
  • PipPipPip
  • Advanced Member
  • 216 posts

Posted 27 July 2006 - 08:47 PM

use the array sort functions.
I'm back baby!

#3 ItsWesYo

ItsWesYo
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 28 July 2006 - 01:49 AM

where would i put the 'SORT_NUMERIC' one at?

#4 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 28 July 2006 - 04:48 AM

I think that rsort($data); will also do...  :-\

Come to think of it, it messes with the keys I think, so it won't work... :(
I'm just a guy who likes to code for fun...

#5 lukelambert

lukelambert
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 July 2006 - 06:41 AM

Try arsort($data);. That will keep the keys intact.

http://us2.php.net/m...tion.arsort.php

#6 ItsWesYo

ItsWesYo
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 28 July 2006 - 07:11 PM

So, it would look like this, I assume:
foreach($data as $name => $num){
	$num = number_format($num);
	$name = ucfirst(strtolower($name));
	$html .="{$name}: {$num}
        arsort($data);
";


#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 July 2006 - 10:03 PM

Ummm , no.

If you actually read that code and think about what's happening

get next array element
sort the array
get next array element
sort the array
get next array element
sort the array
get next array element
sort the array
get next array element
sort the array
get next array element
sort the array
get next array element
sort the array
...
etc

then you might think "perhaps it's better to sort the array first then process the data in it."
arsort($data);
foreach($data as $name => $num){
	$num = number_format($num);
	$name = ucfirst(strtolower($name));
	$html .= "$name: $num <br/>";
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users