I have this table:
And time ago I was looking the way to make it possible display columns in rows with PHP table from MySQL data and I found it! Someone here help me with that.
But now I see a mistake, error in some line because I need to show the "prov_name" values from DB in the <th>
not the field name from the table , I mean like this Fiddle.
Now I see that the error is in this line:
<th colspan="2"><?php echo "prov_name $x"; ?></th>
I know that is wrong because should be say $prov_name or something with variable because the prov_name always is different.
This is the code (all works fine except that):
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$data = array();
$query="SELECT * from provprices WHERE CA_id= ".$CA_id;
$results = mysql_query($query);
$db->setQuery("SELECT * from provprices");
while($row = mysql_fetch_array($results)) {
$data[$row['prov_name']][] = $row;
}
$keys = array_keys($data);
$size = count($keys);
$vals = array();
// grouping:
// if there are six (cam1 to cam6)
// then group them by cam1, ... to cam6, then repeat until theres no more left
while(count($data) > 0) {
foreach($keys as $key) {
if(!empty($data[$key])) {
$vals[] = array_shift($data[$key]);
} else {
unset($data[$key]); // remove them if empty
}
}
}
$vals = array_chunk($vals, $size); // split them by how many prov_names
?>
<div class="datagrid" id='Three'>
<table class="prices" >
<!-- PROV NAMES -->
<tr><?php for($x = 1; $x <= $size; $x++): ?>
////////////////////////////////////// THIS LINE
<th colspan="2"><?php echo "prov_name $x"; ?></th>
////////////////////////////////////// THIS LINE
<?php endfor; ?></tr>
<!-- unitval totvals -->
<tr><?php for($x = 1; $x <= $size; $x++): ?>
<td style="background-color: #00CDB9; border-bottom: 4px solid #006f7d; color: white; font-weight: bold; border-right: 4px solid #006f7d;">Valor Unitario
</td>
<td style="background-color: #00CDB9; border-bottom: 4px solid #006f7d; color: white; font-weight: bold; ">
Valor Total
</td>
<?php endfor; ?>
</tr>
<!-- the grouped values -->
<?php foreach($vals as $val): ?>
<tr>
<?php foreach($val as $v): ?>
<td><?php echo $v['unitval']; ?></td>
<td><?php echo $v['totval']; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>