I have below code which is showing data in table td, Currently I am showing four table td but when I increase the no like 8 or 12 it is messing. I need to start a new row for every 4 td. How can I achieve this?
<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>
Well... this is not the most beatutiful approach... But I guess it should work for you. I add a counter for max_cols
and adds new <tr>
every 4th cell:
<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>
I recommend you to echo
your tr
/ td
and in every four rows echo
new 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>": '';
}
Basically, you need to count each <td></td>
in your while()
loop. When that count reaches your limit, ie: 4, you start a new row and reset the counter to 1. You can set the number of <td>
s per row with $maxTDs
(below).
This is not tested, but should work and/or get you really close:
<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
};
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.