Reputation: 611
I am facing a problem and trying to solve this for 4 hours but no solution till now. I have dynamic data which i like to show as a table like below
Header1.................. Header2............... .. HeaderN
data1 of Header1.... data1 of Header2.... data1 of HeaderN
data2 of Header1.... data2 of Header2.... data2 of HeaderN
data3 of Header1.... data3 of Header2.... data3 of HeaderN
.............................................................
dataN of Header1.... dataN of Header2.... dataN of HeaderN
This is my code:
foreach($query_all_field as $secondary_data):
?><th><?php echo $secondary_data;?></th><?php
$sql_data = mysql_query("SELECT `$secondary_data` FROM `$table_name`");
if(mysql_num_rows($sql_data)){
while($result_data = mysql_fetch_object($sql_data)){
?><tr><td><?php echo $result_data->$secondary_data;?></td></tr><?php
}
}
endforeach;
Most important matter $secondary_data
is all header names of a table which is totally dynamic. Then i am trying to find data of each header .
But it is showing now like this:
Header1
data1 of Header1
data2 of Header1
data3 of Header1
Header2
data1 of Header2
data2 of Header2
data3 of Header2
. . .
HeaderN
data1 of HeaderN
data2 of HeaderN
data3 of HeaderN
Upvotes: 0
Views: 4221
Reputation: 2068
Queries are costly, so if possible, you should only be doing them once in situations like this.
To get what I think you want, you can retrieve all the data in one query, use array_keys
on the first row to find out what columns you're working with, and then loop through the data adding a new table row for each database row. For example;
$data = mysql_query("SELECT * FROM `$table_name`");
if (mysql_num_rows($data)) {
echo '<table>';
$headerRow = true;
while ($row = mysql_fetch_assoc($data)) {
if ($headerRow) {
$headerRow = false;
echo '<tr>';
foreach (array_keys($row) as $header) {
echo "<th>$header</th>";
}
echo '</tr>';
}
echo '<tr>';
foreach ($row as $value) {
echo "<td>$value</td>";
}
echo '</tr>';
}
echo '</table>';
}
It should also be noted that the mysql_*
functions are deprecated, and you should look into switching to a different library such as mysqli or PDO.
Upvotes: 0
Reputation: 49095
You're not constructing your table correctly. you're opening a TR
element before closing/using the one that precedes it. try:
echo '<table><tr>';
foreach($query_all_field as $secondary_data):
?>
<td>
<table>
<tr><th><?php echo $secondary_data;?></th></tr>
<?php
$sql_data = mysql_query("SELECT `$secondary_data` FROM `$table_name`");
if(mysql_num_rows($sql_data)){
while($result_data = mysql_fetch_object($sql_data)){
?><tr><td><?php echo $result_data->$secondary_data;?></td></tr><?php
}
}?>
</table>
</td>
<?php
endforeach;
echo '</tr></table>';
Upvotes: 1
Reputation: 91734
You need two loops instead of one.
In the loop first you show the header and you can store the header fields in an array (if necessary...).
The second loop loops through your database rows and has an inner loop to show the different fields in different table cells.
Upvotes: 0