Iam newbie in php programing, i have some problames with showing data from mysql database using php and html. this is my tables :
location :id_location
location
component :id_comopnent
id_location
comonen
sub_component :id_sub_component
id_comopnent
sub_component
How can i get following output:
location
|-----------|-------------|
| component |sub component|
|-----------|-------------|
| | Data |
| Data |-------------|
| | Data |
|-----------|-------------|
| | Data |
| |-------------|
| Data | Data |
| |-------------|
| | Data |
|-----------|-------------|
location
|-----------|-------------|
| component |sub component|
|-----------|-------------|
| | Data |
| Data |-------------|
| | Data |
|-----------|-------------|
| | Data |
| Data |-------------|
| | Data |
|-----------|-------------|
Location (according to the data)
this is my code
<?php
$dsn = "mysql:host=localhost;dbname=kampus_hijau";
$dbc = new PDO($dsn, 'root', '');
$sql1 = "SELECT * FROM Lokasi ORDER BY id_location";
$stmt1 = $dbc->prepare($sql1);
$stmt1->execute();
while ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)) {
$location++;
echo "Location $location : ".$row1['location'];
?>
<table width="469" border="1">
<tr bgcolor="#00FFFF">
<th width="109" class="rounded" scope="col">Component</th>
<th width="248" class="rounded" scope="col">Sub Component</th>
<th>Nilai</th>
</tr>
<?php
$query = "SELECT * FROM sub_component,component where sub_component.id_component=component.id_component and component.id_location='$data[id_location]' order by component.id_location";
$stmt = $dbc->prepare($query);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[$row['component']][] = $row['sub_component'];
}
foreach($result as $id => $invoices) {
echo '<td rowspan='. count($invoices) . '>' . $id . '</td>';
$count = 0;
foreach ($invoices as $invoice) {
if ($count != 0) {
echo '<tr>';
}
echo "<td>$invoice</td>";
echo "</tr>";
$count++;
}
}
?>
</table>
<?php
}
?>
and this the output that i get :

with that code, the output form is appropriate, but the data does not appear appropriate, it's always show the data previous in the next table (red box is the data that is repeated ). how can i fix it ?
mysql_*and PDO in the same file? You can completely switch to PDO.