dtrhd2850 2014-10-25 01:05 采纳率: 0%
浏览 89
已采纳

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

I have this table:

enter image description here

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>
  • 写回答

1条回答 默认 最新

  • dongmei1988 2014-10-25 03:26
    关注

    You're already close, just print them directly:

    <!-- PROV NAMES -->
    <tr>
    <?php foreach($keys as $prov_name): ?>
        <th><?php echo $prov_name; ?></th>
    <?php endforeach; ?>
    </tr>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器