【问题标题】:bootstrap and codeIgniter data display in tablebootstrap 和 codeIgniter 数据显示在表格中
【发布时间】:2018-07-09 14:54:40
【问题描述】:
<table class="table table-bordered table-hover">
<?php foreach ($pin_data as $pin) {
?>
<tr>
<td><?php echo $pin->location;?> <?php echo $pin->pincode;?></td>
</tr>
<?php
} ?>
</table>
我正在从数据库中获取数据。我想在表格中显示该数据,但我想连续显示 5 列。之后它从新行开始。请帮忙。
【问题讨论】:
标签:
php
twitter-bootstrap-3
foreach
html-table
codeigniter-3
【解决方案1】:
在第一个值中添加
,在第五个值中添加“”。它会运行。
<table class="table table-bordered table-hover">
<?php $timer=1;
foreach ($pin_data as $pin) {
if($timer%5==1)
{
?>
<tr>
<?php
}
?>
<td><?php echo $pin->location;?> <?php echo $pin->pincode;?></td>
if($timer%5==0)
{
?>
</tr>
<?php
}
<?php
$timer++;
} ?>
</table>
【解决方案2】:
希望对您有所帮助:
做这样的事情:
<table class="table table-bordered table-hover">
<?php foreach ($pin_data as $pin) { ?>
<tr>
/*change your column name as according to you*/
<td><?php echo $pin->location;?></td>
<td><?php echo $pin->pincode;?></td>
<td><?php echo $pin->location;?></td>
<td><?php echo $pin->pincode;?></td>
<td><?php echo $pin->location;?></td>
</tr>
<?php } ?>
</table>
【解决方案3】:
您可以使用模数运算符和计数器:
<table class="table table-bordered table-hover">
<tr> <!-- start initial row -->
<? $counter = 1;
foreach ($pin_data as $pin):?>
<td>
<?= $pin->location;?> <?= $pin->pincode;?>
</td>
<?if($counter % 5 == 0):?> //if $counter is a multiple of 5, close the current row and start an new one
</tr>
<tr>
<?endif;?>
<? $counter++;?>
<?endforeach;?>
</table>