Help with Pagination with PHP and XML

Discussion in 'PHP' started by inhook, Apr 25, 2009.

  1. #1
    I am dealing with an XML Feed which looks like this example given below

    
    <catalog>
       <book>
          <author>Gambardella, Matthew</author>
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
          <publish_date>2000-10-01</publish_date>
          <description>An in-depth look at creating applications 
          with XML.</description>
       </book>
    </catalog>
    
    
    PHP:

    I am using simplexml_load_file() to read the XML and display it on the web page. Now i need to add pagination to the output so that i can only display chunks of data rather than the whole 200+ set that i am getting with this XML. Any idea how should i proceed with this.

    Also if there was a way to get a count of the number of books in the XML that i am reading.
     
    inhook, Apr 25, 2009 IP
  2. trukin

    trukin Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think your easiest way will be to load everything in a variable, already parsed like this:

    
    $sxml = simplexml_load_file("THE_FILE.xml");
    
    $books = array(); 
    foreach ($sxml->book as $book) { 
    	$books[] = array(
    		'author'=>(string)$book->author,
    		'genre'=>(string)$book->genre,
    		'price'=>(string)$book->price,
    		'publish_date'=>(string)$book->publish_date,
    		'description'=>(string)$book->description,
    	);
    }
    
    
    PHP:
    Now, $books will contain all books in the XML, we'd need to go through them then

    
    $totalBooks = count($books);
    $perPage = 10;
    $page = isset($_GET['page']) && ($page = intval($_GET['page'])) > 0 ?  $page : 1;
    
    $start = ($page - 1) * $perPage;
    $end = $start + $perPage;
    
    
    // create an array with your workable books
    
    $myBooks = array();
    for ($a=$start; $a<$end; ++$a) { 
    	if (isset($books[$a])) { 
    		$myBooks[] = $books[$a];
    	}
    }
    
    
    PHP:

    Now you need to print a simple pagination

    
    $pages = ceil($totalBooks / $perPage);
    
    for ($a=1; $a<=$pages; ++$a) {
    	echo '<a href="?page=' . $a . '">' . $a . '</a>';
    }	
    
    PHP:
     
    trukin, Apr 25, 2009 IP
  3. inhook

    inhook Well-Known Member

    Messages:
    248
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #3
    thank you very much this puts me on the right track but got stuck somewhere else

    where do i echo the output based on the page that was selected. I get the pagination numbers

    I tried that setting immediately after the get $page but that did not work
     
    inhook, Apr 26, 2009 IP
  4. trukin

    trukin Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    By the way, on the piece of code that does :

    
        $books[] = array(
            'author'=>(string)$book->author,
            'genre'=>(string)$book->genre,
            'price'=>(string)$book->price,
            'publish_date'=>(string)$book->publish_date,
            'description'=>(string)$book->description,
        );
    
    PHP:
    I forgot to append the title element to the array.

    Replace it with this one:

    
        $books[] = array(
            'title'=>(string)$book->title,
            'author'=>(string)$book->author,
            'genre'=>(string)$book->genre,
            'price'=>(string)$book->price,
            'publish_date'=>(string)$book->publish_date,
            'description'=>(string)$book->description,
        );
    
    PHP:
    Oh, yes .

    Anywhere after the piece of code before the pagination or after the pagination.

    Here is an example of parsing the data we parsed initially.

    
    <?
    foreach ($myBooks as $book) { 
    	echo "<h1>" . $book['title'] . "</h1>";
    	echo "Genre is " . $book['genre'] . "<br />";
    	echo "Price is " . $book['price'] . "<br />";
    	echo "Published on " . $book['publish_date'] . "<br />";
    	echo "Description: <blockquote>" . $book['description']</blockquote>";
    }
    
    PHP:
    By the way, I did not test any of this code but it should work flawlessly.
     
    trukin, Apr 26, 2009 IP
  5. ghprod

    ghprod Active Member

    Messages:
    1,010
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    78
    #5
    the best way using PERL pagination package :D

    work without database .. .can read XML too :)

    i've apply to my site in http://mp3.smileylover.com

    regards
     
    ghprod, Apr 27, 2009 IP
  6. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    where, exactly?
     
    szalinski, Apr 28, 2009 IP
  7. ghprod

    ghprod Active Member

    Messages:
    1,010
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    78
    #7
    ghprod, Apr 28, 2009 IP
  8. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    to quote you:
    hmm, no wonder i was confused :D

    thanks, interesting find :)
     
    szalinski, Apr 28, 2009 IP
  9. ghprod

    ghprod Active Member

    Messages:
    1,010
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    78
    #9
    sorry for wrong type the name :p

    regards:D
     
    ghprod, Apr 30, 2009 IP