PHP 在 mysqli_fetch_array 时打断 Table 行
PHP Break Table Row on While mysqli_fetch_array
我有下面的代码显示 table td 中的数据,目前我正在显示四个 table td,但是当我增加 8 或 12 之类的 no 时,它就一团糟了。我需要每 4 td 开始一个新行。我怎样才能做到这一点?
<tr style="background-color:white;">
<?php
while ($row = mysqli_fetch_array($rs_result)) {
// Display each field of the records.
?>
<td style="vertical-align:middle;padding-top:0px;margin-top:0px;width:32px"><img styel="height:32px;width: 32px;" src="<?php echo $row["gravator"]; ?>"/></td>
<td style="vertical-align:top;margin-left:10px;text-align:left !important;">
<!--<span><img src="<?php echo $row["gravator"]; ?>"/</span>-->
<span style="">#Ad id:<?php echo $row["id"]; ?></span><br>
<span style="margin-left:2px;border-top:1px solid blue;" class="node-text"><?php echo $row["fullname"]; ?> Has Added</span> <br>
<span style="position:absolute;margin-top:-15px;margin-left:2px;width:160px;" class="node-text"><a target="_blank" href="<?php echo $row["url"]; ?>"><?php echo $row["title"]; ?></a></span> <br>
<span style="position:absolute;margin-top:-26px;font-size:smaller;color:silver;margin-left:2px;" class="node-text">On <?php echo $row["datetime"]; ?></span>
</td>
<?php
};
?> </tr>
嗯...这不是最漂亮的方法...但我想它应该适合您。我为 max_cols
添加了一个计数器,并每第 4 个单元格添加新的 <tr>
:
<tr style="background-color:white;">
<?php
$max_cols = 4;
while ($row = mysqli_fetch_array($rs_result)) {
// Display each field of the records.
?>
<td style="vertical-align:middle;padding-top:0px;margin-top:0px;width:32px"><img styel="height:32px;width: 32px;" src="<?php echo $row["gravator"]; ?>"/></td>
<td style="vertical-align:top;margin-left:10px;text-align:left !important;">
<!--<span><img src="<?php echo $row["gravator"]; ?>"/</span>-->
<span style="">#Ad id:<?php echo $row["id"]; ?></span><br>
<span style="margin-left:2px;border-top:1px solid blue;" class="node-text"><?php echo $row["fullname"]; ?> Has Added</span> <br>
<span style="position:absolute;margin-top:-15px;margin-left:2px;width:160px;" class="node-text"><a target="_blank" href="<?php echo $row["url"]; ?>"><?php echo $row["title"]; ?></a></span> <br>
<span style="position:absolute;margin-top:-26px;font-size:smaller;color:silver;margin-left:2px;" class="node-text">On <?php echo $row["datetime"]; ?></span>
</td>
<?php
$max_cols--;
if ($max_cols == 0) {
echo '</tr> <tr style="background-color:white">';
$max_cols = 4;
}
};
?> </tr>
我建议你 echo
你的 tr
/td
并且每四行 echo
新 tr
:
$count = 1;
while ($row = mysqli_fetch_array($rs_result)) {
echo $count % 4 == 0 ? "<tr>": '';
echo "<td>Here is a content of your td</td>"
echo $count % 4 == 0 ? "</tr>": '';
}
基本上,您需要计算 while()
循环中的每个 <td></td>
。当该计数达到您的限制时,即:4,您开始新的一行并将计数器重置为 1。您可以使用 $maxTDs
(如下)设置每行的 <td>
s 的数量。
这没有经过测试,但应该可以工作 and/or 让你真正接近:
<tr style="background-color:white;">
<?php
$tdCnt = 0; // to start
$maxTDs = 4; // each row has 4 <td>s
while ($row = mysqli_fetch_array($rs_result)) {
if( $tdCnt == $maxTDs ) {
# close the row
print "</tr>";
# start a new row;
?>
<tr style="background-color:white;">
<?php
# reset $tdCnt
$tdCnt = 0;
}
// Display each field of the records.
?>
<td style="vertical-align:middle;padding-top:0px;margin-top:0px;width:32px"><img styel="height:32px;width: 32px;" src="<?php echo $row["gravator"]; ?>"/></td>
<?php $tdCnt++; ?>
<td style="vertical-align:top;margin-left:10px;text-align:left !important;">
<!--<span><img src="<?php echo $row["gravator"]; ?>"/</span>-->
<span style="">#Ad id:<?php echo $row["id"]; ?></span><br>
<span style="margin-left:2px;border-top:1px solid blue;" class="node-text"><?php echo $row["fullname"]; ?> Has Added</span> <br>
<span style="position:absolute;margin-top:-15px;margin-left:2px;width:160px;" class="node-text"><a target="_blank" href="<?php echo $row["url"]; ?>"><?php echo $row["title"]; ?></a></span> <br>
<span style="position:absolute;margin-top:-26px;font-size:smaller;color:silver;margin-left:2px;" class="node-text">On <?php echo $row["datetime"]; ?></span>
</td>
<?php $tdCnt++; ?>
<?php
};
?>
我有下面的代码显示 table td 中的数据,目前我正在显示四个 table td,但是当我增加 8 或 12 之类的 no 时,它就一团糟了。我需要每 4 td 开始一个新行。我怎样才能做到这一点?
<tr style="background-color:white;">
<?php
while ($row = mysqli_fetch_array($rs_result)) {
// Display each field of the records.
?>
<td style="vertical-align:middle;padding-top:0px;margin-top:0px;width:32px"><img styel="height:32px;width: 32px;" src="<?php echo $row["gravator"]; ?>"/></td>
<td style="vertical-align:top;margin-left:10px;text-align:left !important;">
<!--<span><img src="<?php echo $row["gravator"]; ?>"/</span>-->
<span style="">#Ad id:<?php echo $row["id"]; ?></span><br>
<span style="margin-left:2px;border-top:1px solid blue;" class="node-text"><?php echo $row["fullname"]; ?> Has Added</span> <br>
<span style="position:absolute;margin-top:-15px;margin-left:2px;width:160px;" class="node-text"><a target="_blank" href="<?php echo $row["url"]; ?>"><?php echo $row["title"]; ?></a></span> <br>
<span style="position:absolute;margin-top:-26px;font-size:smaller;color:silver;margin-left:2px;" class="node-text">On <?php echo $row["datetime"]; ?></span>
</td>
<?php
};
?> </tr>
嗯...这不是最漂亮的方法...但我想它应该适合您。我为 max_cols
添加了一个计数器,并每第 4 个单元格添加新的 <tr>
:
<tr style="background-color:white;">
<?php
$max_cols = 4;
while ($row = mysqli_fetch_array($rs_result)) {
// Display each field of the records.
?>
<td style="vertical-align:middle;padding-top:0px;margin-top:0px;width:32px"><img styel="height:32px;width: 32px;" src="<?php echo $row["gravator"]; ?>"/></td>
<td style="vertical-align:top;margin-left:10px;text-align:left !important;">
<!--<span><img src="<?php echo $row["gravator"]; ?>"/</span>-->
<span style="">#Ad id:<?php echo $row["id"]; ?></span><br>
<span style="margin-left:2px;border-top:1px solid blue;" class="node-text"><?php echo $row["fullname"]; ?> Has Added</span> <br>
<span style="position:absolute;margin-top:-15px;margin-left:2px;width:160px;" class="node-text"><a target="_blank" href="<?php echo $row["url"]; ?>"><?php echo $row["title"]; ?></a></span> <br>
<span style="position:absolute;margin-top:-26px;font-size:smaller;color:silver;margin-left:2px;" class="node-text">On <?php echo $row["datetime"]; ?></span>
</td>
<?php
$max_cols--;
if ($max_cols == 0) {
echo '</tr> <tr style="background-color:white">';
$max_cols = 4;
}
};
?> </tr>
我建议你 echo
你的 tr
/td
并且每四行 echo
新 tr
:
$count = 1;
while ($row = mysqli_fetch_array($rs_result)) {
echo $count % 4 == 0 ? "<tr>": '';
echo "<td>Here is a content of your td</td>"
echo $count % 4 == 0 ? "</tr>": '';
}
基本上,您需要计算 while()
循环中的每个 <td></td>
。当该计数达到您的限制时,即:4,您开始新的一行并将计数器重置为 1。您可以使用 $maxTDs
(如下)设置每行的 <td>
s 的数量。
这没有经过测试,但应该可以工作 and/or 让你真正接近:
<tr style="background-color:white;">
<?php
$tdCnt = 0; // to start
$maxTDs = 4; // each row has 4 <td>s
while ($row = mysqli_fetch_array($rs_result)) {
if( $tdCnt == $maxTDs ) {
# close the row
print "</tr>";
# start a new row;
?>
<tr style="background-color:white;">
<?php
# reset $tdCnt
$tdCnt = 0;
}
// Display each field of the records.
?>
<td style="vertical-align:middle;padding-top:0px;margin-top:0px;width:32px"><img styel="height:32px;width: 32px;" src="<?php echo $row["gravator"]; ?>"/></td>
<?php $tdCnt++; ?>
<td style="vertical-align:top;margin-left:10px;text-align:left !important;">
<!--<span><img src="<?php echo $row["gravator"]; ?>"/</span>-->
<span style="">#Ad id:<?php echo $row["id"]; ?></span><br>
<span style="margin-left:2px;border-top:1px solid blue;" class="node-text"><?php echo $row["fullname"]; ?> Has Added</span> <br>
<span style="position:absolute;margin-top:-15px;margin-left:2px;width:160px;" class="node-text"><a target="_blank" href="<?php echo $row["url"]; ?>"><?php echo $row["title"]; ?></a></span> <br>
<span style="position:absolute;margin-top:-26px;font-size:smaller;color:silver;margin-left:2px;" class="node-text">On <?php echo $row["datetime"]; ?></span>
</td>
<?php $tdCnt++; ?>
<?php
};
?>