簡體   English   中英

一行顯示字段 + PHP

[英]Display fields in one row + PHP

美好的一天,我想在一行中顯示我的數據庫中的值。 但是現在發生了什么,它是水平顯示的

圖像樣本

 <table class="table table-bordered"> <thead style="font-size:20px;text-align:center;"class="thead-dark"> <tr> <th class="tblHeader">MODEL</th> <th class="tblHeader">PERIOD 1</th> <th class="tblHeader">PERIOD 2</th> <th class="tblHeader">PERIOD 3</th> <th class="tblHeader">PERIOD 4</th> <th class="tblHeader">PERIOD 5</th> <th class="tblHeader">PERIOD 6</th> <th class="tblHeader">PERIOD 7</th> <th class="tblHeader">PERIOD 8</th> <th class="tblHeader">PERIOD 9</th> <th class="tblHeader">PERIOD 10</th> <th class="tblHeader">PERIOD 11</th> <th class="tblHeader">PERIOD 12</th> </tr> </thead> <?php $connect = mysqli_connect("localhost", "root", "", "hh_bpm"); $query = "SELECT * FROM bpm_periods_instance WHERE Category_Name=1 "; $result = mysqli_query($connect, $query); while($row = mysqli_fetch_array($result)) {?> <tbody style="font-size:20px; text-align:center;"> <tr> <td><?php echo $row["Text_Value"];?></td> </tr> <?php } mysqli_close($connect);?> </tbody> </table>

我想顯示符合 Period 1 - 12 的字符串值`

我還在努力學習。

首先,將 while 循環移到<tr>之前,因為您只想擁有一個表體。 那么最好有相同數量的<th><td> 我看到你有 12 個<th> ,所以在每個<tr>中創建 12 個<td> (如果你願意,可以將它們留空,但包括它們)

標簽是包含表格標題的行。 為了匹配您的列名,您的表中必須有與 th 相同的數量。

 <tr>
     <th>heading1</th>
     <th>heading2</th>
     <th>heading3</th>
 </tr>

您現在可以為您的行迭代它。

 <tr> 
   <td>data1</td>
   <td>data2</td>
   <td>data3</td>
 </tr>

您只為每一行顯示一個 TD。

您缺少的是行循環:

 while($row = mysqli_fetch_array($result))
 { ?>
  <tbody style="font-size:20px; text-align:center;">
    <tr>

    <?php 
      foreach($row as $k=>$v)
        echo "<td>$v</td>";
    ?>


    </tr> 
<?php }  mysqli_close($connect);?> 
  </tbody>
</table>

或者,您應該手動為所有列 output TD:

<tr>
<td><?php echo $row["Column1"];?></td>
<td><?php echo $row["Column2"];?></td>
<td><?php echo $row["Column3"];?></td>
<td><?php echo $row["Column4"];?></td>
<td><?php echo $row["Column5"];?></td>
<td><?php echo $row["Column6"];?></td>
...
</tr> 

您需要一個交叉表查詢來獲得您想要的結果。 由於信息很少,我只會給出和概述

SELECT info,
       sum( if(MONTH(dt)=1,1,0) as Period_1,
       sum( if(MONTH(dt)=2,1,0) as Period_2,
//--- repeat for all 12 months

您的查詢將返回 13 列(信息和 Period_1 到 Period_12)。 您需要調整 html 以適應此 output。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM