Month & year data from mysql

Reply

Join Date: Oct 2004
Posts: 29
Reputation: trashed is an unknown quantity at this point 
Solved Threads: 0
trashed trashed is offline Offline
Light Poster

Month & year data from mysql

 
0
  #1
Mar 16th, 2008
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
  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
  1. if(mysql_num_rows($result)==0) {
  2. break;
  3. }

but I don't seem to figure out how to fix it.
Thanks!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 790
Reputation: pritaeas is on a distinguished road 
Solved Threads: 129
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Master Poster

Re: Month & year data from mysql

 
0
  #2
Mar 18th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC