SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Thread: php mysql print value as table

  1. #1
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php mysql print value as table

    Hi,

    I have selected mysql database table values as following
    Code:
    client    amount month      year
    
    joe       $50    January    2005
    nick      $52    Feburary   2005
    rubble    $49    march      2005
    nick      $52    January    2005
    nick      $52    March      2005
    rubble    $42    Feburary   2005
    joe       $30    January    2006
    nick      $32    Feburary   2006
    rubble    $39    march      2006
    som       $39    April      2006
    joe       $25    January    2006
    nick      $22    Feburary   2006
    rubble    $19    march      2006
    som       $19    April      2006
    joe       $52    Feburary   2005
    joe       $52    march      2005
    rubble    $40    January    2005
    I want the above values to print as
    Code:
    2005        Joe   Nick  rubble  
    
    January     $50    $52   $49
    Feburary    $52    $52   $40 
    March       $52    $52   $40
    and so on for each year in different table. The year as field type for month and client as field type for amount. any idea please help.
    Last edited by divinequran; Feb 19, 2009 at 07:12.

  2. #2
    From Italy with love bronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    8,645
    Mentioned
    77 Post(s)
    Tagged
    4 Thread(s)
    order the query by year, month and client, and then loop through the result with PHP, constructing the output the way you want it to be.

  3. #3
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have tried to select using order by year,count and month the mysql query prints the same result, how to print it as required using loops or coding totally confused.

  4. #4
    SitePoint Member Tavicu's Avatar
    Join Date
    Nov 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i don't understand what do you want to make!

    Do you want to print all order by month or?

    edit: oh ... you want to print the total $ makes in one month for each user?

  5. #5
    From Italy with love bronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    8,645
    Mentioned
    77 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by divinequran View Post
    i have tried to select using order by year,count and month the mysql query prints the same result
    Post the query you've got so far.

  6. #6
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to print it the total $ makes in one month for each user and for each year tavicu.

    this is the query i have used
    Code:
    select client,amount,month,year from paid group by client, month, year

  7. #7
    From Italy with love bronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    8,645
    Mentioned
    77 Post(s)
    Tagged
    4 Thread(s)
    Code:
    SELECT 
        client
      , `month`
      , `year`
      , SUM(amount) AS amount
    FROM paid 
    GROUP BY client, `month`, `year`
    ORDER BY `year`, `month`, client
    Try this query. I put backticks around the month and year field names, because if I remember well these are reserved mysql keywords.

  8. #8
    SitePoint Member Tavicu's Avatar
    Join Date
    Nov 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have a small problem ... the problem is because you put $ ...
    the amount need to be only with numbers ... if it with numbers you can select with count!

    Edit: Will work with code posted by guido2004 but if you make what i say ... to remove $ from database.

  9. #9
    From Italy with love bronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    8,645
    Mentioned
    77 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Tavicu View Post
    the problem is because you put $ ...
    the amount need to be only with numbers ...
    Good one, Tavicu!
    divinequran, do you really save the amount in the DB with the $ sign in front? Don't. Make the amount field a numeric one.

  10. #10
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no, i dont add $ sign in DB i specified if to get an idea for those who read it, i will just try it tomorrow and get back to you.
    Last edited by divinequran; Feb 20, 2009 at 11:10.

  11. #11
    From Italy with love bronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    8,645
    Mentioned
    77 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by divinequran View Post
    no, i dont add $ sign in DB i specified if to get an idea for those who read it, i ll just try it tomorrow and get back to you.
    Ok

  12. #12
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi i tried the query still it prints the same result. It does not prints the required one.

  13. #13
    From Italy with love bronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    8,645
    Mentioned
    77 Post(s)
    Tagged
    4 Thread(s)
    The query definitely does NOT print the same result as you posted in your first post.
    It gives the total amounts for each year/month/client, ordered by year/month/client.
    Then, you'll have to use PHP to create the output you want. You can't use MySQL to go from rows to columns.

  14. #14
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, do you have any idea about php code?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •