我想将数据库元素排列成表格形式


I want to arrange database elements into tabular form

我创建这样的表:

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 整除之后,它应该添加</tr><tr>标签

目前我正在使用:

<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 列元素后换行

我想你正在尝试这个

<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>
        $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 代码的打印。