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

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

通常我只将它用于一列。

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

最佳答案

答案还取决于您想要显示信息的方式。

如果我们假设您的数据库中有以下六个 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 }
?>

关于php - 如何用php在两列中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1653476/

相关文章:

php - 无法允许元素(img : 'src' ) with symfony html-sanitizer (symfony6. 3 php 8.2)

php - Swagger注释和OpenAPI v3.0中 "Produces"的注释是什么?

mysql - MySQL LIKE 查询中大小写的使用

javascript - Windows 8 应用栏无法在 Google map 上运行

html - 使用 Flexbox 和 Overflow 使元素居中

php - MySQLi multi_query 安全预防措施

javascript - 在 PHP 中的所有页面上包含 header

mysql - sql 使用 LIKE vs locate() 搜索子字符串

mysql - 数组数据类型插入MySQL

html - 如何基于移动设备的用户代理替换超链接