以列方式而不是行方式显示来自 foreach 循环的数据
showing data from foreach loop in column way instead of rows way
我尝试使用 foreach 循环从数据库中获取数据并以列的方式显示它们,目前我收到的数据与此
上显示的一样
但我需要它看起来像这样
| Founders Circle | Global Pool | Universal Pool | Infinity Pool | Regional Pool |
|78156 -and input- |1021673-input | 5000000 - input | 56823 - input | 0 and input |
<?php
foreach( $calculations as $calculation ) {
?>
<table>
<tr>
<th>
<?php echo $calculation['Calculation']['poolname']; ?>
</th>
</tr>
<tr>
<td>
<div class="input text" id="pool_calc">
<?php echo $calculation['Calculation']['total_units']; ?>
<input name="own_pools" type="text" value="" id="own_pools" maxlength="3" size="4" >
</div>
</td>
</tr>
<?php
}
?>
</table>
出了什么问题或我该如何解决?
还有很多方法,最容易理解的方法是使用两个相同的 foreach
循环。
<table>
<tr>
<?php
foreach( $calculations as $calculation ) {
// All TH in the first row
?>
<th>
<?php echo $calculation['Calculation']['poolname']; ?>
</th>
<?php
}
?>
</tr>
<tr>
<?php
foreach( $calculations as $calculation ) {
// All TD in the second row
?>
<td>
<div class="input text" id="pool_calc">
<?php echo $calculation['Calculation']['total_units']; ?>
<input name="own_pools" type="text" value="" id="own_pools" maxlength="3" size="4" >
</div>
</td>
<?php
}
?>
</tr>
</table>
在问题的代码中,您为每条记录创建了新的 table 和 2 行(您总共有 5 tables)。
基本思路是将 <table>
移到循环外。
更新了执行某些操作的代码,没有重复 foreach 循环(将 TD 保存在变量中并稍后回显)。
<table>
<tr>
<?php
$tds = '';
foreach( $calculations as $calculation ) {
// All TH in the first row
?>
<th>
<?php echo $calculation['Calculation']['poolname']; ?>
</th>
<?php
// create TDs code
$tds .= '
<td>
<div class="input text" id="pool_calc">
' . $calculation['Calculation']['total_units'] . '
<input name="own_pools" type="text" value="" id="own_pools" maxlength="3" size="4" >
</div>
</td>
';
}
?>
</tr>
<tr>
<?php
echo $tds;
// echo TDs in the second row
?>
</tr>
</table>
我尝试使用 foreach 循环从数据库中获取数据并以列的方式显示它们,目前我收到的数据与此
但我需要它看起来像这样
| Founders Circle | Global Pool | Universal Pool | Infinity Pool | Regional Pool |
|78156 -and input- |1021673-input | 5000000 - input | 56823 - input | 0 and input |
<?php
foreach( $calculations as $calculation ) {
?>
<table>
<tr>
<th>
<?php echo $calculation['Calculation']['poolname']; ?>
</th>
</tr>
<tr>
<td>
<div class="input text" id="pool_calc">
<?php echo $calculation['Calculation']['total_units']; ?>
<input name="own_pools" type="text" value="" id="own_pools" maxlength="3" size="4" >
</div>
</td>
</tr>
<?php
}
?>
</table>
出了什么问题或我该如何解决?
还有很多方法,最容易理解的方法是使用两个相同的 foreach
循环。
<table>
<tr>
<?php
foreach( $calculations as $calculation ) {
// All TH in the first row
?>
<th>
<?php echo $calculation['Calculation']['poolname']; ?>
</th>
<?php
}
?>
</tr>
<tr>
<?php
foreach( $calculations as $calculation ) {
// All TD in the second row
?>
<td>
<div class="input text" id="pool_calc">
<?php echo $calculation['Calculation']['total_units']; ?>
<input name="own_pools" type="text" value="" id="own_pools" maxlength="3" size="4" >
</div>
</td>
<?php
}
?>
</tr>
</table>
在问题的代码中,您为每条记录创建了新的 table 和 2 行(您总共有 5 tables)。
基本思路是将 <table>
移到循环外。
更新了执行某些操作的代码,没有重复 foreach 循环(将 TD 保存在变量中并稍后回显)。
<table>
<tr>
<?php
$tds = '';
foreach( $calculations as $calculation ) {
// All TH in the first row
?>
<th>
<?php echo $calculation['Calculation']['poolname']; ?>
</th>
<?php
// create TDs code
$tds .= '
<td>
<div class="input text" id="pool_calc">
' . $calculation['Calculation']['total_units'] . '
<input name="own_pools" type="text" value="" id="own_pools" maxlength="3" size="4" >
</div>
</td>
';
}
?>
</tr>
<tr>
<?php
echo $tds;
// echo TDs in the second row
?>
</tr>
</table>