943,650 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3065
  • PHP RSS
Mar 16th, 2008
0

Month & year data from mysql

Expand Post »
Hi guys.
I have a DB table looking like this:

#DATE #NAME #C1 #C2

2007-08-27 user 1250 2294
2007-08-28 user 1816 3096
2007-08-29 user 89 3153
2007-08-30 user 71 2986

[..]

I started collecting data in late August 2007 and it's being updated every day.
I want to fetch the monthly totals for columns C1 and C2 and plot them in a graph (PHP/SWF charts - http://www.maani.us/charts/) but I am having troubles excluding the years/months that I have no data for.

This is what I wrote so far
PHP Syntax (Toggle Plain Text)
  1. $month=date("m");
  2. $year=date("Y");
  3.  
  4. $month_array[0]="";
  5. $c1_array[0]="C1";
  6. $c2_array[0]="C2";
  7.  
  8. $index=1;
  9. for ($j=2007;$j<=$year;$j++)
  10. {
  11. for ($i=1;$i<=12;$i++)
  12. {
  13. if ($i==10 || $i==11 || $i==12)
  14. {
  15. $query = "SELECT SUM(C1),SUM(C2) FROM stats WHERE day LIKE '".$j."-".$i."-%'";
  16. }
  17. else
  18. {
  19. $query = "SELECT SUM(C1),SUM(C2) FROM stats WHERE day LIKE '".$j."-0".$i."-%'";
  20. }
  21. $result = mysql_query($query);
  22.  
  23. if(mysql_num_rows($result)==0) {
  24. break;
  25. }
  26. else {
  27. $row=mysql_fetch_array($result);
  28. $c1_array[$index]=$row[0];
  29. $c2_array[$index]=$row[1];
  30. $month_array[$index]=$i."-".$j;
  31. $index++;
  32. }
  33. }
  34. }
  35.  
  36.  
  37.  
  38. /* $index=1;
  39. while($row = mysql_fetch_assoc($result))
  40. {
  41. $day_array[$index]="";
  42. $c1_array[$index]=$row['spam'];
  43. $c2_array[$index]=$row['ham'];
  44. $index++;
  45. }*/
  46.  
  47.  
  48.  
  49. $chart[ 'axis_ticks' ] = array ( 'value_ticks'=>true, 'category_ticks'=>true, 'major_thickness'=>2, 'minor_thickness'=>1, 'minor_count'=>1, 'major_color'=>"000000", 'minor_color'=>"222222" ,'position'=>"outside" );
  50. $chart[ 'chart_border' ] = array ( 'color'=>"000000", 'top_thickness'=>2, 'bottom_thickness'=>2, 'left_thickness'=>2, 'right_thickness'=>2 );
  51. $chart [ 'chart_type' ] = "line";
  52. $chart[ 'chart_data' ] = array ($month_array, $c1_array, $c2_array);
  53. $chart[ 'chart_grid_h' ] = array ( 'alpha'=>10, 'color'=>"000000", 'thickness'=>1, 'type'=>"solid" );
  54. $chart[ 'chart_grid_v' ] = array ( 'alpha'=>10, 'color'=>"000000", 'thickness'=>1, 'type'=>"solid" );
  55. $chart[ 'chart_pref' ] = array ( 'line_thickness'=>2, 'point_shape'=>"none", 'fill_shape'=>false );
  56. SendChartData ($chart);
  57.  
  58. ?>

Of course it is not working.
I believe there must be something wrong around
PHP Syntax (Toggle Plain Text)
  1. if(mysql_num_rows($result)==0) {
  2. break;
  3. }

but I don't seem to figure out how to fix it.
Thanks!
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
trashed is offline Offline
30 posts
since Oct 2004
Mar 18th, 2008
0

Re: Month & year data from mysql

if you want to continue your for loop then use 'continue' instead of 'break'.

Just a note: mysql has a YEAR and MONTH function you can use to simplify your query.
Sponsor
Featured Poster
Reputation Points: 549
Solved Threads: 725
Bite my shiny metal ass!
pritaeas is offline Offline
4,161 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Weird charset problem
Next Thread in PHP Forum Timeline: apche installment error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC