简体   繁体   English

从SQL查询生成HTML表

[英]Generate HTML table from SQL query

结果

From the image above, I am trying to generate this table dynamically with PHP. 从上面的图像中,我试图使用PHP动态生成此表。 Below is my tables in the mysql DB 下面是我在mysql DB中的表

MySQL表MySQL表2

Here is the SQL that I am using to pull the data from the database 这是我用来从数据库中提取数据的SQL

SELECT DISTINCT execution_date,class_name,method_name,status FROM test_cases INNER JOIN test_case_executions ON test_cases.id=test_case_executions.test_case_id WHERE version='$platform' AND execution_date >= ( CURDATE() - INTERVAL 2 DAY ) ORDER BY execution_date DESC;

This is returning the data that need but I am struggling to figure out how to build the table. 这将返回所需的数据,但我正在努力弄清楚如何构建表。 I was thinking of using arrays and when I have all the data I need, then echo out the table code. 我当时在考虑使用数组,当我拥有所需的所有数据时,然后回显表代码。 The thing I need to guard is that a test is not always guaranteed that there will be three dates for a test. 我需要警惕的是,不能总是保证测试能保证有3个日期进行测试。 Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

You will have to do a couple passes on your data set to generate that output. 您将必须对数据集进行两次传递才能生成该输出。 That is, you'll have lets say 4 rows representing all of the status values and you will have to iterate over it a couple of times to extract out the date column headers and the "Class" row identifiers. 也就是说,假设有4行代表所有状态值,并且您将不得不对其进行迭代两次以提取出日期列标题和“ Class”行标识符。

You can perform this in PHP. 您可以在PHP中执行此操作。 So on the 1st pass you grab the dates for the header. 因此,在第一遍中,您将获取标头的日期。 And also store the "Class" for the first column. 并存储第一列的“类”。

On the 2nd pass you then iterate over the data again but this time its wrapped in a loop so you can pull out the records for that cell. 在第二遍中,然后再次遍历数据,但这一次将其包裹在一个循环中,以便您可以提取该单元格的记录。

Here is some psuedo-code: 这是一些伪代码:

$records = $db->query("select * from your_query here...");

$dates = [];
$classes = [];

// first pass is to pull out the distinct dates & classes which represent our bounds
foreach($records AS $record) {
   $dates[] = $record['execution_date'];
   $classes[] = $record['class_name'];
}

// distinct the date set and sort them from lowest to highest
$dates = array_unique($dates);
$dates = sort($dates);
$classes = array_unique($classes);

// display the date row
echo "<tr><td>&nbsp;</td>"
foreach($dates AS $date) {
  echo $date;
}
echo "</tr>";

// start displaying each class+date pair

foreach($classes AS $klass) {
  echo "<tr>";
  echo "<td>" . $klass . "</td>";
  // display each date record for this class
  foreach($dates AS $date) {
    $class_and_date_record = filter($records, $klass, $date);
    if($class_and_date_record) {
      echo "<td>" . $class_and_date_record['status'] . "</td>";
    }
  }
  echo "</tr>";
}


function filter($records, $klass, $date) {
  foreach($records AS $row) {
    if($row['class_name'] == $klass && $row['execution_date'] == $date) {
      return $row;
    }
  }
  return NULL;
}

If I understand your question correctly, you only want to output data to the table when there is a value in "execution_date" 如果我正确理解了您的问题,则只希望在“ execution_date”中存在值时才将数据输出到表中

$query = "SELECT DISTINCT execution_date,class_name,method_name,status FROM test_cases INNER JOIN test_case_executions ON test_cases.id=test_case_executions.test_case_id WHERE version='$platform' AND execution_date >= ( CURDATE() - INTERVAL 2 DAY ) ORDER BY execution_date DESC;";

    if ($result = $mysqli->query($query)) {

        /* fetch associative array */
        while ($row = $result->fetch_assoc()) {
            echo '<table>';
            if(isset($result["execution_date") && !empty($result["execution_date"])){
                 echo '<tr><td>' . $result["execution_date"] . '</td></tr>';
                 ...
            }
            echo '</table>';
        }

        /* free result set */
        $result->free();
    }

The line to note is : 注意的行是:

 if(isset($result["execution_date") && !empty($result["execution_date"])){

will check your returned row for execution_date having a value. 将检查您返回的行中是否有值的execution_date。
You can then print the rest of the items using the same $result["<column name>"] formatting. 然后,您可以使用相同的$result["<column name>"]格式打印其余项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM