Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
well i'm having trouble here. i have a number that i want to put in the number_format so it has a thousand separator. i only know of 2 ways to do this:

in mysql use the FORMAT function. this does the trick, but then i can't sort the number.

the other is in php, using the number_format(); the problem is that the number i want to use this function on is in an array.

any ideas on how to do this?

thanks
 
The best way to do it is sort using the value in the database. And then format the number as your echoing out the number. So your db value is: 34.7654. As you loop through your result set and format the value to the thousandth value. I hope that makes sense.

If you need more info, you can post either code or more info on what exactly you're doing.
 
here is some of the code:

Code:
$result = mysql_query("
	SELECT 
	  b.name AS album_name
	, a.name AS artist_name
	, SUM(s.play_count) as play_count
	, count(s.id) AS songs
	FROM `".$database_table_prefix."song` s
	JOIN `".$database_table_prefix."album` b
	ON s.album = b.id
	JOIN `".$database_table_prefix."artist` a
	ON s.artist = a.id
	GROUP BY b.id
	ORDER BY play_count DESC
") or die(mysql_error());
$grid = new grid;
$grid->columns = array(
	'Album' => 'album_name'
	,'Artist' => 'artist_name'
	,'# of Songs' => 'songs'
	,'Plays' => 'play_count'
);
while ($data = mysql_fetch_object($result)) {
	$grid->items[] = $data;
}

and the play_count is the number i want to format
 
Change:

SUM(s.play_count) as play_count

To:

format(SUM(s.play_count),2) as play_count

Here is help on MySQL's format command if you need to customize the output to suit your needs. Note, this is untested - I wrote this in a hurry.

-jim
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.