Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7925745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:26:04+00:00 2026-06-03T18:26:04+00:00

I tried a few solutions already posted here, but nothing seems to work. That

  • 0

I tried a few solutions already posted here, but nothing seems to work. That might be, because I am really not that familiar with php or xml. I just need to output xml for xcoding. So hopefully someone can shed some light, on how I can reverse the order of my xml, so that the last entry is on top.
My last try what with:

$query = array_reverse($doc->xpath('query'));

but it did not like it ,e.g. it did not work.

So here is my code in order to create my xml document:

<?php 

if(!$dbconnect = mysql_connect('localhost', '-', '-')) {
echo "Connection failed to the host 'localhost'.";
exit;
} // if
if (!mysql_select_db('-')) {
echo "Cannot connect to database '-'";
exit;
} // if

$table_id = 'table1';
$query = "SELECT Name,Age,Sex FROM $table_id";

$dbresult = mysql_query($query, $dbconnect);

// create a new XML document
$doc = new DomDocument('1.0');

// create root node
$root = $doc->createElement('root');
$root = $doc->appendChild($root);

// process one row at a time
while($row = mysql_fetch_assoc($dbresult)) {

// add node for each row
$occ = $doc->createElement($table_id);
$occ = $root->appendChild($occ);

// add a child node for each field
foreach ($row as $fieldname => $fieldvalue) {

$child = $doc->createElement($fieldname);
$child = $occ->appendChild($child);

$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);

} // foreach
} // while


// get completed xml document
$xml_string = $doc->saveXML();
$doc->save("yolo.xml") 

//echo $xml_string;
?> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:26:06+00:00Added an answer on June 3, 2026 at 6:26 pm

    The best thing to do is to issue an SQL query which returns results in the order you want. The SQL query you have here has no ORDER BY clause, so results come back in no particular order.

    However, you can also reverse the order of results as you add them to your XML document: instead of appending new child nodes for each row, prepend them. Replace this line:

    $occ = $root->appendChild($occ);
    

    With

    $occ = $root->insertBefore($occ, $root->firstChild);
    

    Also generally speaking it’s best to finish building a tree of XML before you add it to the document.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried a few of the fixes I found here searching around but they
I have tried quite a few codes but none of them seem to work
I've tried quite a few different solutions ( here , here , here ,
I've tried a few ways to try to do this but none of them
I've tried a few Asus Ones, and found that even switching between multiple windows
I have tried this a few different ways and it always seems to fail.
I have allready tryed solutions from a few posts, but no luck. The problem
I am trying to determine a file's mime type. I've tried a few methods,
I am using http://xpath.online-toolz.com/tools/xpath-editor.php as an online XPath evaluator. I tried a few others
Looking for something like: $(input:radio:checked).previous(name, original_name).attr(name,new_name); I tried a few different seen around here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.