我需要使用php在三个不同的列中显示MySql表的三个记录。
SELECT * FROM TABLE1 WHERE Id IN(1,2,3)
我想在这里显示结果
我该如何写LOOP?
while(loop condition)
{
//what will go here?
}
更新:第一行字段将显示在html表的第一列中,第二记录字段将显示在第二列中,依此类推...我不是在要求仅显示三个记录
OP说,这不是那么简单。但它是。
因此,您有2种方法可以做到这一点。
第一的。在这种情况下,您将遍历3列。获取第一行。这会将所有数据放入一个div中。类名是column_1
。对其他3.这样做,然后使div彼此向左浮动。
$i = 1;
while($row = $db->fetch_row()) {
?>
<div class="column_<?php echo $i; ?>">
<div class="picture">
<?php echo $row["image"]; ?>
</div>
<div class="description">
<?php echo $row["desc"]; ?>
</div>
... and so on...
</div>
<?php
$i++;
}
第二个是,当您第一次收集约3行的数据,然后将它们逐行放入表中时。
<?php
while($row = $db->fetch_row()) {
$results[] = $row;
}
?>
<table>
<tr>
<td><?php echo $result[0]['image'] ?></td>
<td><?php echo $result[1]['image'] ?></td>
<td><?php echo $result[2]['image'] ?></td>
</tr>
<tr>
<td><?php echo $result[0]['desc'] ?></td>
<td><?php echo $result[1]['desc'] ?></td>
<td><?php echo $result[2]['desc'] ?></td>
</tr>
</table>
编辑
我忘记了,还有第三个解决方案。您可以将表构建为空,然后可以使用jQuery的ajax调用来更新单元格。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句