0

I am trying to sort a list of databases and documents in each database by the databasename. Then print them out in a tabled format

here is the xml

<responsedata>
<databases>
<database>
<name>Test</name>
<documents>0</documents>
</database>
−
<database>
<name>Test2</name>
<documents>0</documents>
</database>
</databases>
</responsedata>

here is my current code that lists the databases and documents without the sort and print them out in a list.

$request_url = "http://255.255.255.255/xmlfile";

$xml = simplexml_load_file($request_url)

foreach ($xml->responsedata->databases->database as $db) {

          echo"<tr><td>",$db->name,"</td><td>",$db->documents,"</td></tr>\n";
  }

//

1
  • <xsl:sort select="name" data-type="text" /> Commented Dec 2, 2010 at 19:01

1 Answer 1

2
$dbs = array();

foreach ($xml->responsedata->databases->database as $db) {
  $dbs[(string)$db->name] = $db;
}

ksort($dbs);

foreach ($dbs as $db) {
  echo"<tr><td>",htmlentities($db->name),"</td>",
      "<td>",htmlentities($db->documents),"</td></tr>\n";
}
Sign up to request clarification or add additional context in comments.

2 Comments

There is no output possibly something wrong with $dbs and $db?
@Bucabear: See changed code. You must explicitly cast the $db (which is of type SimpleXMLElement) to string or PHP won't accept the array index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.