如何用php多列显示数据?
您好,我需要从 mySQL 表中构建一个包含两列的表。
这就是我现在所拥有的:
<table>
<?php
$sql = "SELECT field1, field2 FROM tblX"
$result = mysql_query($sql) or die("\nError Retrieving Records.");
while($row = mysql_fetch_array ($result, MYSQL_ASSOC)){?>
<tr>
<td>
<?=$row['field1']?> - <?=$row['field2']?>
</td>
</tr>
<?php }?>
</table>
这将创建一个列表,如下所示:
1
2
3
4
5
我需要该表位于两列中,如下所示:
1 4
2 5
3
如果我该怎么做,这可能吗?
Hi I need to build a table with two columns from a mySQL table.
Here is what i have now:
<table>
<?php
$sql = "SELECT field1, field2 FROM tblX"
$result = mysql_query($sql) or die("\nError Retrieving Records.");
while($row = mysql_fetch_array ($result, MYSQL_ASSOC)){?>
<tr>
<td>
<?=$row['field1']?> - <?=$row['field2']?>
</td>
</tr>
<?php }?>
</table>
This will create one column table like so:
1
2
3
4
5
I need the table to be in two columns like this:
1 4
2 5
3
Is this possible if it is how do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您坚持要一张桌子,这应该可以满足您的要求。
获取中点:
获取数组:
输出行:
不过,我同意 marco 的观点。您可以轻松地在一个 div 中列出从第一个到 $mid 的项目,然后在另一个 div 中从 $mid 到末尾列出项目,并使用 CSS 并排浮动 div。使用表格进行格式化是邪恶的。
This should do what you want, if you insist on a table.
Get the mid point:
Get an array:
Output the rows:
I agree with marco, though. You could just as easily list items one to $mid in one div, then $mid to the end in another, and use CSS to float the divs side-by-side. Using tables for formatting purposes is evil.
读取列表中的所有值,获取半值,编写一个循环来同时显示 [i] 和 [i+halfvalue]
read all values in a list, get the half value, write a loop that shows [i] and [i+halfvalue] together
您的问题不清楚,但听起来您想重新排列数据的显示方式,并且不希望标准的逐行输出。如果您的订单需要无法循环的方法,您可能应该手动执行此操作。
您应该在输出表格之前先安排好表格,然后一旦一切都整洁,只需输出代码即可。
Your question is unclear, but it sounds like you want to rearrange how the data is displayed and don't want the standard row-by-row output. You should probably do this manually if your order requires a method that cannot be looped through.
You should arrange the table BEFORE you output it, then once you've got everything all tidy, simply output the code.
一定要在表中吗?
如果其中有一个 div 并且将其设置为高度中可以包含 3 个项目。
Must it be in tables?
If you have in it a div and you have it set so that 3 items can contain in the height.