简体   繁体   English

试图用php将xml数据回显到html表

[英]trying to echo xml data to html table with php

So I have almost the output that I want except for one thing. 因此,除了一件事之外,我几乎拥有了所需的输出。 I can not seem to figure out how to print a specific part of my XML file on new lines inside of an HTML table using PHP to echo it out. 我似乎无法弄清楚如何使用PHP将HTML文件的特定部分打印在HTML表的新行中,以将其回显。

I am not the greatest when it comes to PHP, but I find it fun to dabble with. 在PHP方面,我并不是最出色的人,但是我发现涉足它很有趣。 As for my code that I have so far, I will post my PHP section. 至于到目前为止的代码,我将发布我的PHP部分。

<?php
$file="badscifimovies.xml";
$xml = simplexml_load_file($file) or die ("no load");
echo <<<EOF
<table>
  <tr>
    <th>Title</th>
    <th>Year</th>
    <th>Producers/Director</th>
    <th>Stars</th>
    <th>Budget</th>
    <th>MST3000</th>
  </tr>
EOF;
foreach($xml->movie as $movie){
  echo "<tr>";
  echo "<td>{$movie->title}</td>";
  echo "<td>{$movie->year}</td>";
  echo "<td>{$movie->director->producer}</td>";
  foreach($movie->stars->star as $stars){
    echo "<td>{$stars[0]}</td>";
  }
  echo "<td>{$movie->budget}</td>";
  echo "<td>{$movie->mst}</td>";
  echo "<br />";
  echo "</tr>";
}
echo '</table>';
?>

The output that I am looking for is with the "Stars" section. 我要查找的输出是“星星”部分。 the part of the XML that I am getting at is this: 我得到的XML部分是这样的:

<stars>
  <star>Name One</star>
  <star>Name two</star>
  <star>Name three</star>
</star>

I can not seem to print each one of those on a separate line. 我似乎无法将每一个打印在单独的行上。 All of my other code print out the rest of the XML document fine (probable because there is 1 element per movie). 我所有其他代码都很好地打印了XML文档的其余部分(可能是因为每个电影只有1个元素)。 For the stars section it prints all three stars out side by side, but will print each of my movie sections out on a new line like it should. 对于“星星”部分,它会并排打印所有三颗星星,但是会像需要的那样在新行中打印我的每个电影部分。

Any help would be appreciated. 任何帮助,将不胜感激。 If you need more info from me I will post more up. 如果您需要我的更多信息,我将发布更多信息。

foreach($xml->movie as $movie){
  echo "<tr>";
  echo "<td>{$movie->title}</td>";
  echo "<td>{$movie->year}</td>";
  echo "<td>{$movie->director->producer}</td>";
  echo "<td>";
  foreach($movie->stars->star as $stars){
    echo $stars[0] . '<br/>';
  }
  echo "</td>";
  echo "<td>{$movie->budget}</td>";
  echo "<td>{$movie->mst}</td>";
  echo "<br />";
  echo "</tr>";
}

I think this should work. 我认为这应该有效。 You were creating a new <td> element for every star, but you only had one <th> tag corresponding. 您正在为每个星星创建一个新的<td>元素,但是您只有一个<th>标记对应。

XML file (employee.xml) XML文件(employee.xml)

<root>
<person>
    <sno>1</sno>
    <name>person1</name>
    <phno>098687667</phno>
</person>
<person>
    <sno>2</sno>
    <name>person2</name>
    <phno>897876756</phno>
</person>
<person>
    <sno>3</sno>
    <name>person3</name>
    <phno>986875675</phno>
</person>

PHP code PHP代码

<body>
<?php
$xml = simplexml_load_file("employee.xml")
or die("Error: Cannot create object");

$element_name = array();                                    //get all xml element name
foreach ($xml->children()->children() as $value) {
$element_name[] = $value->getName();
}
?>
<table border = "1">
<thead>
<tr>
<?php
foreach ($element_name as $value) {             //display table head
echo "<th>{$value}</th>";
}
?>
</tr>
</thead>

<tbody>
<?php
foreach ($xml->children() as $value) {              //display table data
echo "<tr>";    
for ($i = 0; $i < count($element_name); $i++) {
echo "<td>{$value->children()->$element_name[$i]}</td>";
}
echo "</tr>";
}
?>
</tbody>
</table>
</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM