我如何在html表中显示这个mysql结果?

时间:2013-02-07 06:17:23

标签: html mysql presentation

编辑:嘿,我已经在使用php,我不是故意要问的。     我问如何在8列html表中显示3-columned 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        -       ...

2 个答案:

答案 0 :(得分: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);
?>

答案 1 :(得分: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>'
}