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.
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:
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
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.
the best way using PERL pagination package work without database .. .can read XML too i've apply to my site in http://mp3.smileylover.com regards