使用一个php查询迭代两行三列的html表

使用一个php查询迭代两行三列的html表,php,html,arrays,while-loop,Php,Html,Arrays,While Loop,我有一个数据库表,该表有6行。我想要的是使用一个3列2行的表在html页面中显示6行 我知道如何使用php数组和while循环。我的问题是如何限制数组将3个项放在第一行,将其他3个项放在下一行 这是我尝试过的,但没有成功 <div id="maincontent"> <!-- class one --> <?php $getSection = getSection(); $i=0; while($al

我有一个数据库表,该表有6行。我想要的是使用一个3列2行的表在html页面中显示6行

我知道如何使用php数组和while循环。我的问题是如何限制数组将3个项放在第一行,将其他3个项放在下一行

这是我尝试过的,但没有成功

<div id="maincontent">
  <!-- class one -->
      <?php 
        $getSection = getSection();
        $i=0;
        while($allSection = mysql_fetch_array($getSection)){
      ?>
      <div class="subconent">
<table width="937" border="0">
  <tr>
    <td>
        <div class="sub_image">
            <a href="section.php?id=<?php echo urlencode($allSection['id']); ?>"><img src="admin/uploads/fron_sect/<?php echo $allSection['image']; ?>" width="134" height="120" border="0" alt="HNA" class="PopBoxImageLink"  onmouseover="PopEx(this,-50,-25,205,186,20,null);" onclick="window.location='http://localhost/hants/section.php?id=<?php echo urlencode($allSection['id']); ?>'" /></a>
        </div>
            <div class="cont_txt">
              <h3><?php echo $allSection['name_full']; ?></h3>
                <p><?php echo substr($allSection['description'],0,140) . ""; ?></p>
                <br />
              <a href="section.php?id=<?php echo urlencode($allSection['id']); $i++; ?>"><img src="images/read_more.jpg" alt="Read More" width="89" height="25" border="0" /></a>
            </div>
    </td>
  </tr>
</table>
</div>
<?php 
if($i==4) { ?>
<table width="937" border="0">
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td></tr>
<tr><div class="sub_image">
            <a href="section.php?id=<?php echo urlencode($allSection['id']); ?>"><img src="admin/uploads/fron_sect/<?php echo $allSection['image']; ?>" width="134" height="120" border="0" alt="HNA" class="PopBoxImageLink"  onmouseover="PopEx(this,-50,-25,205,186,20,null);" onclick="window.location='http://localhost/hants/section.php?id=<?php echo urlencode($allSection['id']); ?>'" /></a>
        </div>
            <div class="cont_txt">
              <h3><?php echo $allSection['name_full']; ?></h3>
                <p><?php echo substr($allSection['description'],0,140) . ""; ?></p>
                <br />
              <a href="section.php?id=<?php echo urlencode($allSection['id']); ?>"><img src="images/read_more.jpg" alt="Read More" width="89" height="25" border="0" /></a>
            </div><td>
<?php   }
} ?>
  </div>

好的,您可以在sql查询中正确地获取这些信息,这只是一个合适的示例

或者简单地将所有内容提取到PHP数组中

Oldschool:mysql\u query和while$row=mysql\u fetch\u数组
Newchool:PDO

使用模运算符%:

大概是这样的:

<table>

<?php

$i = 0;
while ( $row = mysql_fetch_array($result) ){
    if ($i % 3 == 0){
        echo '<tr>';
    }
    echo '<td>'.$row['column_name'].'</td>';
    if ($i % 3 == 2){
        echo '</tr>';
    }
    $i++; 
}

//here is a check in case you don't have multiple of 3 rows
if ($i % 3 != 0){
    echo '</tr>';
}

?>

</table>
<table>
<tr>

<?

$count = 0;
foreach ($row) {
    echo "<td>" . $row["value"] ."</td>";
    $count++;

    if (($count % 3) == 0) && ($count > 0) {
        echo ("</tr><tr>");
    }
}

?>

</tr>
</table>

在其基础上,您将需要以下内容:

<table>

<?php

$i = 0;
while ( $row = mysql_fetch_array($result) ){
    if ($i % 3 == 0){
        echo '<tr>';
    }
    echo '<td>'.$row['column_name'].'</td>';
    if ($i % 3 == 2){
        echo '</tr>';
    }
    $i++; 
}

//here is a check in case you don't have multiple of 3 rows
if ($i % 3 != 0){
    echo '</tr>';
}

?>

</table>
<table>
<tr>

<?

$count = 0;
foreach ($row) {
    echo "<td>" . $row["value"] ."</td>";
    $count++;

    if (($count % 3) == 0) && ($count > 0) {
        echo ("</tr><tr>");
    }
}

?>

</tr>
</table>

开始打印表的标题,然后开始遍历数据集。跟踪打印出的数量,如果这是第三行,则打印HTML以完成这一行并开始下一行。我已经使用了%,所以它将在每三个条目上包装,而不仅仅是第一个条目

太棒了!谢谢。这对我有用,泽德。你可以试试这样的。


我现在把这些代码放在我的问题里,问题太模糊了。您可以显示表中的示例数据集,您可以添加所需表输出的示例,以及您尝试过的最重要的示例。谢谢,这解决了我的问题,但有一个小问题。当我使用echo时,我必须使用或来包装echo语句。但是我有更多的html内容需要使用,所以我能说什么呢?我会更新答案,但我真的不知道这对你有什么伤害,html内容仍然应该在屏幕上正确打印出来。