2

my product.xml :

<product>
    <name></name>
    <price></price>
    <short_desc></short_desc>
    <quantity></quantity>
</product>

how i can split xml data to many page, let's say i have 30 record of product and i want it to show 5 per page, so then i have 6 page in the same 1 file.php.

i was read to split to pages but it doesn't work, anyone can help me?

    $objDOM = new DOMDocument();
    $objDOM->load("product.xml");

    $titleArray = array();

    $ps = $objDOM->getElementsByTagName("product");

    $allItems = array(
          "name" => $node->getElementsByTagName("name")->item(0)->nodeValue,
          "rice" => $node->getElementsByTagName("price")->item(0)->nodeValue,
          "short_desc" => $node->getElementsByTagName("short_desc")->item(0)->nodeValue,
          "quantity" => $node->getElementsByTagName("quantity")->item(0)->nodeValue);

    $itemsPerPage = 5;
        $page = isset($_GET['page']) ? intval($_GET['page']) : 0;

        foreach (array_slice($allItems, $page * $itemsPerPage, $page * $itemsPerPage + $itemsPerPage) as $item) {
            echo "$item\n";
        }

this is what i have doing but it doesn't show anything,,

pound Pound
  • 123
  • 3
  • 12

2 Answers2

0

Can you use simplexml_load_string or simplexml_load_file functions (from php 5) ? If your product.xml file has a simple structure, you can easily do what you want with some code like that:

$allItems = simplexml_load_file("product.xml");
$itemsPerPage = 5;
$page = isset($_GET['page']) ? intval($_GET['page']) : 0;

$pageItems = array_slice($allItems, $page * $itemsPerPage, $page * $itemsPerPage + $itemsPerPage)

foreach($allItems AS $prod) {
  echo $prod->name."<br>";
  echo $prod->price."<br>";
  echo $prod->short_desc."<br>";
  echo $prod->quantity."<br>";
  echo "---------<br>";
}

Also, check your XML file: it must begin with an xml declaration and a root element!

<?xml version='1.0'?>
<products>
  <product>
    <name>name1</name>
    <price>price1</price>
    <short_desc>desc1</short_desc>
    <quantity>q1</quantity>
  </product>
  <product>
    <name>name2</name>
    <price>price2</price>
    <short_desc>desc2</short_desc>
    <quantity>q2</quantity>
  </product>
  <product>
    <name>name3</name>
    <price>price3</price>
    <short_desc>desc3</short_desc>
    <quantity>q3</quantity>
  </product>
</products>
Ligio
  • 607
  • 2
  • 7
  • 14
0

Instead of turning all products into an array, I would first of all make use of the fact that the DOMDocument you're using offers access to individual elements and data in general.

In the following example, the pagination is done by limiting the output of all product elements to the span of the current page by making use of DOMDocuments DOMXpath object and a LimitIterator. You can find a full example with the following answer which is making use of SimpleXML:

Here is the usage-example that equally works with DOMDocument and DOMXPath:

$doc = new DOMDocument();
$doc->loadXML($xml);

$xpath    = new DOMXPath($doc);
$products = $xpath->query('/xml/product');

$_GET['page'] = 3;
$itemsPerPage = 2;

$pagination = new LimitPagination(
    defaultvar($_GET, 'page'), $products->length, $itemsPerPage
);

foreach ($pagination->getLimitIterator($products) as $product) {
    /* @var $product DOMElement */
    echo $product->ownerDocument->saveXML($product), "\n";
}

You can find the full, self-containing example code (incl. XML) here: https://gist.github.com/hakre/603638a18918b0549019

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836