使用xml数据在php中创建表

时间:2012-09-23 18:54:11

标签: php html xml

我需要在表格中创建结果表。

<?php
$string = '<?xml version="1.0" encoding="utf-8"?>
<books>
<book isbn="978-1594489501">
<title>book Title 1</title>
<author>Author 1</author>
<publisher>publisher 1</publisher>
<price>price 1</price>
<genre>English 1</genre>
</book>
<book isbn="978-1594489502">
<title>book Title 2</title>
<author>Author 2</author>
<publisher>publisher 2</publisher>
<price>price 2</price>
<genre>English 2</genre>
</book>
</books>';
$xml = new SimpleXMLElement($string);
$string = 'book';
$result = $xml->xpath("//book[contains(translate(title,
'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), '".strtoupper($string)."')]"); 
foreach($result as $one){
echo "<table border='1' width='500'>\n";
echo "<tr><th>Title</th><td>$one->title</td></tr>\n";
echo "<tr><th>Author</th><td>$one->author</td></tr>\n";
echo "<tr><th>Publisher</th><td>$one->publisher</td></tr>\n";
echo "<tr><th>Price</th><td>$one->price</td></tr>\n";
echo "<tr><th>Genre</th><td>$one->genre</td></tr>\n";
echo "</table>\n";  
}
?>

在HTML CODE中就像这样             

Result 1 Title: Title 1 Author: Author 1 Publish: publish 1 Price: price 1 Genre: English 1 Result 2 Title: Title 2 Author: Author 2 Publish: publish 2 Price: price 2 Genre: English 2 Result 3 Title: Title 3 Author: Author 3 Publish: publish 3 Price: price 3 Genre: English 3 Result 4 Title: Title 4 Author: Author 4 Publish: publish 4 Price: price 4 Genre: English 4 Result 5 Title: Title 5 Author: Author 5 Publish: publish 5 Price: price 5 Genre: English 5 Result 6 Title: Title 6 Author: Author 6 Publish: publish 6 Price: price 6 Genre: English 6

3 个答案:

答案 0 :(得分:0)

我总是使用xml2array进行一些简单的XML到PHP数组转换。

http://www.bin-co.com/php/scripts/xml2array/

它有很多选项,而且非常易于使用。我不知道它对其他图书馆有多高效,但它总是为我做小项目的工作。

答案 1 :(得分:0)

这样的东西?

    echo "<table border='1' width='500'>\n";

    foreach($result as $one){

        echo "
        <tr>
            <th>Title</th>
            <th>Author</th>
            <th>Publisher</th>
            <th>Price</th>
            <th>Genre</th>
        </tr>";

        echo "<tr>";
        echo "<td>$one->title</td>";
        echo "<td>$one->author</td>";
        echo "<td>$one->publisher</td>";
        echo "<td>$one->price</td>";
        echo "<td>$one->genre</td>";
        echo "</tr>";

    }

    echo "</table>\n";

答案 2 :(得分:0)

您必须使用以下内容:

echo '<table border="1" bordercolor="FFCC00" style="background-color:FFFFCC" width="600" cellpadding="3" cellspacing="3">';
echo '<tr>';
$i = 1;
foreach($result as $one){
    echo '<td>';
    echo '<table>';
    echo '<tr><th>Result </th><td>'.$i.'</td></tr>';
    echo '<tr><th>Title: </th><td>'.$i.'</td></tr>';
    echo '<tr><th>Author: </th><td>'.$i.'</td></tr>';
    echo '<tr><th>Publish: </th><td>'.$i.'</td></tr>';
    echo '<tr><th>Price: </th><td>'.$i.'</td></tr>';
    echo '<tr><th>Genre: </th><td>'.$i.'</td></tr>';
    echo '</table>';
    echo '</td>';
    if ($i%3==0) echo '</tr><tr>';
}
echo '</table>';