【问题标题】:I want to arrange database elements into tabular form我想将数据库元素排列成表格形式
【发布时间】:2016-01-21 09:15:54
【问题描述】:

我这样创建表:

id | a1 | a2 | a3 |  a | a4 | a5 | a6
 1 |  3 |  1 |  7 | 63 |  3 |  5 |  0
 2 |  3 |  1 |  7 | 35 |  3 |  5 |  0
 3 |  3 |  1 |  7 | 40 |  3 |  5 |  0
 4 |  0 |  0 |  0 | 00 |  0 |  0 |  0
 5 |  1 |  5 |  5 | 44 |  2 |  2 |  2
 6 |  5 |  6 |  9 | 07 |  5 |  5 |  7
 7 |  5 |  6 |  9 | 07 |  5 |  5 |  7
 8 |  1 |  5 |  0 | 64 |  2 |  5 |  7
 9 |  5 |  6 |  7 | 84 |  1 |  3 |  0
10 |  1 |  2 |  4 | 74 |  1 |  3 |  0
11 |  4 |  5 |  0 | 96 |  1 |  2 |  3

我想在我的相关页面上显示这些数据。页面的格式应该是这样的:

<table>
<tr>
<th> a1 </br> a2 </br> a3</th>
<th> a </th>
<th> a4 </br> a5 </br> a6 </th>
.
.
.
.


</tr>
</table>

在数据库中的 7 列之后,或者我们可以说在 id 可被 7 整除之后,它应该添加 &lt;/tr&gt;&lt;tr&gt; 标签

目前我正在使用:

<table width="40%" border="1" align="center" bordercolor="#000000" bgcolor="#99CC99">
<?php

$x=mysql_connect("localhost","dbuser","password");
if(!$x)
{
die("not connected");
}
mysql_select_db("mydb");
$q=mysql_query("select * from table");
while($r=mysql_fetch_array($q))
{?>
<td> <?php echo $r["a1"];?></br>

<?php echo $r["a2"];?></br>

<?php echo $r["a3"];?></td>

<td><?php echo $r["a"];?></td>

<td><?php echo $r["a4"];?></br>

<?php echo $r["a5"];?></br>

<?php echo $r["a6"];?></td>  


<?php
}
?>
</table>

但它不会在 7 列元素后中断行

【问题讨论】:

  • 结果很酷,不过...

标签: php html mysql database html-table


【解决方案1】:

我想你正在尝试这个

<table width="40%" border="1" align="center" bordercolor="#000000" bgcolor="#99CC99"><tr>
<?php
$inr=0;
$x=mysql_connect("localhost","dbuser","password");
if(!$x)
{
die("not connected");
}
mysql_select_db("mydb");
$q=mysql_query("select * from table");
while($r=mysql_fetch_array($q))
{?>
<td> <?php echo $r["a1"];?></br>

<?php echo $r["a2"];?></br>

<?php echo $r["a3"];?></td>

<td><?php echo $r["a"];?></td>

<td><?php echo $r["a4"];?></br>

<?php echo $r["a5"];?></br>

<?php echo $r["a6"];?></td>  


    <?php
$inr++;
if($inr%7==0)
echo"</tr><tr>";
    }
    ?>
   </tr></table>

【讨论】:

    【解决方案2】:
            $x=mysql_connect("localhost","dbuser","password");
            if(!$x)
            {
                die("not connected");
            }
            mysql_select_db("mydb");
            $q=mysql_query("select * from table");
            $markup = '<table width="40%" border="1" align="center"         while($r=mysql_fetch_array($q)) {   
            $headersPrinted = false;
    bordercolor="#000000" bgcolor="#99CC99"><tr>
        ';
                while ($r = mysql_fetch_array($q)) {
                    if (!$headersPrinted) {
                        foreach ($q as $key => $value) {
                            $markup .= "<th>$key</th>";
                        }
                        $markup .= "<tr>$markup</tr>";
                        $headersPrinted = true;
                    }
                    $markup .= '<tr>';
                    foreach ($q as $key => $value) {
                        $markup .= "<td>$value</td>";
                    }
                    $markup = $markup. '</tr>';
                }
                $markup .= '</table>';
    
            echo $markup;
    

    在检查是否有数据库连接之前,您应该中止任何 HTML 代码的打印。

    【讨论】:

      猜你喜欢
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-15
      • 2020-07-18
      • 1970-01-01
      相关资源
      最近更新 更多