Results 1 to 2 of 2

Thread: pagination and xml

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

    Default pagination and xml

    I found a script that looks fairly easy to implement...I just have two problems with it.

    Code:
    <?php
    $xml = simplexml_load_file('yourxml.xml');
    
    $limit = 5;
    $page = $_GET['page'];
    
    foreach ( $xml->item as $item ) {
      if ( empty($page) ) {
        if ( $item->attributes()->page > 0 && $item->attributes()->page <= $page+$limit )
          echo $item->attributes()->title,'<br/>';
        } else {
        if ( $item->attributes()->page > ($page-1)*$limit && $item->attributes()->page <= (($page-1)*$limit)+$limit )    
          echo $item->attributes()->title,'<br/>';
      }
    }
    ?>
    the xml that goes with it is
    Code:
    <root>
    <item page="1" title="title 1" />
    </root>
    My first problem is that my xml does not have a page# or ID (but I can add that)
    Code:
    <sayings>
      <message>
        <id></id> //can add this
        <vdate></vdate>
        <msgpost></msgpost>
      </message>
    </sayings>
    my big problem is that the pagination script does a foreach loop going from 1 to the end, while my script posts the data from last to first. How can I put them together?
    Code:
    $i = count($xml->messages)-1;
    for($x=$i;$x>=0;$x--)
    {	//reformat date
      $string = $xml->messages[$x]->vdate;
      $stringArray = explode("/", $string);
      $mydate = mktime(0,0,0,$stringArray[0],$stringArray[1],$stringArray[2]); 
      $converteddate = date("F j, Y", $mydate);
     	
      //write to page
      echo
        $converteddate.'<br /><p>'
        .$xml->messages[$x]->msgpost.'<hr></p>';
    }
    ?>
    Thanks

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

    Default

    I'm getting closer...
    What I have gives me the 5 most recent posts.

    Code:
    //read from bev.xml
    $file = 'xml/bev.xml';
    $xml = simplexml_load_file($file);
    
    $limit = 5;
    $page = $_GET['page'];
    $c = 1;
    
    $i = count($xml->messages)-1;
    for($x=$i;$x>=0;$x--)
    if (empty($page) ) 
    {
      if ($c > 0 && $c <= $limit)
      {	
         //write to page
        echo
          $xml->messages[$x]->$vdate.'<br /><p>'
          .$xml->messages[$x]->msgpost.'<hr></p>';
          $c++;
      }
    }
    I now need to know how to create the next page.

    Am I even going in the right direction?
    Last edited by I am Abby; 06-07-2010 at 04:52 PM.

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
  •