Display Name Display Name - 2 months ago 14
MySQL Question

I want to arrange database elements into tabular form

I create table like this

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


I want to show this data on my relevant page. the format for the page should be like this

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


</tr>
</table>


and after 7 columns from database or we can say after id divisible by 7, it should add
</tr><tr>
tag

Currently I am using

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


but it does not break row after 7 columns element

Answer

I think you are trying this

<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>
Comments