flow control depending on value of xml element

I have a long xml data file with 500+ items in it, it comes in this form:

<?xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>
<ITEM>
    <TITLE>ITEM name</TITLE>
    <TYPE>TYPE </TYPE>
    <DESCIPTION>DESCIPTIONiliate Page CPM</DESCIPTION>
    <PRICE>PRICE</PRICE>
    <ITEM>http://mysite.com/item-link</ITEM>
</ITEM>
</CATALOG>

and I use the following code in the php page to import data from the xml file:

<?php
$ITEMSS = new SimpleXMLElement('ITEMS.xml', null, true);

echo <<<EOF
<table width="100%" align="center" border="1" bordercolor="#0099ff" cellpadding="1" cellspacing="0">

    <tr>
            <th bgcolor="#66ccff"><span class="style4">ITEM Name</span></th>
            <th bgcolor="#66ccff"><span class="style4">item TYPE </span></th>
            <th bgcolor="#66ccff"><span class="style4">item DESCIPTION </span></th>
            <th bgcolor="#66ccff"><span class="style4">item PRICE</span></th>
            <th bgcolor="#66ccff"><span class="style4">link to item</span></th>
    </tr>

EOF;
foreach($ITEMSS as $ITEMS) // loop through our DATAS
{
    echo <<<EOF
    <tr height="30" align=middle>
            <td><a href="{$ITEMS->ITEM}" target="_blank"><span class="STYLE7">{$ITEMS->TITLE}</span></a></td>
            <td><span class="STYLE8">{$ITEMS->TYPE}</span></td>
            <td><span class="STYLE8">{$ITEMS->DESCIPTION}</span></td>
            <td><span class="STYLE8">{$ITEMS->PRICE}</span></td>
            <td><a href="{$ITEMS->ITEM}" target="_blank"><B><span class="STYLE7">cHECK IT OUT</span></B></a></td>
    </tr>

EOF;
}
echo '</table>';
?>

I need to add an "if" statement in the loop to select only some data if "TYPE" has a certain value, it will show that data, if not it will skip it.

Also need to add paging system, since there will be 500+ item to be listed, I want the table to show a miximum number of items, saying 25.

Thank you guys for your help!

Answers


As an alternative you could select (and process) only those ITEM elements that have a specific text value in the TYPE element via xpath, e.g.

<?php
$doc = new SimpleXmlElement(getData());
foreach($doc->xpath("//ITEM[TYPE='A']") as $item) {
    echo $item->DESCIPTION, "\n";
}

function getData() {
    return <<< eox
<?xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>
<ITEM>
    <TITLE>item 1</TITLE>
    <TYPE>A</TYPE>
    <DESCIPTION>desc1</DESCIPTION>
    <PRICE>price1</PRICE>
    <ITEM>item1-link</ITEM>
</ITEM>
<ITEM>
    <TITLE>item x</TITLE>
    <TYPE>B</TYPE>
    <DESCIPTION>descx</DESCIPTION>
    <PRICE>pricex</PRICE>
    <ITEM>itemx-link</ITEM>
</ITEM>
<ITEM>
    <TITLE>item y</TITLE>
    <TYPE>B</TYPE>
    <DESCIPTION>descy</DESCIPTION>
    <PRICE>pricey</PRICE>
    <ITEM>itemy-link</ITEM>
</ITEM>
<ITEM>
    <TITLE>item 2</TITLE>
    <TYPE>A</TYPE>
    <DESCIPTION>desc2</DESCIPTION>
    <PRICE>price2</PRICE>
    <ITEM>item2-link</ITEM>
</ITEM>
</CATALOG>
eox;
}

prints

desc1
desc2

-- edit: with some paging functionality --

<?php
define('ITEMS_PER_PAGE', 10);
$page = 1; // =intval($_GET['page']);

$posMin = $page*ITEMS_PER_PAGE;
$posMax = ($page+1)*ITEMS_PER_PAGE;
$doc = new SimpleXmlElement(getData());
foreach($doc->xpath("(//ITEM[TYPE='A'])[position()>=$posMin and position()<$posMax]") as $item) {
    echo $item->DESCIPTION, "\n";
}

function getData() {
    $xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>';
    for($i=0; $i<100; $i++) {
        $type = 0===$i%2 ? 'A':'B';
        $xml .= "   
        <ITEM>
        <TITLE>item $i</TITLE>
        <TYPE>$type</TYPE>
        <DESCIPTION>desc $i</DESCIPTION>
        <PRICE>price $i</PRICE>
        <ITEM>item{$i}-link</ITEM>
        </ITEM>";
    }
    $xml.='</CATALOG>';
    return $xml;
}

The function getData() is just some boilerplate that returns some xml data. For understanding the xpath query have a read of a (good) xpath tutorial, see e.g. Using XPATH to access XML elements (was: Good tutorial to learn xpath)


You can add the loop as such:

foreach($ITEMSS as $ITEMS) // loop through our DATAS
{
    if($ITEMS->TYPE=="BoogeyWoogey")
    {
        echo <<<EOF
        <tr height="30" align=middle>
            <td><a href="{$ITEMS->ITEM}" target="_blank"><span class="STYLE7">{$ITEMS->TITLE}</span></a></td>
            <td><span class="STYLE8">{$ITEMS->TYPE}</span></td>
            <td><span class="STYLE8">{$ITEMS->DESCIPTION}</span></td>
            <td><span class="STYLE8">{$ITEMS->PRICE}</span></td>
            <td><a href="{$ITEMS->ITEM}" target="_blank"><B><span class="STYLE7">cHECK IT OUT</span></B></a></td>
        </tr>

EOF;
    }
}

Assuming that you are looping through the foreach and want to only display if the $ITEMS->TYPE property has the value BoogeyWoogey in it.

The Paging part will not be quite as simple.

You can do something like this:

You can add the loop as such:

$myLimit=(!empty($_REQUEST['myLimit']) ? $_REQUEST['myLimit'] : 0;
foreach($ITEMSS as $ITEMS) // loop through our DATAS
{
    for($i=$myLimit;$i<$myLimit+25;$i++)
    {
        if($ITEMS->ITEM=="BoogeyWoogey")
        {
            echo <<<EOF
            <tr height="30" align=middle>
                <td><a href="{$ITEMS->ITEM}" target="_blank"><span class="STYLE7">{$ITEMS->TITLE}</span></a></td>
                <td><span class="STYLE8">{$ITEMS->TYPE}</span></td>
                <td><span class="STYLE8">{$ITEMS->DESCIPTION}</span></td>
                <td><span class="STYLE8">{$ITEMS->PRICE}</span></td>
                <td><a href="{$ITEMS->ITEM}" target="_blank"><B><span class="STYLE7">cHECK IT OUT</span></B></a></td>
            </tr>

            EOF;
        }
    }
}

But you need to pass the variable $myLimit through a $GET or $_POST to the page to use it in the loop. (Feel free to change it to use a $_SESSION or anything else that suits your fancy - I figured using the variable in a link would be the easiest, so most likely a $GET should do the trick.


Need Your Help

Command line to find units in a p2 repository using p2 query languange

eclipse p2 update-site

p2 has a query languange that allows to run queries on the content of a p2 repository. However all examples in the documentation assume that the query language is used from within Java, e.g.

Correcting floating point numbers

iphone ios xcode cocoa-touch cocoa

I am wondering if there is a way that you can easily and safely correct floating point numbers.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.