SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP/XML critique

    Please could you look at the following and see if I have used the best php methods/objects etc:

    xml:
    Code:
    <rss>
    <channel>
    <title>Our rss feed</title>
    <item>
    <title>title1</title>
    <date>21/04/2008</date>
    </item>
    <item>
    <title>title2</title>
    <date>21/05/2008</date>
    </item>
    <item>
    <title>title3</title>
    <date>29/04/2008</date>
    </item>
    </channel>
    </rss>
    PHP Code:
    $xmlPath = ("test.xml"); 

    $xmlDoc = new DOMDocument();
    $xmlDoc->preserveWhiteSpace false;
    $xmlDoc->validateOnParse true;
    $xmlDoc->load($xmlPath);

    $xpath = new DomXPath($xmlDoc);
    $nodepath='//rss/channel/item';

    foreach (
    $xmlDoc->getElementsByTagName("item") as $element) {
        
    $temp $xpath->query($nodepath.'/date')->item($s)->nodeValue;
        echo (
    substr($temp02)."/".substr($temp32)."/".substr($temp82).": "$xpath->query($nodepath.'/title')->item($s)->nodeValue "<br />");

    I'm sure the for each loop could be much better, the date thing was a right pain (kept swapping the d/m, hence the rebuild!)

    Any advice greatly appreciated

    cheer

    monkey
    monkey - the rest is history

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I would have thought simpleXml would be ideal for a simple job like that.

    http://www.ibm.com/developerworks/li...simplexml.html
    http://www.google.co.uk/search?q=php+simplexml+rss

    explode the date by "/" to extract the bits if you are reshaping the date.

    But if it works for you, then leave it.
    Last edited by Cups; Nov 21, 2008 at 11:01. Reason: added last comment

  3. #3
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers cups. i looked at this (the very page actually ), but came across a problem I couldn't sort:

    I don't loop through every record using a foreach, as I have paginated my resuts:

    PHP Code:
    $nodepath='//rss/channel/item';

    for(
    $s=$startItem$s<($startItem+$maxItemsPerPage); $s++){
        if(
    $s==$totalItems){break;}
        
    $tempDate $xpath->($nodepath.'/pubdate')->item($s)->nodeValue;
        echo (
    substr($tempDate02) . "/" .  substr($tempDate32) . "/" substr($tempDate82) .": "$xpath->query($nodepath.'/title')->item($s)->nodeValue "<br />");

    I can't work out how to do the $xpath->($nodepath.'/pubdate')->item($s)->nodeValue; part using simpleXML - have have tried chopping and changing, but am getting no where.

    cheers for your help

    monkey
    monkey - the rest is history

  4. #4
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I now have this (from the IBM site), but it doesn't work - no error, just nothing on the page:

    PHP Code:
    for($s=$startItem$s<($startItem+$maxItemsPerPage); $s++){//loop through xml nodes($s)
        
    if($s==$totalItems){break;}
        
    $tempDate $xmlDoc->rss->channel->item[$s]->pubdate[0];
        echo (
    substr($tempDate02) . "/" .  substr($tempDate32) . "/" substr($tempDate82) .": ".
        . 
    $xmlDoc->rss->channel->item[$s]->title[0] . "<br />");

    monkey - the rest is history

  5. #5
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by boxhead View Post
    I now have this (from the IBM site), but it doesn't work - no error, just nothing on the page:

    PHP Code:
    for($s=$startItem$s<($startItem+$maxItemsPerPage); $s++){//loop through xml nodes($s)
        
    if($s==$totalItems){break;}
        
    $tempDate $xmlDoc->rss->channel->item[$s]->pubdate[0];
        echo (
    substr($tempDate02) . "/" .  substr($tempDate32) . "/" substr($tempDate82) .": ".
        . 
    $xmlDoc->rss->channel->item[$s]->title[0] . "<br />");

    You don't specify "rss" when accessing your data, you simply go straight to "channel."

    Your code also seems very verbose, what's wrong with:

    PHP Code:
    foreach ($xmlDoc as $item) {
        echo 
    "$item->date$item->title<br />";

    ?
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  6. #6
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Brad - that works a treat.

    however - I can't get your alternative code to work. As mentioned in my previous post, I am using 'for' instead of 'foreach' to enable me to paginate the results - this method needs the $s variable. I have tried this in your method, but cannot get it to work.

    cheers for your help

    monkey
    monkey - the rest is history

  7. #7
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by boxhead View Post
    Thanks Brad - that works a treat.

    however - I can't get your alternative code to work. As mentioned in my previous post, I am using 'for' instead of 'foreach' to enable me to paginate the results - this method needs the $s variable. I have tried this in your method, but cannot get it to work.

    cheers for your help

    monkey
    Whoops! I made a mistake in my code snippet:

    PHP Code:
    foreach ($xmlDoc->channel->item as $item) {
        echo 
    "$item->date$item->title<br />";

    Last edited by bhanson; Nov 25, 2008 at 10:30. Reason: More detail
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  8. #8
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bhanson View Post
    Whoops! I made a mistake in my code snippet:

    PHP Code:
    foreach ($xmlDoc->channel->item as $item) {
        echo 
    "$item->date$item->title<br />";

    That works better, however, it still doesn't work for the foreach method:

    PHP Code:
    for($s=$startItem$s<($startItem+$maxItemsPerPage); $s++){
        if(
    $s==$totalItems){break;}
                 echo (
    $xmlDoc->channel->item[$s]->pubdate.': ' $xmlDoc->channel->item[$s]->title "<br />");


    monkey - the rest is history

  9. #9
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by boxhead View Post
    That works better, however, it still doesn't work for the foreach method:

    PHP Code:
    for($s=$startItem$s<($startItem+$maxItemsPerPage); $s++){
        if(
    $s==$totalItems){break;}
                 echo (
    $xmlDoc->channel->item[$s]->pubdate.': ' $xmlDoc->channel->item[$s]->title "<br />");


    You mean for, right?

    Is everything working now? Or is your for loop broken.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  10. #10
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bhanson View Post
    You mean for, right?

    Is everything working now? Or is your for loop broken.
    spot on! I'm sure I tried that, but must have missed something. One final question. I want to format the date to d/m/y without the time. the date stored in my xml take this format: 21/08/2008 13:34:44.

    I have tried this:
    echo (date("d/m/y",($xmlDoc->channel->item[$s]->pubdate)).': ' . $xmlDoc->channel->item[$s]->title . "<br />");
    [/PHP]

    but get the error 'date() expects parameter 2 to be long'

    cheers again

    monkey
    monkey - the rest is history

  11. #11
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    Since you're using XPath anyway, why not use that to only grab the items that you're interested in (for the current page) then just use a regular foreach loop as you normally would. For example, this grabs the latest topics from Sitepoint's PHP forums and displays the first page (of 5 items) only.

    PHP Code:
    // Step 1. Grab the XML document
    $url 'http://www.sitepoint.com/forums/external.php?type=RSS2&forumids=34,147';
    $xml = new SimpleXMLElement($urlLIBXML_NOCDATATRUE);

    // Step 2. Grab only this page of items
    $page    1;
    $perpage 5;
    $offset  = ($page 1) * $perpage;
    $path    sprintf('/rss/channel/item[position() > %d and position() <= %d]'$offset$offset $perpage);
    $items   $xml->xpath($path);


    // Step 3. Output a HTML list of this page of items
    header('Content-Type: text/html; charset=utf-8');
    if (
    count($items)) 
    {
        echo 
    '<ul>';
        foreach (
    $items as $item)
        {
            
    $title = (string) $item->title;
            
    $date  date('d/m/Y'strtotime($item->pubDate));
            
    printf('<li>%s: %s</li>'$date$title);
        }
        echo 
    '</ul>';
    }
    else
    {
        echo 
    '<p>Nothing to display</p>';

    Just an idea.
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •