简体   繁体   English

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

[英]iterate a html table of two row and 3 column using one php query

I have a database table and that table has 6 rows. 我有一个数据库表,该表有6行。 What I want is to display that 6 rows in a html page using a 3 column and 2 row table. 我想要的是使用3列和2行表在html页中显示6行。

I know how to work with php arrays and while loops. 我知道如何使用php数组和while循环。 My problem is how to limit the array to put 3 items in the first row and put the other 3 in the next row. 我的问题是如何限制数组在第一行中放置3个项目,在下一行中放置其他3个项目。

this is what i have tried but didn't work 这是我尝试过但没有奏效的

<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>

Use modulo operator (%): 使用模运算符(%):

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

something like this: 像这样的东西:

<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>

At its base, you'll need something like this: 从根本上讲,您将需要以下内容:

<table>
<tr>

<?

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

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

?>

</tr>
</table>

Start printing out the header of your table, and then begin iterating through the dataset. 开始打印表头,然后开始遍历数据集。 Keep track of how many you've printed out, and if this is the third one, print the HTML to finish this row and start the next one. 跟踪已打印的数量,如果这是第三个,则打印HTML以完成这一行并开始下一个。 (I've used % , so it'll wrap on every third entry, not just the first one) (我用过% ,所以它将包裹在每第三个条目上,而不仅仅是第一个)

Well, you could correctly fetch those informations in your sql-query ( just one example that could fit http://en.wikibooks.org/wiki/MySQL/Pivot_table ). 好吧,您可以在sql查询中正确获取这些信息(仅是一个适合http://en.wikibooks.org/wiki/MySQL/Pivot_table的示例)。

Or simply fetch everything into PHP arrays. 或者只是将所有内容提取到PHP数组中。

Oldschool: mysql_query() and while( $row = mysql_fetch_array() ) Newchool: PDO ( http://de.php.net/manual/en/book.pdo.php ) Oldschool:mysql_query()和while($ row = mysql_fetch_array())Newchool:PDO( http://de.php.net/manual/zh/book.pdo.php

Awesome! 太棒了! Thanks a lot. 非常感谢。 It works for me, Zend. Zend对我有用。 You can try something like this. 您可以尝试这样。

   <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Laravel雄辩的查询在一张表中获得一行的两列 - How to get two column of one row in one table using laravel eloquent query 使用php拉特定的表行列数据以html输出 - pull specific table row column data to output in html using php PHP:在一个查询中的两个表 - PHP: two table in one query php显示表格; 第一列与mysql列注释和第二列转置的行结果 - php to display table; column one with mysql column comments and column two transposed row results 使用php更改在MySQL查询中生成的html表列的值 - Change value of html table column, generated in MySQL query, using php PHP MySQL查询-数组将数据放入表中的一列,而表中需要两列 - PHP MySQL Query - Array places data into one column in table , wanting two columns in the table PHP多维数组到两列HTML表 - PHP Multidimensional Array to Two Column HTML Table PHP,HTML和MySqli-如何在一个表行中显示特定列的所有数据 - PHP, HTML and MySqli - how to show all data from specific column in one table row PHP MySQL根据另一张表中的ID在一行中查询两个用户名 - Php mysql Query two usernames in one row based on id from another table 如何在表一中为每个线程数据行插入注释数据,如何在php和mysql中为表二使用表格 - how to insert comment data for each thread data row in table one, using form for table two in php and mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM