Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How doI paginate xml?

Featured Replies

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 by Renaissance-Design
Please use the code button or tags to format your code.

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?

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>';
}

  • 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 by AnnobilsWebDesign

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.