May 6, 201214 yr Hi, I'm creating an affiliate site using php and html (to create the site) and xml product files I got from merchants. My problem is how do I paginate the xml file so I have 10 products showing per page? I've just started to dive into php and love it so far! Here is the code I'm using to parse the xml. (it currently outputs the programe name and name of each product): $file = 'Kaspersky.xml'; $xml = simplexml_load_file("$file"); foreach ( $xml->product as $product ){ echo $product->programname .'<br/>'; echo $product->name .'<br>'; echo '<p>'; } Freddie Edited May 7, 201214 yr by Renaissance-Design Please use the code button or tags to format your code.
May 6, 201214 yr Welcome to the forums. XML doesn't use the concept of pagination, it's meant for defining and transporting data. If you Google around there are some workaround code examples such as http://www.kirupa.com/forum/showthread.php?320633-PHP-amp-XML-Pagination I'm confused as to why you would want to paginate XML data, care to elaborate?
May 6, 201214 yr I'm assuming you want to set up an html page to display 10 products per page. If I've assumed incorrectly, ignore this reply. To paginate your display with php, initially you need to determine how many records to display and how many you will display on each page. Then you can do something like $total_records = some count process; $display = 10; //10 products/page if (isset($_GET['p']) && is_numeric($_GET['p'])) { $pages = $_GET['p']; } else { //divide the number of records by the number to display on a page to determine //how many pages are required. Use the ceil function to round up the result. $records = $row[0]; if ($records > $display) { $pages = ceil ($records/$display); } else { $pages=1; } } ascertain at what record to start displaying on the page if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } Loop thru your xml data to display on the html page At the bottom of the page, display links to other pages, using the url to pass the page number and which record to start the display. if ($pages > 1) { echo '<p>'; //determine how many pages have been displayed so far $currentPage = ($start/$display) + 1; if ($currentPage != 1) { echo '<a href="thispage.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> '; } for ($i = 1; $i<=$pages; $i++) { if ($i != $currentPage) { echo '<a href="thispage.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> '; } else { echo $i . ' '; } } if ($currentPage != $pages) { echo '<a href="thispage.php?s=' . ($start + $display) . '&p=' . $pages .'">Next</a>'; } echo '</p>'; }
May 7, 201214 yr Author Thanks for the replies!! Your right gadgetgirl, I want to set up an html page to display 10 (or more) products per page. I have a large list of over 1000+ products in the xml file, so showing them all on one page isn't practical. I think I'm going to import the xml file into a database then paginate/display the products on the html page (because of the xml file size). Edited May 7, 201214 yr by AnnobilsWebDesign
May 7, 201214 yr I think I mixed up variables in my previous post - my bad. If you're going to put the xml data into a d/b you can use the count function to determine how many products you have. require_once ('mysqli_connect.php'); //connect to your d/b $display = 10; if (isset($_GET['p']) && is_numeric($_GET['p'])) { $pages = $_GET['p']; } else { //using the count function get the total number of records $r = @mysqli_query($dbc,"SELECT COUNT(product_id) FROM products"); $row = @mysqli_fetch_array($r, MYSQLI_NUM); //divide the number of records by the number to display on a page to determine //how many pages are required. Use the ceil function to round up the result. $total_records = $row[0]; if ($total_records > $display) { $pages = ceil ($total_records/$display); } else { $pages=1; } }
Create an account or sign in to comment