php / mysql number format question

Discussion in 'Web Design and Development' started by twoodcc, Sep 9, 2009.

  1. twoodcc macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #1
    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
     
  2. CANEHDN macrumors 6502a

    CANEHDN

    Joined:
    Dec 12, 2005
    Location:
    Eagle Mountain, UT
    #2
    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.
     
  3. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #3
    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
     
  4. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #4
    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
     

Share This Page