2016-06-14 117 views
0

你好,我一直在试图做一个论坛,我做到了这一点,但问题是,整个页面重新加载,这意味着如果你对页面有太多评论,它总是会重定向你到顶部,你必须向下滚动到最后一个评论,所以我学习了Ajax来解决这个问题,但是我正视这个问题,说:“第28列第323行的错误:实体''未定义'请问是什么问题这里解释的是生成XML的PHP​​如何使用php生成xml文件

<?php 
header('Content-Type: text/xml'); 
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'; 
?> 

<?php 
require 'connect.inc.php'; 
$sql= $db->query("SELECT * FROM comments"); 
$results = $sql->fetchAll(PDO::FETCH_ASSOC); 


echo '<comments>'."\n"; 
foreach($results as $result) 
{ 
    echo '<comment>'."\n"; 
     echo $result['post']."\n"; 
    echo '</comment>'."\n"; 
} 
echo '</comments>'; 

?> 
+0

文本注释包含符号(& )这在xml中是不可用的 – splash58

+1

您应该能够用CDATA包装XML中不允许的字符。 – Tom

+0

非常感谢这是问题 –

回答

0

它似乎相当于您的$结果。 我用假$结果尝试你的代码,它工作得很好。 顺便说一句,假$结果是: $ results = array(array('id'=> 1,'post'=>'a'),array('id'=> 2,'post'=>'b “));

演示代码:

<?php 
    header('Content-Type: text/xml'); 
    echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'; 
?> 

<?php 
    #require 'connect.inc.php'; 
    #$sql= $db->query("SELECT * FROM comments"); 
    #$results = $sql->fetchAll(PDO::FETCH_ASSOC); 
    $results = array(array('id' => 1, 'post' => 'a'), array('id' =>2 , 'post' => 'b')); 


    echo '<comments>'."\n"; 
    foreach($results as $result) 
    { 
     echo '<comment>'."\n"; 
     echo $result['post']."\n"; 
     echo '</comment>'."\n"; 
    } 
    echo '</comments>'; 

    ?> 
0

在PHP中可以数组转换为XML数据

$test_array = array (
    'bla' => 'blub', 
    'foo' => 'bar', 
    'another_array' => array (
      'stack' => 'overflow', 
    ), 
); 
$xml = new SimpleXMLElement('<root/>'); 
array_walk_recursive($test_array, array ($xml, 'addChild')); 
print $xml->asXML(); 

O/P: -

<?xml version="1.0"?> 
<root> 
    <blub>bla</blub> 
    <bar>foo</bar> 
    <overflow>stack</overflow> 
</root> 
+0

谢谢我会牢记这一点 –