Results 1 to 5 of 5

Thread: Writing only last object in xml

  1. #1
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Writing only last object in xml

    I have a page that has a default message unless you change it. So when there's only one record in the xml you will see the default...but it you add a record the page will display that.

    So I want my page to only print the last record in the xml.

    The xml
    Code:
    <myusers>
      <messages>
        <id>1</id>
        <message>words words and more words</message>
      </messages>
    </myusers>
    Code:
    <?php
     $file = 'myxml.xml';
     $xml = simplexml_load_file($file);
    
     foreach($xml->children as messages)
     {
        $i = count ($xml->messages)-1;
        echo $messages[$i]->message;
     }
    ?>
    I would thing this would print the last message only...but that's not what I'm getting.
    Last edited by I am Abby; 05-10-2010 at 04:13 PM. Reason: too many "only"s in the title

  2. #2
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    I tried

    Code:
    <?php
     $file = 'myxml.xml';
     $xml = simplexml_load_file($file);
    
     $i = count($xml->messages)-1;
     for($x=$i;$x>=0;$x--)
     {
        echo '
               <hr>'
               .$id.'<br />'
               .$messages[$x]->message;
     }
    ?>
    which gives me a "<hr>" line across the page for every item in my xml. But I don't get anything in the xml printed out.

  3. #3
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Quote Originally Posted by I am Abby View Post
    I tried

    Code:
    <?php
     $file = 'myxml.xml';
     $xml = simplexml_load_file($file);
    
     $i = count($xml->messages)-1;
     for($x=$i;$x>=0;$x--)
     {
        echo '
               <hr>'
               .$id.'<br />'
               .$messages[$x]->message;
     }
    ?>
    w
    In case anyone else has this problem the answer is to use

    Code:
    echo '
            <hr>'
            .$xml->messages[$x]->id.'<br />'
            .$xml->messages[$x]->message;
            break;
    the above will print the last item in your xml only.

    Yea me!

  4. #4
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Smile

    If you will write like this, you may be able to see the result as expected.

    <?php
    $file = "myxml.xml";
    $xml = simplexml_load_file($file);
    $cnt = count($xml->messages);

    for($i=0;$i<$cnt;$i++){

    if($i==$cnt-2){
    print $xml->messages[$i]->message;
    }

    }

    ?>

  5. #5
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    [QUOTE=katierosy;226454]
    if($i==$cnt-2)
    QUOTE]

    katierosy,
    I'm not sure I understand this line...why -2 and not -1?

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
  •