I'm not sure whether this is a question about php or xml or what to be honest - a poke in the right direction would be much appreciated...

I'm storing news articles in XML format.

newsitems.php which contains the XML looks like this;

<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<items>

 <item id="1">
  <date>10/12/05</date>
  <topic>Topic</topic>
  <head>Old News</head>
  <info>
This is the oldest news item
  </info>
  <by>Me</by>
 </item>

 <item id="2">
  <date>13/12/05</date>
  <topic>Topic</topic>
  <head>New News</head>
  <info>
This is the most recent bit of news
  </info>
  <by>Me</by>
 </item>

<items>

XML;
?> 

and outputting the using the code below;

<?php 

include 'newsitems.php'; 

$news = '<div class="main">' . 
               '<h1>Latest News</h1>';

$xml = simplexml_load_string($xmlstr);

foreach ($xml->item as $item) {

$news .=
'<div class="newsitem">
<table class="news" border="0">
<tr>
<td class="head"> '. $item->head .' </td>
<td class="topic"> '. $item->topic .' </td>
</tr>
<td class="info" colspan="2"> '. $item->info .' </td>
</tr>
<tr>
<td class="by" colspan="2">By: '. $item->by .' '. $item->date .' </td>
</tr>
</table>
</div>';
}

$news .= '</div>';

echo $news;
?>

The problem is that the articles run in the wrong order, oldest appearing at the top. How do I go about sorting the order they appear in based on the id attibute of <item> ? A link to the relevant bit of a manual or tutorial would be much appreciated - I don't even know where to start!

    This one's easy! :p

    Your problem is the $news .= 'your tables here';

    Make it $news = 'your tables here' . $news;

    That will make them print in reverse order.

    -Peace!

      I'm not quite sure what Andrew is talking about or how his solution could work.

      Anyway I had a quick look at passing a SimpleXml object to PHP's usort() function but can't get it to do any sorting - it might be worth more experimentation.

      Another solution is to create an array when iterating over the XML, sort the array with usort() and then create the HTML table from the array.

        ummmmm try instead of using the usual foreach loop use a backward for loop starting at the last item....

          Thanks for the thoughts.

          In all honesty, I could just switch the order I write the XML data which would work too... I was thinking of in the future allowing different views of looking at news - sorted by date, topic or person who submitted it for example so I'm keen to make it a genuine sort rather than 'fixing' it simply by knowing which order I put my data in.

          I'll have a look at using an array and usort() when I get home from work.

          I've also been just looking at using XSLT to transform the XML to create an XHTML file which I can then just include. I suspect this may be the easiest way.

          Keep the ideas coming if anyone knows better tho, I'm clutching at straws 🙂

            I've also been just looking at using XSLT to transform the XML to create an XHTML file which I can then just include. I suspect this may be the easiest way.

            well yes and no....

            using XSLT transformation's can be easy to just display some news definatly.. however your guna loose alot of functionallity (unless of course you an XSL guru?)

              Nice cheers for that. I've done a bit more reading up and it seems like XSLT is the way forward - better to use a tool thats designed for manipulating XML than trying to get PHP to do it for me.

              This tutorial does almost exactly the same thing with a CD collection list as I'm trying to achieve with my news page - http://www.w3schools.com/xsl/xsl_sort.asp.

              I'm certainly no 'XSL guru' but it looks straightforward enough. Cheers for the link too.

                Hmm... OK, I've run into some unexpected problems with this.

                I now have an XML file which references a XSL file. When I open the XML file in a browser it correctly applies the XSL and sorts and formats the XML file. See the link to the tutorial in my post above to see what I mean.

                The problem is though that when I try to include () the xml file I get a parse error. I think that this is due to the xml declaration at the start of the .xml file being enclosed in <? ?>.

                This brings me to question 1: How do I turn off short tags so it'll only parse <?php ?> rather than everything in <? ?>. I'm using php5 on apache server if that helps... I had a brief serach throgh both manuals but couldn't find referance to short tag format in either (I'll have a proper look later though)

                Moving on...

                On the coments on this page there are a number of ways offered in the user comments at the bottom for getting round this problem;

                http://www.php.net/manual/en/language.basic-syntax.php

                I've tried several and, while I no longer get the php parse error, the xml file isn't finding the xsl so is just displayed as plain text...

                So heres question 2: Why is the xml file not linking to the xsl correctly? Is it just because the browser isn't recognising it as xml because I 'cheated' php into echoing the xml and xsl sheet declarations (in which case I assume resolving question 1 will sort this out too) or am I missing something more serious?

                Hope that all made sense!

                  There is a php.ini setting for this, short_open_tag. Quote from the manual:

                  Tells whether the short form (<? ?>) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"'; ?>. Also if disabled, you must use the long form of the PHP open tag (<?php ?>).

                      Note: This directive also affects the shorthand <?=, which is identical to <? echo. Use of this shortcut requires short_open_tag to be on. 

                    Brilliant, thank you! Wasn't sure whether it was an apache server setting or something I needed to change in a php file so thats saved me a lot of floundering!

                    NOTE: After I realised I couldn't 'unresolve' this thread I created a new one! I'd intended come come back and delete my latest post in this one but Shrike was too fast for me...

                    Please could new replies be put in the new thread (which I won't resolve until I've actually tested the solution) and we can let this one die. Sorry for the confusion 🙂

                    http://phpbuilder.com/board/showthread.php?t=10314127

                      Write a Reply...