1

我有一个根据周 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 周 ID 的 week_1、week_2 和 week_3。这几周我想要 3 张单独的桌子。我尝试了多种方法来做到这一点,但它一直在拆分行。

</table><table>

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

任何帮助表示赞赏

4

4 回答 4

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>";
   }
于 2013-11-01T09:40:43.713 回答
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>";
}
于 2013-11-01T09:58:04.603 回答
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'];
}

?>

于 2013-11-01T09:39:34.193 回答
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>";
}
于 2013-11-01T09:14:40.160 回答