#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    25
    Rep Power
    0

    Pagination from XML using PHP


    Hi, I need to create pages with data parsed from XML into PHP.

    ---catalog.xml---
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <Catalog>
    <Category>
    <Name>CAT 1</Name>
    <Location>
    <Id>C01</Id>
    <Description>This is CAT 1, ID C01</Description>
    </Location>
    <Location>
    <Id>C02</Id>
    <Description>This is CAT 1, ID C02</Description>
    </Location>
    <Location>
    <Id>C03</Id>
    <Description>This is CAT 1, ID C03</Description>
    </Location>
    <Location>
    <Id>C04</Id>
    <Description>This is CAT 1, ID C04</Description>
    </Location>
    </Category>
    <Category>
    <Name>CAT 2</Name>
    <Location>
    <Id>C05</Id>
    <Description>This is CAT 2, ID C05</Description>
    </Location>
    <Location>
    <Id>C06</Id>
    <Description>This is CAT2, ID C06</Description>
    </Location>
    <Location>
    <Id>C07</Id>
    <Description>This is CAT 2, ID C07</Description>
    </Location>
    <Location>
    <Id>C08</Id>
    <Description>This is CAT2, ID C08</Description>
    </Location>
    <Location>
    <Id>C09</Id>
    <Description>This is CAT2, ID C09</Description>
    </Location>
    <Location>
    <Id>C10</Id>
    <Description>This is CAT2, ID C10</Description>
    </Location>
    </Category>
    </Catalog>

    ---search.php---
    [php]

    $catalog = simplexml_load_file("catalog.xml");

    foreach($catalog->Category[1]->Name as $name)
    foreach($catalog->Category[1]->Location as $site)
    foreach($site->Id as $ID)
    foreach($site->Description as $desc)
    {
    $all_data[$i]["Name"] = $name;
    $all_data[$i]["ID"] = $ID;
    $all_data[$i]["Description"] = $desc;
    $i++;
    }

    $limit = 3;
    $page = (int) $_GET['page'];
    $page = $page > 1 ? $page : 1;
    $start = $limit * ($page - 1);

    for($i=$start; $i<($start+$limit); $i++) {
    $content.="
    ".'<table><tr><td>'.$all_data[$i]["Name"].'<br />'."
    ".'<br />'.$all_data[$i]["ID"].'<br />'."
    ".'<br />'.$all_data[$i]["Description"].'</td></tr></table>'."
    ";
    }

    echo $content;

    $pages = ceil(count($all_data) / $limit);

    $links = array();

    for($i=1; $i<=$pages; $i++) {
    $links[] = "<a href=\?page=".$i.">".$i."</a>";
    }

    echo implode(" - ",$links);


    ---Output---

    CAT 2

    C06

    This is CAT2, ID C06
    CAT 2

    C07

    This is CAT 2, ID C07
    1 - 2


    ---Problem---
    1. The result is not showing the first ID Node C05

    2. The page link is not working. When click on 2, it just brings me to the server error without indicating the error code.

    Can someone advise me how should I overcome this?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    25
    Rep Power
    0
    Ok, I resolved the first problem due to the array. Now can someone tells me how to resolve the page link issue. Many thanks.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    25
    Rep Power
    0
    Thanks all, I got this resolved.

IMN logo majestic logo threadwatch logo seochat tools logo