dongran1779 2011-09-09 01:54
浏览 7
已采纳

基于数据输入PHP创建表

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_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条回答 默认 最新

  • douhuireng4407 2011-09-09 02:24
    关注

    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>";
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算