[英]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> </td>
<td> </td>
<td> </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.