0

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?
  • So content_id refers to the post ids? Then simply order by that first, and implement a control break in your output loop over the data. – misorude Aug 13 '19 at 12:37
  • here content_id and post id are different – Rohan1990 Aug 13 '19 at 12:39
  • 1
    Which of the XML documents you trying to create and what is the issue? I guess it's the first one since the second one is invalid (tag names can't contain spaces in XML)? Also, what have you tried? The posted code just gets some data from a database which you then iterate over. Where are you actually trying to create the XML? If the question is how to create XML from an array, [check the answers for this question](https://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml) – M. Eriksson Aug 13 '19 at 12:39
  • But I guess you _have_ the post id in the comments table in some column? Then order by _that_ first, rest still stands. – misorude Aug 13 '19 at 12:44

0 Answers0