【问题标题】:How to display mysql data as table header and then all data related to that result as table data in html如何将mysql数据显示为表头,然后将与该结果相关的所有数据显示为html中的表数据
【发布时间】:2020-07-23 11:02:58
【问题描述】:

我正在为音乐节开发一个场地编程系统,以更新我们当前仅使用大量电子表格的系统。我试图找出一种方法来显示一个表格,该表格将与节日相关的所有子场所显示为表格标题,然后将与该子场所相关的所有时间段显示为表格列。我希望它最终看起来像这样。

当前使用的电子表格的屏幕截图:

这个想法是,您将能够单击其中一个空闲时间段,然后打开一个模式以将节目分配给该时间段或显示已附加到该时间段的节目。理想情况下,每个 subvenue 都将按顺序拖放,但这些都是以后的问题。

到目前为止,我正在尝试使用循环来创建一个只有 1 列的表。让一个 sql 查询返回标题,然后在该返回循环内有另一个返回所有时隙的 sql 查询,然后关闭第一个循环。但这仅显示 1 个表,而不是循环返回其他表。

代码是

<?php 
  //selects subvenue  
 $sql = "SELECT *
        FROM Subvenue S
        JOIN Venue V
            ON S.venueId = V.venueId
        JOIN festvenue FV
            ON V.venueId = FV.venueId
        WHERE FV.festId = $festId;";
      
$result = mysqli_query($conn, $sql);
if (!$result) die ("Database access failed");

$rows = $result->num_rows;

//starts loop to display subvenues
for ($j = 0 ; $j < $rows ; ++$j) {

$row = $result->fetch_array(MYSQLI_NUM);

$subvenueId = htmlspecialchars($row[0]);
$subvenueName = htmlspecialchars($row[2]);
    
echo <<<_END
        <table>
        <tr>
            <th id="$subvenueId">$subvenueName<th>
        </tr>
    _END;

 $sql = "SELECT * FROM TimeSlot 
 WHERE subVenId = $subvenueId 
 ORDER BY (start >= '05:00:00') desc, start;";

 $result = mysqli_query($conn, $sql);
 if (!$result) die ("Database access failed");

 $rows = $result->num_rows;
 for ($j = 0 ; $j < $rows ; ++$j) {
 $row = $result->fetch_array(MYSQLI_NUM);
        
 $timeId = htmlspecialchars($row[0]);
 $type = htmlspecialchars($row[3]);
 $start = htmlspecialchars($row[4]);
 $end = htmlspecialchars($row[5]);
 $length = htmlspecialchars($row[8]);
        
  echo <<<_END
   <tr id="$timeId" class="timeslot-time">
      <td class="$type-$length">$start - $end</td>
   </tr>
  _END;
  }
    echo "</table>";
  }

 ?>

我的样本数据如下

Subvenue Table
+----------+---------+------------------------+
| subVenId | venueId | subVenName             |
+----------+---------+------------------------+
|        1 |       2 | Subvenue 1             |
|        2 |       2 | subvenue 2             |
+----------+---------+------------------------+

timeslot Table
+--------+--------+----------+-------+----------+----------+--------+
| timeId | festId | subVenId | type  | start    | end      | length |
+--------+--------+----------+-------+----------+----------+--------+
|      1 |     11 |        1 | show  | 12:00:00 | 13:00:00 |     60 |
|      2 |     11 |        1 | show  | 13:30:00 | 14:30:00 |     60 |
|      3 |     11 |        1 | break | 13:00:00 | 13:30:00 |     30 |
|      4 |     11 |        1 | break | 14:30:00 | 15:00:00 |     30 |
|      5 |     11 |        1 | show  | 15:00:00 | 16:00:00 |     60 |
|      6 |     11 |        2 | show  | 16:30:00 | 17:30:00 |     60 |
|      7 |     11 |        2 | show  | 18:00:00 | 19:00:00 |     60 |
|      8 |     11 |        2 | show  | 19:30:00 | 20:30:00 |     60 |
|      9 |     11 |        1 | show  | 21:00:00 | 22:00:00 |     60 |
|     10 |     11 |        2 | show  | 22:30:00 | 23:30:00 |     60 |
+--------+--------+----------+-------+----------+----------+--------+

我什至不确定一张桌子是否最适合这个,或者列表或其他东西会更好吗?

最后我希望它显示

+-------------------+.  +-------------------+
| subvenue 1        |   | subvenue 2        |
+-------------------+.  +-------------------+
| 12:00:00-13:00:00 |   | 16:30:00-17:30:00 |
| 13:30:00-14:30:00 |   | 18:00:00-19:00:00 |
| 13:00:00-13:30:00 |   | 19:30:00-20:30:00 |
| 14:30:00-15:00:00 |   | 22:30:00-23:30:00 |
| 15:00:00-16:00:00 |.  +-------------------+
| 21:00:00-22:00:00 |
+-------------------+
etc

【问题讨论】:

  • 感谢您的建议,我目前正在为所有用户从表单提交的数据使用准备好的语句,但也会对其进行更改以停止从其他方法注入。
  • 这称为“枢轴”。这是 MySQL 中 xxx 脖子上的一个臭名昭著的痛苦。你可能想让你的 php 程序来做这种格式化。
  • 您在两个嵌套循环中使用相同的 $j 索引(认为它们是嵌套的,因为没有格式化)。并且您在 subvenues 循环中创建表格,因此您将为每个场地获得单独的表格。我以为你想为整个节日安排一张桌子?
  • 我不太担心每个 subvenue 是在它自己的桌子上还是在一个桌子上。任何最有效的方法。

标签: php html mysql pivot


【解决方案1】:

我已经设法弄清楚了。我决定输出一系列列表而不是使用表格。

除此之外,我的主要解决方法是在我的第二个查询中使用 $subSql 和 $subStmt,而不是在 mySQL 查询中使用相同的 $sql 和 $stmt 变量。

require_once "header.php";

$festId = mysqli_real_escape_string($conn, $_GET["festival_Id"]);

?>

    <div id="programming" class="tabcontent">
    <h2 >Programming</h2><br>  
    
<?php  

     //gets the subvenue and starts first loop

$sql = "SELECT *
    FROM Subvenue S
    JOIN Venue V
        ON S.venueId = V.venueId
    JOIN festvenue FV
        ON V.venueId = FV.venueId
    WHERE FV.festId = ?;";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)) {
  header("location: ../festival.php?error=sqlerror&festival_Id".$festId);
  exit();
}

else {
   mysqli_stmt_bind_param($stmt, "s", $festId);
   mysqli_stmt_execute($stmt);
   $result = $stmt->get_result();
   $rows = $result->num_rows;

   for ($j = 0 ; $j < $rows ; ++$j)
   {
       $row = $result->fetch_array(MYSQLI_NUM);
       $subVenueId = htmlspecialchars($row[0]);
       $subVenueName = htmlspecialchars($row[2]);
    
   echo <<<_END
       <div id="$subVenueId" class="programme">
         <ul>
            <li class="programme-heading">$subVenueName</li>
              <ul>
    _END;

        //select all timeslots with that subvenue   
        $subSql = "SELECT * FROM TimeSlot 
                WHERE subVenId = ?
                ORDER BY (start >= '05:00:00') desc, start;";
        $subStmt = mysqli_stmt_init($conn);
        if(!mysqli_stmt_prepare($subStmt, $subSql)) {
            header("location: ../festival.php? 
            error=sqlerror&festival_Id".$festId);
            exit();
        }
        else {
           mysqli_stmt_bind_param($subStmt, "s", $subVenueId);
           mysqli_stmt_execute($subStmt);
           $subResult = $subStmt->get_result();
                
           while ($row = mysqli_fetch_assoc($subResult)) {
              $timeId = htmlspecialchars($row['timeId']);
              $type = htmlspecialchars($row['type']);
              $start = htmlspecialchars($row['start']);
              $end = htmlspecialchars($row['end']);
              $length = htmlspecialchars($row['length']);
                    
        echo <<<_END
              <li id="[$timeId" class="$type-$length">$start - $end</li>
        _END;][1]
                }
            }
            echo "</ul></ul></div>";
        }
}

这看起来正是我想要的。在最初的问题中

【讨论】:

    猜你喜欢
    • 2021-02-08
    • 2013-07-08
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 2014-04-18
    • 2020-04-28
    相关资源
    最近更新 更多