1

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>"T1", "T2", "T3" </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>';
?>

in the TYPE I have different values like:

    <TYPE>"T1", "T2", "T3" </TYPE>
    <TYPE>"T1", "T4", "T6" </TYPE>
    <TYPE>"T1", "T8", "T1" </TYPE>

each item may have diffrent TYPE values

I need to add an "if" statement in the loop to select only some data if a value is in array of TYPE somethink like:

foreach($ITEMSS as $ITEMS) // loop through our DATAS
{
$options = array($ITEMS->TYPE);

if (in_array("T1", $options))

   { 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 even if the value I look for is in the array , it shows nothing!.

So how can I use if in array statement in this case?

10
  • With "value I want", can you give an example of exactly what you would have there? Commented Sep 22, 2012 at 15:15
  • That is not an array but a string, so in_array does not make much sense. Better isolate the problem first, your question will be much better and you will solve it faster. Commented Sep 22, 2012 at 15:18
  • the Value I want can be T1 , T2 ..., I want to show on a page data that have T1 in TYPE and in other page data that have T3 in TYPE for example Commented Sep 22, 2012 at 15:18
  • @hakra , what do you mean? so how to use if statement to check if a value exists in a given list of TYPE ? Commented Sep 22, 2012 at 15:21
  • Please add the output of var_dump($ITEMS->TYPE); to your question. Commented Sep 22, 2012 at 15:21

2 Answers 2

1

The problem comes from your preparation of the $options variable, try initiating it like so;

PHP >= 5.3

$options = array_map(function($type) {
    return str_replace('"', '', trim($type));
}, explode(',', $ITEMS->TYPE));

PHP < 5.3 (put the function out of the loop :D)

function parseTypes($type) {
    return str_replace('"', '', trim($type));
}

$options = array_map(parseTypes, explode(',', $ITEMS->TYPE));

That should explode the types into an array, remove the quotes and trim extra spaces.

Sign up to request clarification or add additional context in comments.

3 Comments

I got this error: " unexpected T_FUNCTION, expecting ')' in line" and that line is for "$options = array_map(function($type) { "
Added a < 5.3 example for use without closures
thank you so much Jason Brumwell, it was perfect , I have php 5.2.17 , that's why it wouldn't work :)
1

Instead of placing an if clause inside the foreach loop, you just pass another list into the foreach, one that only contains the elements you are looking for. Works like magic, actually this is called xpath:

$value = 'T1';
$list  = $ITEMSS->xpath('/CATALOG/ITEM[contains(TYPE, \'"' . $value . '"\')]');
foreach($list as $ITEMS)
{
    ...

And that's it. This works because each value you might want to look for is enclosed in double quotes ", a common principle.

BTW, I think your code would profit a lot from a simple template engine, like for example Mustache. It works as well with iterators (a list of values you iterate over with foreach).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.