如何用php多列显示数据?

发布于 2024-11-28 01:18:49 字数 557 浏览 1 评论 0原文

您好,我需要从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哭泣的笑容 2024-12-05 01:18:49

如果您坚持要一张桌子,这应该可以满足您的要求。

获取中点:

$mid = ceil(mysql_num_rows($result)/2);

获取数组:

while( $row = mysql_fetch_row($result) ) {
    $list[] = $row; }

输出行:

for( $i = 0; $i < $mid; $i++ ) {
    $itemOne = $list[$i];
    $itemTwo = $list[$i + $mid]; 
    // echo them in two tds.
}

不过,我同意 marco 的观点。您可以轻松地在一个 div 中列出从第一个到 $mid 的项目,然后在另一个 div 中从 $mid 到末尾列出项目,并使用 CSS 并排浮动 div。使用表格进行格式化是邪恶的。

This should do what you want, if you insist on a table.

Get the mid point:

$mid = ceil(mysql_num_rows($result)/2);

Get an array:

while( $row = mysql_fetch_row($result) ) {
    $list[] = $row; }

Output the rows:

for( $i = 0; $i < $mid; $i++ ) {
    $itemOne = $list[$i];
    $itemTwo = $list[$i + $mid]; 
    // echo them in two tds.
}

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.

像极了他 2024-12-05 01:18:49

读取列表中的所有值,获取半值,编写一个循环来同时显示 [i] 和 [i+halfvalue]

read all values in a list, get the half value, write a loop that shows [i] and [i+halfvalue] together

自由如风 2024-12-05 01:18:49

您的问题不清楚,但听起来您想重新排列数据的显示方式,并且不希望标准的逐行输出。如果您的订单需要无法循环的方法,您可能应该手动执行此操作。

您应该在输出表格之前先安排好表格,然后一旦一切都整洁,只需输出代码即可。

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.

毁梦 2024-12-05 01:18:49

一定要在表中吗?
如果其中有一个 div 并且将其设置为高度中可以包含 3 个项目。

<div id="container">
  <div class="item">1</div>
  <div class="item">2</div> 
  <div class="item">3</div>
  <div class="item">4</div>
  <div class="item">5</div>
</div>

#container { height: 300px; }
.item { height: 100px; }

You can use some css floating to solve that combined with some padding and margin. 

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.

<div id="container">
  <div class="item">1</div>
  <div class="item">2</div> 
  <div class="item">3</div>
  <div class="item">4</div>
  <div class="item">5</div>
</div>

#container { height: 300px; }
.item { height: 100px; }

You can use some css floating to solve that combined with some padding and margin. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文