0

I'm fairly new to PHP and was wondering how I could output this information into one table.

<?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>";
}

?>

This is the output...

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

The html output is as follows...

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

I just want it so it is in one table with the headers only on the top.

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

Any suggestions on how to get this information all into one table or best practices would be great.

3
  • 1
    Who downvoted this question? It had sufficient information, and shows an attempt at coding (just poor understanding of what it was doing). Commented Mar 7, 2014 at 3:40
  • Always understand, that if you place table inside the for loop the number of elements present would be the number of table's formed coz of iteration. Hence you should print table tag & its table head i.e. first tr tag before for loop starts and close the table tag after for loop end, and within the for loop you should only keep that code which is meant to iterate and print each row Commented Mar 7, 2014 at 3:43
  • Thank you very much for the clarification on this. I knew I was close but skipped right passed where to place my headers. Thank you Commented Mar 7, 2014 at 3:54

3 Answers 3

2

Just print the <table boarder=1> and the headers before the foreach loop and then print out the closing table tag after your finished. Also encapsulate the $content->children() in a table row tag for good measure.

<?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>";

?>
Sign up to request clarification or add additional context in comments.

4 Comments

You beat me to it. Good to see you picked up on his missing <tr>
Awesome! Thank you so much for the fast response.
User, please remember to hit the check mark next to the answer that you used. Always do that so that person can get credit
Thanks Brad! I was trying to vote on it but don't have enough points. Just updated. Thanks again for all your help.
0

Put your

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>";

before your foreach() condition, not inside

Comments

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>";

    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.