使用PHP打印出水平而不是垂直的表格

时间:2012-06-12 17:07:43

标签: php mysql html-table

问题:

我有一张打印出垂直的桌子,但我希望它可以打印水平。谁可以就如何实现这一目标提供指导?

PHP代码:

echo '
    <table class="table table-condensed table-bordered neutralize">     
        <tbody>
            <tr>
                <td><b>Kriterium</td>
                <td><b>Betyg</td>
            </tr>
';

while ($row = mysql_fetch_assoc($result))
{
    echo '
        <tr>
            <td>'.$i.'</td>
            <td>'.$row['RID'].'</td>
        </tr>
    ';

    $i++;
}

echo '
        </tbody>
    </table>
';

当前输出:

enter image description here

期望的输出:

enter image description here

2 个答案:

答案 0 :(得分:17)

循环查询结果,首先构建所需的两行,然后将它们添加到表中:

$kriterium = '';
$betyg = '';

while ($row = mysql_fetch_assoc($result))
{
    $kriterium .= '<td>'.$i.'</td>';
    $betyg .= '<td>'.$row['RID'].'</td>';
    $i++;
}

echo '
    <table class="table table-condensed table-bordered neutralize">     
        <tbody>
            <tr>
                <td><b>Kriterium</td>'.$kriterium .'
            </tr>
            <tr>
                <td><b>Betyg</td>'.$betyg .'
            </tr>
        </tbody>
    </table>
';

答案 1 :(得分:1)

您可以在二维数组中收集数据,然后重复使用此数组以不同格式构建输出:

$rows  = array();
$index = 0;

while ($row = mysql_fetch_assoc($result))
    $rows[0][] = ++$index;
    $rows[1][] = $row['RID'];
}

$table = '<table class="table table-condensed table-bordered neutralize">     
  <tbody>
    <tr><td><b>Kriterium</b></td><td>%s</td></tr>
    <tr><td><b>Betyg</b></td><td>%s</td></tr>
  </tbody>
</table>';

printf(
  $table, 
  implode('</td><td>', $rows[0]),
  implode('</td><td>', $rows[1])
);
相关问题