显示一行中的字段+ PHP



您好,我想在一行中显示DB中的值。但是现在,它是水平显示的

图像样本

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

我想显示与周期1 - 12 '一致的字符串值

我还在努力学习。

首先,将while循环移到<tr>之前,因为您只想要一个表体。那么最好有相同数目的<th><td>。我看到你有12个<th>s,所以在每个<tr>中创建12个<td>s(如果你想要,将它们保留为空,但包括它们)

标签是一个包含表格标题的行。要匹配列名,必须有与表中相同数量的列。

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

或者,应该手动输出所有列的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列(info和Period_1到Period_12)。您需要调整html以满足此输出。

最新更新