php - 从数据库打印数据到html表

标签 php html mysql

我正在尝试将数据从数据库打印到 html 表。我希望我的 table 是这样的:

| username | studio APEX | studio CANVAS | studio ORBIT |
+----------+-------------+---------------+--------------+
| Aaron    |       x     |       x       |              |
| Adel     |             |               |       x      |
| John     |       x     |       x       |              |
| James    |       x     |       x       |              |
| Kate     |             |               |       x      |
| Peter    |       x     |               |              |

工作室名称取自数据库。我得到的是查询:

$sql2 = '
            select
              p2.proc_leader as username,
                   max(case when studio = "APEX" then "x" else "" end) as APEX,
                   max(case when studio = "BASECAMP" then "x" else "" end) as BASECAMP,
                   max(case when studio = "CANVAS" then "x" else "" end) as CANVAS,
                   max(case when studio = "HORIZON" then "x" else "" end) as HORIZON,
                   max(case when studio = "LAUNCHPAD" then "x" else "" end) as LAUNCHPAD,
                   max(case when studio = "NEBULA" then "x" else "" end) as NEBULA,
                   max(case when studio = "ORBIT" then "x" else "" end) as ORBIT,
                   max(case when studio = "PALETTE" then "x" else "" end) as PALETTE,
                   max(case when studio = "SANDBOX" then "x" else "" end) as SANDBOX,
                   max(case when studio = "STELLAR" then "x" else "" end) as STELLAR,
                   max(case when studio = "THE CLIMB" then "x" else "" end) as THECLIMB,
                   max(case when studio = "TOONIGAMI" then "x" else "" end) as TOONIGAMI,
                   max(case when studio = "TREEHOUSE" then "x" else "" end) as TREEHOUSE
            from process p1
            left join (
              select *
              from proc_leader
              union all
              select *
              from proc_checker
              union all
              select *
              from proc_staff    
            ) p2 on p1.projectNo = p2.projectNo
            and p1.process = p2.process
            group by p2.proc_leader
';

然后我尝试打印到 html 表:

 $query2 = mysqli_query($conn, $sql2);

  while ($data2 = mysqli_fetch_assoc($query2)) {
    $username = $data2['username'];
    $apex = $data2['APEX'];
    $basecamp = $data2['BASECAMP'];
    $canvas = $data2['CANVAS'];
    $horizon = $data2['HORIZON'];
    $launchpad = $data2['LAUNCHPAD'];
    $nebula = $data2['NEBULA'];
    $orbit = $data2['ORBIT'];
    $palette = $data2['PALETTE'];
    $sandbox = $data2['SANDBOX'];
    $stellar = $data2['STELLAR'];
    $theclimb = $data2['THECLIMB'];
    $toonigame = $data2['TOONIGAMI'];
    $treehouse = $data2['TREEHOUSE'];   

            $header =
                '<th>Username</th>
                <th>Studio'.$apex.'</th>
                <th>Studio'.$basecamp.'</th>
                <th>Studio'.$canvas.'</th>
                <th>Studio'.$horizon.'</th>
                <th>Studio'.$launchpad.'</th>
                <th>Studio'.$nebula.'</th>
                <th>Studio'.$orbit.'</th>
                <th>Studio'.$palette.'</th>
                <th>Studio'.$sandbox.'</th>
                <th>Studio'.$stellar.'</th>
                <th>Studio'.$theclimb.'</th>
                <th>Studio'.$toonigame.'</th>
                <th>Studio'.$treehouse.'</th>';

            //body
            $body = '';
                $row = '<td>' . htmlspecialchars($username) . '</td>';
                $body .= "<tr>$row</tr>";
            echo "<thead>$header</thead><tbody>$body</tbody>";      
}

但它似乎并没有像我想要的那样工作。这是我的 table 现在的样子:

| username | studiox     | studiox       | studio       |
+----------+-------------+---------------+--------------+
| Aaron    |             |               |              |
| username | studio      | studio        | studiox      |
| Adel     |             |               |              |
| username | studiox     | studiox       | studio       |
| John     |             |               |              |
| username | studiox     | studiox       | studio       |
| James    |             |               |              |
| username | studio      | studio        | studiox      |
| Kate     |             |               |              |
| username | studiox     | studio        | studio       |
| Peter    |             |               |              |

有什么问题,我可以正确打印表格吗?谢谢

最佳答案

如果你想保留 while 循环:

$query2 = mysqli_query($conn, $sql2);

$header = '<tr><th>Username</th>
            <th>Studio APEX</th>
            <th>Studio BASECAMP</th>
            <th>Studio CANVAS</th>
            <th>Studio HORIZON</th>
            <th>Studio LAUNCHPAD</th>
            <th>Studio NEBULA</th>
            <th>Studio ORBIT</th>
            <th>Studio PALETTE</th>
            <th>Studio SANDBOX</th>
            <th>Studio STELLAR</th>
            <th>Studio THECLIMB</th>
            <th>Studio TOONIGAMI</th>
            <th>Studio TREEHOUSE</th></tr>';
$body = '';
while ($data2 = mysqli_fetch_assoc($query2)) {
    $username = $data2['username'];
    $apex = $data2['APEX'];
    $basecamp = $data2['BASECAMP'];
    $canvas = $data2['CANVAS'];
    $horizon = $data2['HORIZON'];
    $launchpad = $data2['LAUNCHPAD'];
    $nebula = $data2['NEBULA'];
    $orbit = $data2['ORBIT'];
    $palette = $data2['PALETTE'];
    $sandbox = $data2['SANDBOX'];
    $stellar = $data2['STELLAR'];
    $theclimb = $data2['THECLIMB'];
    $toonigame = $data2['TOONIGAMI'];
    $treehouse = $data2['TREEHOUSE'];

    $row = '<td>' . htmlspecialchars($username) . '</td>
                <td>' . $apex . '</td>
                <td>' . $basecamp . '</td>
                <td>' . $canvas . '</td>
                <td>' . $horizon . '</td>
                <td>' . $launchpad . '</td>
                <td>' . $nebula . '</td>
                <td>' . $orbit . '</td>
                <td>' . $palette . '</td>
                <td>' . $sandbox . '</td>
                <td>' . $stellar . '</td>
                <td>' . $tdeclimb . '</td>
                <td>' . $toonigame . '</td>
                <td>' . $treehouse . '</td>';
    $body .= '<tr>' . $row . '</tr>';
}
echo '<table><thead>' . $header . '</thead><tbody>' . $body . '</tbody></table>';

只需确保您没有循环错误的部分。在您的情况下,您只想在循环中构建表的主体。因此,在执行循环之前或之后构建头部,这样您只需执行一次,而不是在每次迭代中都执行。

关于php - 从数据库打印数据到html表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41735948/

相关文章:

php - Symfony2 : Change property of object in the session

php - 浏览器中的独立 php jabber 客户端,无需 javascript

javascript - 在 li 悬停时更改背景图像;当我得到 li 时如何更改回来?

javascript - "enter key"单击按钮在运行相应脚本后瞬间刷新页面

mysql - MySQL中如何清理含有特殊字符的数据

PHP+MYSQL : select/update/insert from(into) many tables

php - 如何使用子查询从两个表中获取值?

PHP 与 jquery 数据表与 CRUD 只能编辑可见记录

javascript - 在 MaterializeCSS 导航栏中垂直居中 material_icon?

mysql - 连接 SQL 列中的选择