2017-01-19 59 views
1

我想從數據庫打印數據到html表中。我希望我的表是這樣的:從數據庫打印數據到html表

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

凡工作室的名字是從DATABSE拍攝。我得到的是查詢:

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

,但它似乎沒有工作,我希望它。這裏是我的表怎麼看像現在:

| 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 |    |    |    | 

有什麼問題,我可以正確打印表格?謝謝

+0

如何更新與渲染HTML的問題?爲什麼每次都回顯標題? – mplungjan

回答

1

$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>'; 

只要確保你不要不會循環錯誤的部分。在你的情況下,你只想在你的循環中建立表格的主體。所以在執行循環之前或之後構建頭部,所以你只做一次而不是每次迭代。

0

的問題是你正在環路的表頭,所以試試這個:如果你想保持while

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>'; 
+0

我應該把什麼放在'foreach'裏面? –

+0

檢查更新的答案 –

+0

而你的頭:'回聲'用戶名 工作室頂點 工作室大本營 工作室帆布 工作室地平線 工作室啓動板 工作室星雲 工作室軌道 工作室調色板 工作室沙箱 工作室恆星 工作室theclimb 工作室toonigame 工作室樹「;(?)' – mplungjan

0

這裏的一些思考 - 但我要強調的是,我沒有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";} 
} 

?> 

輸出...

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