douti0467 2019-08-13 12:30
浏览 72

如何以下列格式构建数组?

I want to build an array to display the data in XML formats. I am fetching records for two different post from database and wants to build an structure in following format.

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

I have done code in following way...

$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)) {
  }
}

basically there are two posts with comments and its child comments... i want to map the database records in xml formats...

<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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题