基于转置的Joomla数据创建html表

时间:2016-11-18 19:56:01

标签: php joomla html-table transpose

我正在努力创建一个由joomla数据库组成的表。目前我正在使用以下代码:

$query= "SELECT DATUM, A , B ,C, D, E, F, G FROM ".$db->quoteName('#__XYZTABLE'). " WHERE DATUM_ENG>= CURDATE()";
$db->setQuery($query);
$results = $db -> loadObjectList();
    echo "<table><tr><th>Name</th><th>Department</th></tr>";

foreach($results as $row){

echo "<tr>"; echo "<td>".$row->last_name."</td>";

echo "<td>".$row->dept."</td>";

echo "</tr>"; }

echo "</table>";

基本上,这段代码运行正常。结果是:

DATUM , A , B ,C, D, E, F, G 
xxxx1 ,aa1,bb1,cc1,dd1,ee1,ff1,gg1
xxxx2 ,aa2,bb2,cc2,dd2,ee2,ff2,gg2
....

但是,我想创建一个html表来转置数据并将其显示为以下形式:

    xxxx1 , xxxx2, xxxx3 ....
A   aa1   , aa2  , aa3
B   bb1   , bb2  ,bb3
C   cc1   , cc2  ,cc3
D   .....

在这个论坛和谷歌搜索后,我找不到类似的东西。有人可以用特定代码帮我吗?

1 个答案:

答案 0 :(得分:1)

使用一些php数组函数,你可以这样:     

$object = new stdClass();
// create some test values
$object->DATUM = '2016120';
$object->A = 'aa';
$object->B = 'bb';
$object->C = 'cc';
$object->D = 'dd';
$object->E = 'ee';
$object->F = 'ff';
$object->G = 'gg';

for ($i = 1; $i < 10; $i++){
    $vars = get_object_vars($object);
    $obj = new stdClass;
    foreach ($vars as $key=>$var){
        $obj->$key = $var.$i;
    }
    $results[] = $obj;
}
// end of creating test values

echo "<table><tr><th>DATUM</th><th>A</th><th>B</th><th>C</th><th>D</th></tr>";

foreach ( $results as $row ) {
    echo "<tr>";
    echo "<td>" . $row->DATUM . "</td>";
    echo "<td>" . $row->A . "</td>";
    echo "<td>" . $row->B . "</td>";
    echo "<td>" . $row->C . "</td>";
    echo "<td>" . $row->D . "</td>";
    echo "<td>" . $row->E . "</td>";
    echo "<td>" . $row->F . "</td>";
    echo "<td>" . $row->G . "</td>";
    echo "</tr>";
}
echo "</table>";

echo '<h3>New Variant</h3>';
echo '<table>';
$elements = count($results);
foreach (get_object_vars($results[0]) as $key=>$var){
    echo '<tr>';
    $tag = ($key === 'DATUM') ? 'th>' : 'td>';
    echo '<'.$tag . $key .'</'.$tag;
    for ($i = 0; $i < $elements; $i++){
        echo '<'.$tag . $results[$i]->$key.'</'.$tag;
    }
    echo '</tr>';
}
echo '</table>';`