本文介绍了在2列中显示单个PHP数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将单个数据显示为2列,因为我的PHP文件中的mysql表结果.我已经尝试过使用我的代码,但是结果显示了相同的数据,例如<tr><td>result1</td><td>result1</td>.

I would like to display a single data to 2 columns as mysql table result in my PHP file. I have tried with my code but the same data has been showed as result like <tr><td>result1</td><td>result1</td>.

我尝试使用我的代码,但结果如下:

I have tried with my code but with the following result:

| result 1 | result 1 |
| result 2 | result 2 |
| result 3 | result 3 |
| result 4 | result 4 |
| result 5 | result 5 |
| result 6 | result 6 |
| result ... | result ... |

但是我需要结果

| result 1 | result 2 |
| result 3 | result 4 |
| result 5 | result 6 |
| result 7 | result 8 |
| result ... | result ... |

这是我的代码:

    <?php
        include('config.php');
        $data_content = '';
        $qry = "SELECT DISTINCT bankName FROM bankData ORDER BY bankName";
        $result = mysql_query($qry);
        while($row = mysql_fetch_array($result))
            {
                 $data_content.= "<a href='bank/".$row['bank_Name'].".php'> ".$row['bankName']."</a>";
            }
        mysql_close();
        ?>
<!DOCTYPE html>
<html lang="en">
<head>
<body>
<div>   
<table border="1">
    <?php
         for($i=0; $i<=1; $i++)
            {
            echo "<tr>";
            for($j=0; $j<=1; $j++)
            {
             echo "<td>";
             echo $data_content;
             echo "</td>"; 
            }
             echo "</tr>";
            }
    ?>
</table>
</div>
</body>
</html>

请帮助我.

推荐答案

将循环放在下面:

<table border="1">
<?php
$i = 0;
while($row = mysql_fetch_array($result))
    {
         // Odd row opens
         if (++$i % 2 != 0) echo "<tr>";
         echo "<td><a href='bank/".$row['bankName'].".php'> ".$row['bankName']."</a></td>";
         // Even row closes
         if ($i % 2 == 0) echo "</tr>";  
    }
    // If you have an odd number of results, add a blank column and close the last row
    if ($i % 2 != 0)  echo "<td></td></tr>";
?>
</table>

(++$i % 2 != 0)递增$ i并检查它是否为奇数.如果为奇数,它将使用</tr>打开表行,而下一次迭代将使用</tr>关闭表行.

(++$i % 2 != 0) increments $i and checks if it is odd. If it is odd, it will open the table row with </tr> and the next iteration will close a table row with </tr>.

这篇关于在2列中显示单个PHP数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 23:15