基于数据输入PHP创建表

时间:2011-09-09 01:54:59

标签: php mysql sql html-table

我有几行代码。它的功能是它将根据输入的日期创建一个表。例如,如果今天的日期是星期一,那么它将导致 5列(星期一,星期二,星期三,星期四,星期五)或者今天是星期二,它将导致 4列(星期二,星期五,星期四,星期五)等等。我的问题是我使用的代码太长了。我想问一下是否有可能缩短这个?如果有可能,你能教我怎么样?

这是我的代码:

if ($jd2 == 'Monday')
{
    $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>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>";

1 个答案:

答案 0 :(得分:0)

也许这可以给你一个想法:

<?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>";
}

?>