Php 将数据从数据库打印到html表

Php 将数据从数据库打印到html表,php,html,mysql,Php,Html,Mysql,我正在尝试将数据从数据库打印到html表。我希望我的桌子是这样的: | username | studio APEX | studio CANVAS | studio ORBIT | +----------+-------------+---------------+--------------+ | Aaron | x | x | | | Adel | | |

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

| 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>";      
}

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

问题是您正在使表格标题处于循环中,请尝试以下操作:

echo '<table>';

// This is table header    
echo '<tr> <th>Column Name 1</th> <th>Column Name 2</th> <th>Column Name 3</th></tr><tbody>';

// This is the table body
foreach( .. )
{
    echo '<tr>';
    echo '<td>'.$var.'<td>';
    echo '<td>'.$var.'<td>';
    ...
    echo '</tr>';
}

echo '</tbody></table>';
echo';
//这是表格标题
echo“Column Name 1 Column Name 2 Column Name 3”;
//这是桌子的主体
foreach(…)
{
回声';
回显“.$var.”;
回显“.$var.”;
...
回声';
}
回声';

如果要在循环时保持

$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>';
$query2=mysqli\u查询($conn,$sql2);
$header='用户名
工作室顶点
工作室大本营
画室画布
工作室地平线
工作室发射台
工作室星云
工作室轨道
工作室调色板
工作室沙盒
星光工作室
录音室
图尼加米工作室
工作室树屋';
$body='';
而($data2=mysqli\u fetch\u 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'];
$palete=$data2['palete'];
$sandbox=$data2['sandbox'];
$stellar=$data2['stellar'];
$theclimb=$data2['theclimb'];
$toonigame=$data2['TOONIGAMI'];
$treehouse=$data2['treehouse'];
$row=''.htmlspecialchars($username)。'
“.$apex。”
“.$basecamp。”
“.$canvas。”
“.$horizon。”
“.$launchpad。”
“星云。”
“.$orbit。”
“.$palette。”
“.$sandbox”
“.$stellar。”
“.$tdeclimb。”
“.$toonigame。”
“.$treehouse.”;
$body.=''.$row';
}
回显“”$标题。“”$身体",;

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

这里有一些需要考虑的问题-尽管我应该强调我不是PHP程序员

<?php

$my_array = array(
    0 => 1,
    1 => 2,
    2 => 3,
    3 => 5,
    4 => 7
);

for($i=0;$i<10;$i++){
echo $i;
if(in_array($i,$my_array)){echo " yes <br>\n";} else {echo " no <br>\n";}
}

?>

用呈现的HTML更新问题怎么样?为什么每次都要回显标题?我应该在
foreach
中放入什么?以及您的标题:
echo“Username Studio apex Studio basecamp Studio canvas Studio horizon Studio launchpad Studio nebula Studio orbit”画室调色板画室沙盒画室星光画室theclimb画室toonigame画室树屋'
我的意思是在括号内
foreach(?)
应该是
username
?foreach($data as$variable){}类似这样的东西
<?php

$my_array = array(
    0 => 1,
    1 => 2,
    2 => 3,
    3 => 5,
    4 => 7
);

for($i=0;$i<10;$i++){
echo $i;
if(in_array($i,$my_array)){echo " yes <br>\n";} else {echo " no <br>\n";}
}

?>
0 no
1 yes
2 yes
3 yes
4 no
5 yes
6 no
7 yes
8 no
9 no