如何使用php在两列中显示数据

时间:2009-10-31 05:11:54

标签: php mysql html

如何在两列中显示数据?

 <?php
$query = "SELECT * FROM `release` WHERE artist_id = '$rcode' AND label_id = '$id'";
$result = mysql_query($query);
$tr_no = mysql_num_rows($result);
while ($info = mysql_fetch_array($result)) {
?>  
        <div>
        <table style="width: 100%">
                <tr>
                    <td ><img src="../artwork/<?php echo $info['label_id']; ?>/<?php echo $info['ID']; ?>.jpg" width="100" height="100" /></td>
                    <td valign="top">
                    <table style="width: 100%">
                        <tr>
                            <td style="width: 45px; height: 20px;" class="style5">
                            &nbsp;</td>
                            <td style="width: 180px"><?php echo $info['code']; ?></td>
                        </tr>
                        <tr>
                            <td style="width: 45px; height: 20px;" class="style5">
                            &nbsp;</td>
                            <td style="width: 180px"><?php echo $info['name']; ?></td>
                        </tr>
                        <tr>
                            <td style="width: 45px; height: 20px;" class="style5">
                            &nbsp;</td>
                            <td style="width: 180px"><?php echo trh($info['date']); ?></td>
                        </tr>
                        <tr>
                            <td style="width: 45px; height: 20px;" class="style5">
                            &nbsp;</td>
                            <td style="width: 180px">

                            </td>
                        </tr>
                    </table>
                    </td>
                </tr>
            </table>
        </div>
        <?php
        }
        ?>

通常我只使用一列。

使用与此代码中相同的字段在两列中显示它的最简单方法是什么?

6 个答案:

答案 0 :(得分:4)

答案还取决于您希望显示信息的方式。

如果我们假设您的数据库中有以下六个artist_id

1,2,3,4,5,6

您想以哪种方式展示它们?这样

1  2
3  4
5  6

还是这样?

1  4
2  5
3  6

更新:正如您所说,您希望以第一种方式展示它们,解决方案非常简单。每对迭代(0,2,4 ...)你应该打开行和每个奇数迭代(1,3,5 ...)你应该关闭它,所以你有

<tr><td>0</td><td>1</td></tr><tr><td>2</td><td>3</td></tr>...

代码是

<?php
$query = "SELECT * FROM `release` WHERE artist_id = '$rcode' AND label_id = '$id'";
$result = mysql_query($query);
$tr_no = mysql_num_rows($result);
$ii = 0; // Iterator
while ($info = mysql_fetch_array($result)) {
?>  
        <div>
                <table style="width: 100%">
                        <?php if ($ii%2 == 0) { // If pair, we open tr?>
                        <tr>
                        <?php } ?>
                                <td ><img src="../artwork/<?php echo $info['label_id']; ?>/<?php echo $info['ID']; ?>.jpg" width="100" height="100" /></td>
                                <td valign="top">
                                <table style="width: 100%">
                                        <tr>
                                                <td style="width: 45px; height: 20px;" class="style5">
                                                &nbsp;</td>
                                                <td style="width: 180px"><?php echo $info['code']; ?></td>
                                        </tr>
                                        <tr>
                                                <td style="width: 45px; height: 20px;" class="style5">
                                                &nbsp;</td>
                                                <td style="width: 180px"><?php echo $info['name']; ?></td>
                                        </tr>
                                        <tr>
                                                <td style="width: 45px; height: 20px;" class="style5">
                                                &nbsp;</td>
                                                <td style="width: 180px"><?php echo trh($info['date']); ?></td>
                                        </tr>
                                        <tr>
                                                <td style="width: 45px; height: 20px;" class="style5">
                                                &nbsp;</td>
                                                <td style="width: 180px">

                                                </td>
                                        </tr>
                                </table>
                                </td>
                        <?php if ($ii%2 == 1) { // If odd, we close tr?>
                        </tr>
                        <?php } ?>
                </table>
        </div>
<?php $ii++; // We increment the iterator }
?>

答案 1 :(得分:0)

您有多种选择,具体取决于您希望使用HTML / CSS获得多少花哨。您可以尝试将每个宽度保持在50%并向左浮动,元素应自动排列在两列中。

您还可以使用基于表格的布局更明确。在这种情况下,以a和tag开头,然后在循环中添加元素。在每两个元素($ index%2 == 0)之后,写一个a开始一个新行..然后,在你的循环结束后,关闭你的。如果你走这条路线,如果你的查询得到奇数结果,你肯定要确保增加额外费用。

可能有更复杂的方法可以做到这一点,但这很简单,也很简单,可以完成工作。

答案 2 :(得分:0)

除非您有“大”数据集,否则  1)将所有行放入数字索引的数组中  2)将行数除以2:x1 = 0; x2 =(int)count / 2;
 3)从0循环到count / 2,创建一个有两列的表,
 3a)在第一列中由x1 ++索引的那些

答案 3 :(得分:0)

获取图片总数,您可以动态执行此操作,然后将所有内容放在for loop中,如下所示:

假设您想在三列上显示9张图片:

for ($i = 0; $i<=6; $i=($i + 3))
{
    $result = mysql_query("select * from users order by id desc limit $i,3 ");
    while(mysql_fetch_array($result))
    { 
        //bla bla bla html code
    }
}
祝你好运。

答案 4 :(得分:0)

<table border='1'><tr><?phpfor ($i = 0; $i<=17; $i=($i + 3)){echo "<td>$i</td>";}?></tr><tr><?phpfor ($n = 1; $n<=17; $n=($n + 3)){    echo "<td>$n</td>";}?></tr><tr><?phpfor ($m = 2; $m<=17; $m=($m + 3)){echo "<td>$m</td>";}?></tr></table>
  1. | 0 | 3 | 6 | 9 | 12 | 15 |
  2. | 1 | 4 | 7 | 10 | 13 | 16 |
  3. | 2 | 5 | 6 | 11 | 14 | 17 |
  4. 谢谢:)

答案 5 :(得分:0)

<table border='1'><?phpfor ($n = 0; $n<=2; $n++) {echo "<tr>";for ($i = $n; $i<=17; $i=($i + 3)){ echo "<td>$i</td>";}echo "</tr>";}?></table>

只需使用两个这样的循环 结果

0 3 6 9 12 15

1 4 7 10 13 16

2 5 8 11 14 17