PHP-如何创建4列(4 x 25%),然后以4的新列结束行

PHP-如何创建4列(4 x 25%),然后以4的新列结束行,php,html,css,Php,Html,Css,我有一个项目正在进行中,我们可以创建一个产品,并用PHP加载到首页。 但是,如果有5种产品,则不会创建新的生产线。如果有5个产品,则应显示2行,每行4个 <section class="ICO"> <div class="one-fourth-gridtable"> <table class="grid table"> <?php while ($subject = mysqli_f

我有一个项目正在进行中,我们可以创建一个产品,并用PHP加载到首页。
但是,如果有5种产品,则不会创建新的生产线。如果有5个产品,则应显示2行,每行4个

<section class="ICO">
    <div class="one-fourth-gridtable">
        <table class="grid table">
            <?php
            while ($subject = mysqli_fetch_assoc($subject_set)) {
                $imagenavn = $subject['navn'];
                $sql = "SELECT * FROM product_images WHERE image_name = '$imagenavn'";
                $sth = $db->query($sql);
                $result = mysqli_fetch_array($sth);
                ?>
                <td>
                    <div style="border: 1px solid black; border-radius:2000px;">
                        <img src="data:image/jpeg;base64,<?= base64_encode($result['image']); ?>" />
                    </div>
                    <a class="action" href="<?= url_for('../show.php?id=' . h(u($subject['id']))); ?>"><?php echo h($subject['navn']); ?></a>
                </td>      
            <?php } ?>
        </table>
    </div>
</section>

表自动展开以包含单行上的所有元素

为了解决这个问题,您可以只使用新的表行(
),尽管我建议您切换到
布局,并将元素浮动到
左侧

<div class="contents">
 // output the contents
</div>

.contents img {
  float: left;
}

表格会自动展开以包含单行中的所有元素

为了解决这个问题,您可以只使用新的表行(
),尽管我建议您切换到
布局,并将元素浮动到
左侧

<div class="contents">
 // output the contents
</div>

.contents img {
  float: left;
}

首先计算要显示的结果,例如$count

<table class="grid table">
<tr>
<?php 
$trer = 1;
$cycler = 0;
while($subject = mysqli_fetch_assoc($subject_set))
{
     $cycler++;
                 $imagenavn = $subject['navn'];
                 $sql = "SELECT * FROM product_images WHERE image_name = '$imagenavn'";
                 $sth = $db->query($sql);
                 $result=mysqli_fetch_array($sth);
 ?>

            <td>YOUR CONTENT</td>
<?php
if($trer == 4 && $cycler < $count) //if true you're at the last td of the line, but not on the last line, so let's put a new line
  {
    echo "</tr><tr>";
    $trer = 0;
  }
$trer++;
}
if($trer > 0 && $trer < 4) //if line is incomplete (less than 4 items but 1 or more
{
  for(int t = 0; $t < 4 - $trer; $t++) //echo empty TDs till filling the line
     echo "<td>&nbsp;</td>";
}
?>
</tr>
</table>


首先计算要显示的结果,例如$count

<table class="grid table">
<tr>
<?php 
$trer = 1;
$cycler = 0;
while($subject = mysqli_fetch_assoc($subject_set))
{
     $cycler++;
                 $imagenavn = $subject['navn'];
                 $sql = "SELECT * FROM product_images WHERE image_name = '$imagenavn'";
                 $sth = $db->query($sql);
                 $result=mysqli_fetch_array($sth);
 ?>

            <td>YOUR CONTENT</td>
<?php
if($trer == 4 && $cycler < $count) //if true you're at the last td of the line, but not on the last line, so let's put a new line
  {
    echo "</tr><tr>";
    $trer = 0;
  }
$trer++;
}
if($trer > 0 && $trer < 4) //if line is incomplete (less than 4 items but 1 or more
{
  for(int t = 0; $t < 4 - $trer; $t++) //echo empty TDs till filling the line
     echo "<td>&nbsp;</td>";
}
?>
</tr>
</table>


有了这段代码,你在4个图像后得到一个新行,在8个图像后循环停止,因为$u。如果不需要,您可以删除/更改$u


使用此代码,您可以在4个图像后获得一个新行,在8个图像后,由于$u,循环停止。如果不需要,您可以删除/更改$u



使用generic
div
s可能会更好地实现您试图实现的结构,一个表可以在一行中容纳尽可能多的单元格或列,只要您不断向
tr
元素添加
td
元素。“如何创建4列(4 x 25%),然后以4“。。。这是否意味着您需要4列表格单元格,并从第5列开始,移动到后续行?您是否可以更新您的问题以包含示例输出(例如HTML、图形)?您试图实现的结构可能更好地使用generic
div
s完成,一个表可以容纳尽可能多的单元格或列,只要您继续向
tr
元素添加
td
元素。“如何创建4列(4 x 25%),然后以新的4列结束行”…这是否意味着您需要4列表格单元格,并从第5列开始,移动到后续行?您是否可以更新您的问题以包括示例输出(例如HTML、图形)?