Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

My code:

$go = mysql_query($sql);
$fetch = mysql_fetch_array($go);
$return = Array($fetch[0],$fetch[1],$fetch[2]);  
$results = Array(  
    'body' => Array (  
           'entry' => Array ( 
            'title'    => $return[0],  
            'image'    => $return[1], 
            'caption'  => $return[2]
                )  
                   )
        ); 

XML Output:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<callback xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
<body>
 <entry>
  <title>REHEARSAL DINNER IN SEATTLE, WA</title>
  <image>images/portfolio_entries/default.png</image>
  <caption>A wonderful in home event for 27 people.</caption>
 </entry>
</body>


I want it to loop through all of the rows from the query though and for the xml output to look something like this:

<callback xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
 <body>
  <entry>
   <title>REHEARSAL DINNER IN SEATTLE, WA</title>
   <image>images/portfolio_entries/default.png</image>
   <caption>A wonderful in home event for 27 people.</caption>
  </entry>
  <entry>
   <title>BLAH NUMBER TWO</title>
   <image>images/portfolio_entries/default2.png</image>
   <caption>Blah blah info about number two.</caption>
  </entry>
  <entry>
   <title>BLAH NUMBER THREE</title>
   <image>images/portfolio_entries/default3.png</image>
   <caption>blah blah info about number three.</caption>
  </entry>
 </body>
</callback>
share|improve this question
    
And what are you having difficult with ? –  Prix Jun 1 '13 at 0:27
    
It only outputs the first row of the query results. I'd like it to loop through all the rows of the result as shown in the second output above –  user1519235 Jun 1 '13 at 0:31

1 Answer 1

echo '<callback xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
<body>'; 
foreach($results['entry'] as $data{
    echo '<entry>';
    echo '<title>'.$data['title'].'</title>';
    echo '<image>'.$data['image'].'</image>';
    echo '<caption>'.$data['caption'].'</caption>';
    echo '</entry>';
}
echo '</body>';

May need further tweaking but this should display your XML document just fine.

Edit:

This will work if your array is fully populated, to do this you will want to populate the array using another loop, alternatively you can use a while loop to echo out all the rows like the following:

while ($row = mysql_fetch_assoc($result)) {
    echo '<entry>';
    echo '<title>'.$row['database column name'].'</title>';
    echo '<image>'.$row['database column name'].'</image>';
    echo '<caption>'.$row['database column name'].'</caption>';
    echo '</entry>';
}

Obviously you must replace 'database column name' with the relevant column names from your MySQL database. good luck, and let me know how you get on.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.