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



我有一个数据库表,该表有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>

使用模运算符(%):

http://www.devchunks.com/web-development/using-the-php-modulus-operator/

像这样的东西:

<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以完成这一行并开始下一行。(我已经使用了%,所以它将每三个条目包装一次,而不仅仅是第一个)

好吧,您可以在sql查询中正确地获取这些信息(只是一个适合的示例http://en.wikibooks.org/wiki/MySQL/Pivot_table)。

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

旧学校:mysql_query()和while($row=mysql_fetch_array())新学校:PDO(http://de.php.net/manual/en/book.pdo.php)

太棒了!非常感谢。这对我有用,赞德。你可以试试这样的东西。

   <table width="1024px" border="0" cellspacing="2" cellpadding="2">  
  <?php
      $i = 0;
      foreach ($this->rows as $row )
      {
          $img = IMAGE_PATH . '/' . 'gallery/' . $row->gly_thumbnail;
          if ($i % 3 == 0)
          {
              echo '<tr>';
          }
  ?>
  <td align="center">
    <img src="<?php echo $img; ?>" width="300" height="215"><br/>
    <?php echo $row->gly_title; ?>
  </td>
  <?php        
          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>
                    <?php if ($i++%$_columnCount==0): ?>
                        <tr>
                    <?php endif ?>
                  
                   
                    <td> <img src="<?php echo site_url('uploads/shelter_images/'.$row->shelter_id."/".$img->imagefile) ?>" alt="" width="300" ></td>
                    
                     <?php if ($i%$_columnCount==0 || $i==$totalImg): ?>
                      </tr>
                   <?php endif; ?> 
            
            <?php  } ?>

最新更新