PHP输出多个HTML表

时间:2014-03-07 03:35:09

标签: php html html-table echo

我是PHP的新手,想知道如何将这些信息输出到一个表中。

<?php

$url='mysite.com';
$content = simplexml_load_file($url);

foreach ($content->children() as $alarm) {

$severity = $alarm['severity'];
$id = $alarm ['id'];


echo "<table boarder=1>";
echo "<tr><th>Header1</th><th>ID</th><th>Header2</th><th>Header3</th><th>Header4</th>      <th>Header5</th></tr>";

echo "<td style=\"text-align:center\" width=100px >";
echo $severity;
echo "</td><td style=\"text-align:center\" width=100px >";
echo $id;

echo "</td><td style=\"text-align:center\" width=100px >";
echo $alarm->nodeId;
echo "</td><td style=\"text-align:center\" width=100px >";
echo $alarm->nodeLabel;

echo "</td><td style=\"text-align:center\" width=100px >";

echo $alarm->lastEventTime;
echo "</td><td style=\"text-align:center\" width=500px >";
echo  $alarm->logMessage;
echo "</td>";
echo "</table>";
}

?>

这是输出......

Header1     Header2      Header3            Header4                Header5**
$severity     $id       $alarm->nodeID    $alarm->$nodeLabel    $$alarm->logmessage

Header1     Header2      Header3            Header4                Header5
$severity     $id       $alarm->nodeID    $alarm->$nodeLabel    $$alarm->logmessage

Header1     Header2      Header3            Header4                Header5
$severity     $id       $alarm->nodeID    $alarm->$nodeLabel    $$alarm->logmessage

html输出如下......

<table boarder="1">...</table>
<table boarder="1">...</table>
<table boarder="1">...</table>
<table boarder="1">...</table>

我只是想要它所以它在一个表中,标题只在顶部。

Header1     Header2      Header3            Header4                Header5**
$severity[0]     $id       $alarm->nodeID    $alarm->$nodeLabel    $$alarm->logmessage
$severity[1]     $id       $alarm->nodeID    $alarm->$nodeLabel    $$alarm->logmessage
$severity[2]     $id       $alarm->nodeID    $alarm->$nodeLabel    $$alarm->logmessage

关于如何将这些信息全部放入一个表或最佳实践的任何建议都会很棒。

3 个答案:

答案 0 :(得分:2)

只需在<table boarder=1>循环之前打印foreach和标题,然后在完成后打印出结束表标记。同时将$content->children()封装在表格行标记中以便进行测量。

<?php

echo "<table boarder=1>";
echo "<tr><th>Header1</th><th>ID</th><th>Header2</th><th>Header3</th><th>Header4</th>      <th>Header5</th></tr>";

foreach ($content->children() as $alarm) {
    $severity = $alarm['severity'];
    $id = $alarm ['id'];

    echo "<tr><td style=\"text-align:center\" width=100px >";
    echo $severity;
    echo "</td><td style=\"text-align:center\" width=100px >";
    echo $id;

    echo "</td><td style=\"text-align:center\" width=100px >";
    echo $alarm->nodeId;
    echo "</td><td style=\"text-align:center\" width=100px >";
    echo $alarm->nodeLabel;

    echo "</td><td style=\"text-align:center\" width=100px >";

    echo $alarm->lastEventTime;
    echo "</td><td style=\"text-align:center\" width=500px >";
    echo  $alarm->logMessage;
    echo "</td></tr>";
}

echo "</table>";

?>

答案 1 :(得分:0)

点亮你的

echo "<table boarder=1>";
echo "<tr><th>Header1</th><th>ID</th><th>Header2</th><th>Header3</th><th>Header4</th>      <th>Header5</th></tr>";

foreach()条件之前,而不是在

之内

答案 2 :(得分:0)

<?php
    echo "<table boarder=1>";
    echo "<tr><th>Header1</th><th>ID</th><th>Header2</th><th>Header3</th><th>Header4</th>      <th>Header5</th></tr>";

    foreach ($content->children() as $alarm) {

        echo "<tr><td style=\"text-align:center\" width=100px >".$alarm->severity."</td><td style=\"text-align:center\" width=100px >".$alarm->id."</td><td style=\"text-align:center\" width=100px >".$alarm->nodeId."</td><td style=\"text-align:center\" width=100px >".$alarm->nodeLabel."</td><td style=\"text-align:center\" width=100px >".$alarm->lastEventTime."</td><td style=\"text-align:center\" width=500px >".$alarm->logMessage."</td></tr>";
    }

    echo "</table>";

    ?>