Page 1 of 1

Creat table with therteen month

Posted: Wed Mar 18, 2009 2:09 pm
by ihabgr
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I have mad this simple script to retrieve data from myself I want to show the preview 13 month I did manually but I want php to create the table so i don't have to updated next month
Please Help me
____________________

Code: Select all

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
 
$dbname = 'beta_insideschools';
mysql_select_db($dbname);
 
 
 
$query = 'SELECT COUNT(*) FROM `SchoolComments`where (MONTH(createTime) = 1 AND YEAR(createTime) = 2008)';
$result = mysql_query($query) ;
$com = mysql_result($result, 0);
$query1 = 'SELECT COUNT(*) FROM `SchoolComments`where (MONTH(createTime) = 2 AND YEAR(createTime) = 2008)';
$result1 = mysql_query($query1) ;
$com1 = mysql_result($result1, 0);
$query2 = 'SELECT COUNT(*) FROM `SchoolComments`where (MONTH(createTime) = 3 AND YEAR(createTime) = 2008)';
$result2 = mysql_query($query2) ;
$com2 = mysql_result($result2, 0);
$query3 = 'SELECT COUNT(*) FROM `SchoolComments`where (MONTH(createTime) = 4 AND YEAR(createTime) = 2008)';
$result3 = mysql_query($query3) ;
$com3 = mysql_result($result3, 0);
$query4 = 'SELECT COUNT(*) FROM `SchoolComments`where (MONTH(createTime) = 5 AND YEAR(createTime) = 2008)';
$result4 = mysql_query($query4) ;
$com4 = mysql_result($result4, 0);
$query5 = 'SELECT COUNT(*) FROM `SchoolComments`where (MONTH(createTime) = 6 AND YEAR(createTime) = 2008)';
$result5 = mysql_query($query5) ;
$com5 = mysql_result($result5, 0);
$query6 = 'SELECT COUNT(*) FROM `SchoolComments`where (MONTH(createTime) = 7 AND YEAR(createTime) = 2008)';
$result6 = mysql_query($query6) ;
$com6 = mysql_result($result6, 0);
$query7 = 'SELECT COUNT(*) FROM `SchoolComments`where (MONTH(createTime) = 8 AND YEAR(createTime) = 2008)';
$result7 = mysql_query($query7) ;
$com7 = mysql_result($result7, 0);
$query8 = 'SELECT COUNT(*) FROM `SchoolComments`where (MONTH(createTime) = 9 AND YEAR(createTime) = 2008)';
$result8 = mysql_query($query8) ;
$com8 = mysql_result($result8, 0);
$query9 = 'SELECT COUNT(*) FROM `SchoolComments`where (MONTH(createTime) = 10 AND YEAR(createTime) = 2008)';
$result9 = mysql_query($query9) ;
$com9 = mysql_result($result9, 0);
$query10 = 'SELECT COUNT(*) FROM `SchoolComments`where (MONTH(createTime) = 11 AND YEAR(createTime) = 2008)';
$result10 = mysql_query($query10) ;
$com10 = mysql_result($result10, 0);
$query11 = 'SELECT COUNT(*) FROM `SchoolComments`where (MONTH(createTime) = 12 AND YEAR(createTime) = 2008)';
$result11 = mysql_query($query11) ;
$com11 = mysql_result($result11, 0);
$query12 = 'SELECT COUNT(*) FROM `SchoolComments`where (MONTH(createTime) = 1 AND YEAR(createTime) = 2009)';
$result12 = mysql_query($query12) ;
$com12 = mysql_result($result12, 0);
$query13 = 'SELECT COUNT(*) FROM `SchoolComments`where (MONTH(createTime) = 2 AND YEAR(createTime) = 2009)';
$result13 = mysql_query($query13) ;
$com13 = mysql_result($result13, 0);
$query14 = 'SELECT COUNT(*) FROM `SchoolComments`where (MONTH(createTime) = 03 AND YEAR(createTime) = 2009)';
$result14 = mysql_query($query14) ;
$com14 = mysql_result($result14, 0);
 
 
 
?>
 
<p align="center"><strong>Total School Comments Received</strong></p>
<table width="100%" border="1" cellpadding="2" cellspacing="2" bordercolor="#9999FF">
  <tr>
    <td bordercolor="#9999FF" bgcolor="#9999FF"><strong>Jan. 2008</strong></td>
    <td bgcolor="#9999FF"><strong>Feb. 2008</strong></td>
    <td bgcolor="#9999FF"><strong>Mar. 2008</strong></td>
    <td bgcolor="#9999FF"><strong>Apr.. 2008</strong></td>
    <td bgcolor="#9999FF"><strong>May. 2008</strong></td>
    <td bgcolor="#9999FF"><strong>Jun. 2008</strong></td>
    <td bgcolor="#9999FF"><strong>Jul. 2008</strong></td>
    <td bgcolor="#9999FF"><strong>Aug. 2008</strong></td>
    <td bgcolor="#9999FF"><strong>Sep. 2008</strong></td>
    <td bgcolor="#9999FF"><strong>Oct. 2008</strong></td>
    <td bgcolor="#9999FF"><strong>Nov. 2008</strong></td>
    <td bgcolor="#9999FF"><strong>Dec. 2008</strong></td>
    <td bgcolor="#9999FF"><strong>Jan. 2009</strong></td>
    <td bgcolor="#9999FF"><strong>Feb. 2009</strong></td>
    <td bgcolor="#9999FF"><strong>Mar. 2009</strong></td>
  </tr>
  <tr>  <strong>
    <td bordercolor="#9999FF"><?echo $com;?></td>
    <td><?echo $com1;?></td>
    <td><?echo $com2;?></td>
    <td><?echo $com3;?></td>
    <td><?echo $com4;?></td>
    <td><?echo $com5;?></td>
    <td><?echo $com6;?></td>
    <td><?echo $com7;?></td>
    <td><?echo $com8;?></td>
    <td><?echo $com9;?></td>
    <td><?echo $com10;?></td>
    <td><?echo $com11;?></td>
    <td><?echo $com12;?></td>
    <td><?echo $com13;?></td>
    <td><?echo $com14;?></td>
  </tr> </strong>
</table>

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Creat table with therteen month

Posted: Wed Mar 18, 2009 4:38 pm
by pickle
I've added

Code: Select all

 [ /code] tags around your code, like I asked you to last time  :roll: 
 
Rather than manually hard coding in the month and year, do all your queries in a loop, kind of like this (not complete code)
 
[syntax=php]$current_stamp = date();
for($i = 0; $i <= 12; $i++)
{
   $curr_month = date('m',$current_stamp);
   $curr_year = date('Y',$current_stamp);
 
    $query = 'GET COUNT OF ROWS WITH month = $curr_month AND year = $curr_year';
   //do something with the results
   $current_stamp = strtotime('-1 month', $current_stamp);
}[/syntax]

Re: Creat table with therteen month

Posted: Thu Mar 19, 2009 9:06 am
by ihabgr
Hello
Thanks for all your hard work
Please tell me how i do that with my code I'm a beginner
Thank you

Re: Creat table with therteen month

Posted: Thu Mar 19, 2009 9:52 am
by pickle
I pretty much already gave you the code. Try to adapt it.

I'm happy to explain what my code does, but not to do your work outright.

Re: Creat table with therteen month

Posted: Thu Mar 19, 2009 10:23 am
by ihabgr
can you explain what to dose please

Re: Creat table with therteen month

Posted: Thu Mar 19, 2009 10:37 am
by pickle
Sure. Basically it starts at the current time. The for() loop iterates 13 times (0 - 12). In each loop it determines the month & year for the time represented by $current_stamp, runs a query based on that information, then updates $current_stamp to be one month earlier. After the loop is done, 13 queries will have been run, one for each of the previous 13 months (including the current month).

Re: Creat table with therteen month

Posted: Thu Mar 19, 2009 10:42 am
by ihabgr
Do you know where can I get php course

Re: Creat table with therteen month

Posted: Thu Mar 19, 2009 10:55 am
by pickle
Nope.

Re: Creat table with therteen month

Posted: Thu Mar 19, 2009 10:57 am
by William
ihabgr wrote:Do you know where can I get php course
http://www.zend.com/en/store/php-training/ :D