0

编辑:嘿人,我已经在使用 php,我不是故意要问这个的。我在问如何在 8 列 html 表中显示 3 列 mysql 结果

--work table--                                               --crew table--

date           |   hours                                     date   |    name   
2013-02-03(Sun)     8                                    2013-02-03     john
2013-02-04(Mon)     7                                    2013-02-03     sam 
                                                         2013-02-03     peter
                                                         2013-02-04     john
                                                         2013-02-04     sam

我有这两张表来记录员工的工作时间。
我需要将此数据显示为时间表。

但是我如何在下面的这个 html 表中显示 8 列?

我的意思是假设如果你运行这个查询:

SELECT hours, name, DAYNAME(work.date) day
FROM work 
LEFT JOIN crew ON work.date = crew.date
WHERE WEEK(work.date) = 5

它将显示 3 列。那么如何构建这个 html 表呢?

这个mysql结果

hours    name    day
  8      john   Sunday
  8      Sam    Sunday
  8      Peter  Sunday
  7      John   Monday
  7      Sam    Monday

这个 html 表(如何?)

Name    Sunday   Monday  Tuesday..(day name can be written in html)
John      8        7       ...
Sam       8        7       ...
Peter     8        -       ...
4

2 回答 2

0

修改这个.......如果你使用php

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

while($row = mysql_fetch_array($result))
  {
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br />";
  }

mysql_close($con);
?>
于 2013-02-07T06:27:51.027 回答
0

警告:我不是 PHP 程序员,所以下面充其量是伪代码,并且包含很多语法错误(更不用说任何缺失的错误处理,甚至是 html 布局)。我希望无论如何都能得到这个想法。任何人都可以随意编辑

$result = mysql_query("query here");

// a double nested array
// $weekdays['Sunday']['Peter'] will be the hours peter working on sunday 
$weekdays = [];
// a list of all persons in the table
$persons = [];

while($row = mysql_fetch_array($result))
{
  $hours = $row[0];
  $person = $row[1];
  $weekday= $row[2];

  if (!$weekdays[$person]) {
     $weekdays[$person] = [ $person => $hours ];
  } else {
     $weekdays[$person][$weekday] = $hours;
  }
  if (! $person in $persons) {
    $persons[] = $person;
  }
}

// then unroll in two loops
for ($person in $persons) {
  print '<tr><td>'.$person.'</td>'
  for ($weekday in [ "Sunday", "Monday", ...]) {
     print '<td>'.$weekdays[$weekday][$person].'</td>'
  }
  print '</tr>'
}
于 2013-02-07T14:17:05.557 回答