I am trying to make a project wherein it display the 3 previous monnth and the current month.. now my problem is I dont know where or how to reflect this mysql data into a table in php… can anyone taught me please?
Pardon me if I’m not good in explaining to you the system because I’m just a trying hard programmer..
this should be how the table will look like:
http://www.fileden.com/files/2011/7/27/3174077//1.JPG
here is the php codes and mysql query that i want to put into a table:
<form action="" method="post" class="niceform">
<fieldset>
<legend>Job Orders</legend>\
<table>
<tr>
<th>SSA</th>
<th>Months</th>
</tr>
<?php
$datefrom= $_POST['timestamp'];
$dateto=$_POST['timestamp1'];
$parsemonth="";
$parseday ="01";
$conditionmonth=$parsemonth-3;
//january
if ($conditionmonth == '1'){
$sql="SELECT
a.specialist_partner_ID
,count(CASE WHEN a.receivedDate between DATE_SUB('2011-01-01', INTERVAL 3 MONTH) and DATE_SUB('2011-09-30', INTERVAL 3 MONTH) THEN a.job_order_number ELSE null END) As December,
count(CASE WHEN a.receivedDate between DATE_SUB('2011-01-01', INTERVAL 2 MONTH) and DATE_SUB('2011-09-30', INTERVAL 2 MONTH) THEN a.job_order_number ELSE null END) As November
,count(CASE WHEN a.receivedDate between DATE_SUB('2011-01-01', INTERVAL 1 MONTH) and DATE_SUB('2011-09-30', INTERVAL 1 MONTH) THEN a.job_order_number ELSE null END) As October
,count(CASE WHEN a.receivedDate between '2011-01-01' and '2011-01-30'THEN a.job_order_number ELSE null END) As Jauary
,count(job_order_number) As Total
FROM jo_partner a
WHERE a.receivedDate BETWEEN '2011-01-01' AND '2011-12-31'
GROUP BY a.specialist_partner_ID";
}
//february
else if ($conditionmonth == '2'){
$sql="SELECT
a.specialist_partner_ID
,count(CASE WHEN a.receivedDate between DATE_SUB('2011-02-01', INTERVAL 11 MONTH) and DATE_SUB('2011-02-29', INTERVAL 3 MONTH) THEN a.job_order_number ELSE null END) As November
,count(CASE WHEN a.receivedDate between DATE_SUB('2011-02-01', INTERVAL 10 MONTH) and DATE_SUB('2011-02-29', INTERVAL 2 MONTH) THEN a.job_order_number ELSE null END) As December
,count(CASE WHEN a.receivedDate between DATE_SUB('2011-02-01', INTERVAL 9 MONTH) and DATE_SUB('2011-02-29', INTERVAL 1 MONTH) THEN a.job_order_number ELSE null END) As January
,count(CASE WHEN a.receivedDate between '2011-02-01' and '2011-02-29'THEN a.job_order_number ELSE null END) As February
,count(job_order_number) As Total
FROM jo_partner a
WHERE a.receivedDate BETWEEN '2011-01-01' AND '2011-12-31'
GROUP BY a.specialist_partner_ID";
}
//march
else if ($conditionmonth == '3')
{
$sql="SELECT
a.specialist_partner_ID
,count(CASE WHEN a.receivedDate between DATE_SUB('2011-03-01', INTERVAL 3 MONTH) and DATE_SUB('2011-03-31', INTERVAL 3 MONTH) THEN a.job_order_number ELSE null END) As December
,count(CASE WHEN a.receivedDate between DATE_SUB('2011-03-01', INTERVAL 2 MONTH) and DATE_SUB('2011-03-31', INTERVAL 2 MONTH) THEN a.job_order_number ELSE null END) As Jauary
,count(CASE WHEN a.receivedDate between DATE_SUB('2011-03-01', INTERVAL 1 MONTH) and DATE_SUB('2011-03-31', INTERVAL 1 MONTH) THEN a.job_order_number ELSE null END) As February
,count(CASE WHEN a.receivedDate between '2011-03-01' and '2011-03-31'THEN a.job_order_number ELSE null END) As March
,count(job_order_number) As Total
FROM jo_partner a
WHERE a.receivedDate BETWEEN '2011-01-01' AND '2011-12-31'
GROUP BY a.specialist_partner_ID";
}
and so on and so forth… up to DECEMBER
while ($row = mysql_fetch_row($sql)
{
}
?>
</tr></table>
</fieldset>
the condition is when I select a month to view the report for that month, ex January, only data from January will only be visible and the last 3 consrctive month, ex december and november
Given that a table is build up using this kind of data:
It’s really simple to build the table using SQL, just bracket the data using the table tags:
SQL only code, not recommended because it leaves open a XSS security hole!
Only use this if you are 100% sure the data inside your DB is safe
This code constructs the whole table in SQL. An other option is to loop though the values and piece together the table in a while loop:
Code example using a while loop, escapes the output, preventing XSS
Points to remember
mysql_real_escape_string()on all$varsyou inject into a SQL-statement.htmlspecialchars()to prevent XSS.select *habit, only select what you need.Links
SQL-injection: How does the SQL injection from the "Bobby Tables" XKCD comic work?
XSS prevention: How to prevent XSS with HTML/PHP?
php-MySQL tutorial: http://www.tizag.com/mysqlTutorial/
group_concat: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat