我有一個從MySQL數組,現在我試圖把它放到一個4列使用while循環的表。我的方法是,當$counter%4==0
,我回顯</tr>
,使每4個項目將排列在一行。但是我不能得到我想要的結果,所以任何人都可以告訴我問題在哪裏,或者給我提供一個更好的方法來做到這一點?這是我的代碼:回聲一個數組從MySQL和在一個表中定義的行和列
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<?php
$query1 = "SELECT * FROM post WHERE post_status='New' AND post_type='Facilities' AND post_keywords='icon'";
$run1 = mysql_query($query1);
$counter=0;
while($row1 = mysql_fetch_array($run1)){
$post_id = $row1['post_id'];
$post_title = $row1['post_title'];
$post_date = $row1['post_date'];
$post_author = $row1['post_author'];
$post_keywords = $row1['post_keywords'];
$post_type =$row1['post_type'];
$post_content = $row1['post_content'];
$post_status =$row1['post_status'];
if($counter%4==0){
echo '<tr>';
}
?>
<td>//here is the content from the array
<div id="<?php echo $post_id?>" class="FacItem" onmousedown="function onmousedown() { fac_change('down','<?php echo $post_id?>') }" onmouseup="function onmouseup() { fac_change('up','<?php echo $post_id?>') }" onmouseover="function onmouseover() { fac_change('over','<?php echo $post_id?>') }" onmouseout="function onmouseout() { fac_change('out','<?php echo $post_id?>') }"><a href="?page_id=282"><?php echo $post_content?></a></div>
</td>
<?php
if($counter%4==0){
echo '</tr>';
}
$counter++;
}
?>
</tbody>
</table>
哇!爲什麼是負面投票? –
在解決方案中,您已發佈,當$ counter爲0,4,8等時,添加close行元素,這不是OP需要的。他想要4個元素的列。所以,聲明應該檢查$計數器,當它變成3,7,11等。 – Stoic
謝謝它的作品,我只需要重置cols! –