使用PHP表在行中显示列标题值

时间:2014-10-25 01:05:39

标签: php html mysql arrays

我有这张桌子:

enter image description here

前段时间我正在寻找方法,可以使用MySQL数据中的PHP表显示行中的列,我发现了它!这里有人帮助我。

但现在我看到一个错误,错误在一些行,因为我需要显示&#34; prov_name&#34; <th>中的数据库值不是表中的字段名称,我的意思是Fiddle

现在我看到错误在这一行:

<th colspan="2"><?php echo "prov_name $x"; ?></th>

我知道这是错误的,因为应该说$ prov_name或带变量的东西因为prov_name总是不同。

这是代码(除了之外一切正常):

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

1 个答案:

答案 0 :(得分:1)

您已经关闭,只需直接打印:

<!-- PROV NAMES -->
<tr>
<?php foreach($keys as $prov_name): ?>
    <th><?php echo $prov_name; ?></th>
<?php endforeach; ?>
</tr>