【问题标题】:how to display data in two column with php如何用php在两列中显示数据
【发布时间】:2017-09-14 17:09:11
【问题描述】:

如何在两列中显示数据?

 <?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
        }
        ?>

通常我只将它用于一列。

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

【问题讨论】:

    标签: php mysql html


    【解决方案1】:

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

    如果我们假设您的数据库中有以下六个艺术家 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,3,5(第 1 列)和 2,4,6(第 2 列)
    • 成功了!非常感谢,我怎样才能使它成为 3 或 4 列?
    • 第二种方法怎么做?请解释
    【解决方案2】:

    您有多种选择,具体取决于您对 HTML/CSS 的喜爱程度。您可以尝试将每个元素保持 50% 的宽度并将它们向左浮动,元素应自动排列成两列。

    您还可以更明确地使用基于表格的布局。在这种情况下,从一个 and 标记开始,然后在循环中添加元素。在每两个元素( $index % 2 == 0 )之后,写 a 来开始一个新行。然后,在你的循环完成后,关闭你的 .如果您走这条路,如果您的查询结果为奇数,则需要确保添加一个额外的。

    可能有更复杂的方法可以做到这一点,但这足够简单直接地完成工作。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        获取图片的总数,你可以动态做,然后把所有的都放在一个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
            }
        }
        

        祝你好运。

        【讨论】:

          【解决方案5】:
          <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|8|11|14|17|

          谢谢你:)

          【讨论】:

            【解决方案6】:
            <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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-10-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多