如何建立以下格式的数组?

时间:2019-08-13 12:30:22

标签: php mysql arrays

我想构建一个数组以XML格式显示数据。 我正在从数据库中获取两个不同帖子的记录,并希望以以下格式构建结构。

<hc>
<post>
    <title>Post 1</title>
    <url>test.com/post/1</url>
    <xid>test.com/?p=1</xid>
    <stream_id></stream_id>
    <comments>
        <comment>
            <id>2345</id>
            <parent_id/><root_id/>
            <text>Comment text</text>
            <nick>Ivan Ivanov</nick>
            <time>Fri, 07 Jun 2013 13:52:31 GMT</time>
            <ip>218.117.64.236</ip>
            <email/>
            <account_id>1</account_id>
            </files>
            <vote_up>0</vote_up>
            <vote_dn>3</vote_dn>
            <topic>false</topic>
            <param/>
            <hc_comment>false</hc_comment>
            <avatar>http://test.com/images/user_1/photo.jpg</avatar>
            <category>2</category>
        </comment>
        <comment>
            <id>2345</id>
            <parent_id>1370613151388748</parent_id>
            <root_id>1370613151388748</root_id>
            <text>Comment text</text>
            <nick>Ivan Ivanov ert</nick>
            <time>Fri, 07 Jun 2013 13:52:40 GMT</time>
            <ip>223.117.64.236</ip>
            <email/>
            <account_id>1</account_id>
            <files/>
            <vote_up>0</vote_up>
            <vote_dn>0</vote_dn>
            <topic>false</topic>
            <param/>
            <hc_comment>false</hc_comment>
            <avatar/>
            <category/>
        </comment>
    </comments>
</post>
<post>
    <title>Post 2</title>
    <url>test.com/post_2</url>
    <xid>test.com/?p=1</xid>
    <stream_id>51b1e59177e3146f63000003</stream_id>
    <comments>
        <comment>
            <id>34567</id>
            <parent_id>1370613240522627</parent_id>
            <root_id>1370613240522627</root_id>
            <text>Hypercomment</text>
            <nick>Ivan Ivanov</nick>
            <time>Fri, 07 Jun 2013 13:54:00 GMT</time>
            <ip/>
            <email/>
            <account_id>1</account_id>
            <files/>
            <vote_up>0</vote_up>
            <vote_dn>0</vote_dn>
            <topic>false</topic>
            <param/>
            <hc_comment>true</hc_comment>
            <avatar/>
            <category/>
        </comment>
    </comments>
</post>
</hc>

我已经按照以下方式完成了代码...

$query = "SELECT * FROM `comments` WHERE `content_id` IN ('43268','401509')  ORDER BY `id` ASC ";
$result = mysqli_query($conn, $query);
$posts = array();
if (mysqli_num_rows($result) > 0) {
  while($post = mysqli_fetch_assoc($result)) {
  }
}

基本上有两个帖子带有注释及其子注释...我想以xml格式映射数据库记录...

<hc>
  <post 1>
    <comments>
      <comment>
       .....
       .....
      </comment>
      <comment>
       .....
       .....
      </comment>
    </comments>
  </post 1>
  <post 2>
    <comments>
      <comment>
       .....
       .....
      </comment>
    </comments>
  </post 2>
</hc>```

How is it possible to fetch this?

0 个答案:

没有答案