I have this several lines of code. It's function is that it will create a table based on the date that is inputted. Example if the date today is Monday, then it will result into 5 columns (mon, tue, wed, thu, fri) or if today is Tuesday, it will result to 4 columns (tue, wed, thu, fri) and so on and so forth. My problem is that the code i'm using is too long. I want to ask if it could be possible to shorten this? If it is possible, could you taught me how?
Here is my code:
if ($jd2 == 'Monday')
{
$sql="SELECT
a.specialist_partner_ID
,count(CASE WHEN a.receivedDate between '".$datefrom."' and '".$dateto."' THEN a.job_or开发者_开发问答der_number ELSE null END) As THU
,count(CASE WHEN a.receivedDate between DATE_SUB('".$datefrom."', INTERVAL 1 DAY) and DATE_SUB('".$dateto."', INTERVAL 1 DAY) THEN a.job_order_number ELSE null END) As FRI
,count(job_order_number) As Total
FROM jo_partner a
WHERE a.receivedDate BETWEEN '".$datefrom."' AND '".$dateto."'
GROUP BY a.specialist_partner_ID";
}
//echo $sql;
echo "<table width='200' border='2'>";
echo "<tr>";
echo "<td>MON</td>";
echo "<td>THU</td>";
echo "<td>WED</td>";
echo "<td>THU</td>";
echo "<td>FRI</td>";
echo "<td>total</td>";
echo "</tr>";
}
$query = mysql_query($sql);
while($row = mysql_fetch_array($query))
{
$MON = $row['MON'];
$TUE = $row['TUE'];
$WED = $row['WED'];
$THU = $row['THU'];
$FRI = $row['FRI'];
$Total = $row['Total'];
if ($jd2 == 'Monday')
{
echo "<tr>";
echo "<td>$MON</td>";
echo "<td>$TUE</td>";
echo "<td>$WED</td>";
echo "<td>$THU</td>";
echo "<td>$FRI</td>";
echo "<td>$Total</td>";
echo "</tr>";
}
}
echo "</table>";
if ($jd2 == 'Tuesday')
{
$sql="SELECT
a.specialist_partner_ID
,count(CASE WHEN a.receivedDate between '".$datefrom."' and '".$dateto."' THEN a.job_order_number ELSE null END) As THU
,count(CASE WHEN a.receivedDate between DATE_SUB('".$datefrom."', INTERVAL 1 DAY) and DATE_SUB('".$dateto."', INTERVAL 1 DAY) THEN a.job_order_number ELSE null END) As FRI
,count(job_order_number) As Total
FROM jo_partner a
WHERE a.receivedDate BETWEEN '".$datefrom."' AND '".$dateto."'
GROUP BY a.specialist_partner_ID";
//echo $sql;
echo "<table width='200' border='2'>";
echo "<tr>";
echo "<td>TUE</td>";
echo "<td>WED</td>";
echo "<td>THU</td>";
echo "<td>FRI</td>";
echo "<td>total</td>";
echo "</tr>";
}
$query = mysql_query($sql);
while($row = mysql_fetch_array($query))
{
$TUE = $row['TUE'];
$WED = $row['WED'];
$THU = $row['THU'];
$FRI = $row['FRI'];
$Total = $row['Total'];
if ($jd2 == 'Tueday')
{
echo "<tr>";
echo "<td>$TUE</td>";
echo "<td>$WED</td>";
echo "<td>$THU</td>";
echo "<td>$FRI</td>";
echo "<td>$Total</td>";
echo "</tr>";
}
}
echo "</table>";
if ($jd2 == 'Wednesday')
{
$sql="SELECT
a.specialist_partner_ID
,count(CASE WHEN a.receivedDate between '".$datefrom."' and '".$dateto."' THEN a.job_order_number ELSE null END) As THU
,count(CASE WHEN a.receivedDate between DATE_SUB('".$datefrom."', INTERVAL 1 DAY) and DATE_SUB('".$dateto."', INTERVAL 1 DAY) THEN a.job_order_number ELSE null END) As FRI
,count(job_order_number) As Total
FROM jo_partner a
WHERE a.receivedDate BETWEEN '".$datefrom."' AND '".$dateto."'
GROUP BY a.specialist_partner_ID";
//echo $sql;
echo "<table width='200' border='2'>";
echo "<tr>";
echo "<td>WED</td>";
echo "<td>THU</td>";
echo "<td>FRI</td>";
echo "<td>total</td>";
echo "</tr>";
}
$query = mysql_query($sql);
while($row = mysql_fetch_array($query))
{
$WED = $row['WED'];
$THU = $row['THU'];
$FRI = $row['FRI'];
$Total = $row['Total'];
if ($jd2 == 'Wednesday')
{
echo "<tr>";
echo "<td>$WED</td>";
echo "<td>$THU</td>";
echo "<td>$FRI</td>";
echo "<td>$Total</td>";
echo "</tr>";
}
}
echo "</table>";
Maybe this can give you an idea:
<?php
//to make testing easier
$jd2 = isset($_GET['jd2'])?$_GET['jd2']:'Monday';
//array with abbr
$days = array(
'Monday' => 'MON',
'Tuesday' => 'TUE',
'Wednesday' => 'WED',
'Thursday' => 'THU',
'Friday' => 'FRI');
//this will hold the tr content
$tr_content = '';
foreach(array_reverse($days,true) as $day => $abbr){
$tr_content = "<td>$day</td>".$tr_content;
if($jd2 == $day) break;
}
echo "<table border=1><tr>$tr_content</tr></table>";
//create a few links to try
foreach($days as $day => $i){
echo "<a href='?jd2=$day'>$day</a><br>";
}
?>
精彩评论