当ID相同时拆分成新表

时间:2013-11-01 09:06:26

标签: php mysql sql

我有一个表格,根据周ID分割信息。这是创建它的查询:

$result = mysqli_query($con,"SELECT * FROM b_tasks_report WHERE TASK_ID=$taskid");

$current_week_id = -1;

while($row = mysqli_fetch_array($result))
{
if($current_week_id != $row['WEEK_ID'])
{
echo "<tr class='no-border'><td class='no-border'><div class='task-detail-title'>Week Number: " . $row['WEEK_ID'] . "</div></td></tr>";         
echo "<tr>";
echo "<th width='100'>Day</th>";
echo "<th width='75'>Start</th>";
echo "<th width='75'>End</th>";
echo "<th width='100'>Billable Hours</th>";
echo "<th width='100'>Non Billable Hours</th>";
echo "</tr>";

$current_week_id = $row['WEEK_ID'];
}
echo "<tr>";
echo "<td class='tdclass'>" . $row['DAY'] . "</td>";
echo "<td class='tdclass'>" . $row['START'] . "</td>";
echo "<td class='tdclass'>" . $row['END'] . "</td>";
echo "<td class='tdclass'>" . $row['BILLABLE_HOURS'] . "</td>";
echo "<td class='tdclass'>" . $row['NON_BILLABLE_HOURS'] . "</td>";
echo "</tr>";

}

echo "</table>";

这样可行,它会添加新标题,用于标识每个新周ID的周数。但是我想要的是:我需要将它们作为单独的表格,而不是具有多个分组周数和上面标题的表格。

例如说我们有3周身份证的第1周,第2周和第3周。这几周我想要3张单独的牌桌。我已经尝试了多种方法来做到这一点,但它不断分开行。

</table><table>

我知道它需要在while部分,只是不确定在哪里。

任何帮助表示赞赏

4 个答案:

答案 0 :(得分:1)

首先,在您的SQL语句中,您应该使用WEEK_ID订购记录。 在此部分代码之前,您应该删除开始表echo "<table>"

$result = mysqli_query($con,"SELECT * FROM b_tasks_report WHERE TASK_ID=$taskid ORDER BY WEEK_ID");
$current_week_id = -1;
while($row = mysqli_fetch_array($result))
{
if($current_week_id != $row['WEEK_ID'])
{
  if($current_week_id != - 1)
   {      
      echo "</table>";
   }
echo "<table>";

echo "<tr class='no-border'><td class='no-border'><div class='task-detail-title'>Week Number: " . $row['WEEK_ID'] . "</div></td></tr>";         
echo "<tr>";
echo "<th width='100'>Day</th>";
echo "<th width='75'>Start</th>";
echo "<th width='75'>End</th>";
echo "<th width='100'>Billable Hours</th>";
echo "<th width='100'>Non Billable Hours</th>";
echo "</tr>";
$current_week_id = $row['WEEK_ID'];
}
echo "<tr>";
echo "<td class='tdclass'>" . $row['DAY'] . "</td>";
echo "<td class='tdclass'>" . $row['START'] . "</td>";
echo "<td class='tdclass'>" . $row['END'] . "</td>";
echo "<td class='tdclass'>" . $row['BILLABLE_HOURS'] . "</td>";
echo "<td class='tdclass'>" . $row['NON_BILLABLE_HOURS'] . "</td>";
echo "</tr>";
}
  if($current_week_id != - 1)
   {      
      echo "</table>";
   }

答案 1 :(得分:0)

$result = mysqli_query($con,"SELECT * FROM b_tasks_report WHERE TASK_ID=$taskid");

$current_week_id = -1;

while($row = mysqli_fetch_array($result))
{
 echo "<table>";

if($current_week_id != $row['WEEK_ID'])
{
echo "<tr class='no-border'><td class='no-border'><div class='task-detail-title'>Week Number: " . $row['WEEK_ID'] . "</div></td></tr>";         
echo "<tr>";
echo "<th width='100'>Day</th>";
echo "<th width='75'>Start</th>";
echo "<th width='75'>End</th>";
echo "<th width='100'>Billable Hours</th>";
echo "<th width='100'>Non Billable Hours</th>";
echo "</tr>";

$current_week_id = $row['WEEK_ID'];
}
echo "<tr>";
echo "<td class='tdclass'>" . $row['DAY'] . "</td>";
echo "<td class='tdclass'>" . $row['START'] . "</td>";
echo "<td class='tdclass'>" . $row['END'] . "</td>";
echo "<td class='tdclass'>" . $row['BILLABLE_HOURS'] . "</td>";
echo "<td class='tdclass'>" . $row['NON_BILLABLE_HOURS'] . "</td>";
echo "</tr>";
echo "<table>";
}

答案 2 :(得分:0)

试试这个 - 它应该可行

<?php
$current_week_id = -1;
while($row = mysqli_fetch_array($result))
{

if($current_week_id != $row['WEEK_ID'])
{
    echo "<table>";
    echo "<tr class='no-border'><td class='no-border'><div class='task-detail-title'>Week Number: " . $row['WEEK_ID'] . "</div></td></tr>";         
    echo "<tr>";
    echo "<th width='100'>Day</th>";
    echo "<th width='75'>Start</th>";
    echo "<th width='75'>End</th>";
    echo "<th width='100'>Billable Hours</th>";
    echo "<th width='100'>Non Billable Hours</th>";
    echo "</tr>";
}
    echo "<tr>";
    echo "<td class='tdclass'>" . $row['DAY'] . "</td>";
    echo "<td class='tdclass'>" . $row['START'] . "</td>";
    echo "<td class='tdclass'>" . $row['END'] . "</td>";
    echo "<td class='tdclass'>" . $row['BILLABLE_HOURS'] . "</td>";
    echo "<td class='tdclass'>" . $row['NON_BILLABLE_HOURS'] . "</td>";
    echo "</tr>";

    if($current_week_id != $row['WEEK_ID'])
    {
        echo "</table>";
    }

    $current_week_id = $row['WEEK_ID'];
}

&GT;

答案 3 :(得分:0)

这个怎么样?可能会有一些错误,但你可以解决它的问题。目前没有坐在PHP计算机上,所以我无法100%检查语法。

$weeks = array();
while($row = mysqli_fetch_array($result))
{
  if(isset($row['WEEK_ID'])){               
    $tmp_array = $row['WEEK_ID']);          // fetch the existing array holding rows.
    array_push($tmp_array, $row);               // Adding a row
    $weeks['$row['WEEK_ID']'] = $tmp_array;   // Putting it back in the main array.
  } else {
    $list = array();                           // Creates a new one array
    $list[] = $row;                         // Adds the row data
    $weeks['$row['WEEK_ID']'] = $list[0];    // Stores the array under week id.
}
}

foreach ($weeks as $weekID => $week_array){
  echo "<tr class='no-border'><td class='no-border'><div class='task-detail-title'>Week Number: " . $weekID . "</div></td></tr>";         
  echo "<tr>";
  echo "<th width='100'>Day</th>";
  echo "<th width='75'>Start</th>";
  echo "<th width='75'>End</th>";
  echo "<th width='100'>Billable Hours</th>";
  echo "<th width='100'>Non Billable Hours</th>";
  echo "</tr>";
  foreach($week_array as $data){
    echo "<tr>";
    echo "<td class='tdclass'>" . $data['DAY'] . "</td>";
    echo "<td class='tdclass'>" . $data['START'] . "</td>";
    echo "<td class='tdclass'>" . $data['END'] . "</td>";
    echo "<td class='tdclass'>" . $data['BILLABLE_HOURS'] . "</td>";
    echo "<td class='tdclass'>" . $data['NON_BILLABLE_HOURS'] . "</td>";
    echo "</tr>";
  }
  echo "</table>";
}