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 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding