I am working on XML data extraction using php and came across two challenging issues to deal with…I have been trying to figure this one out but couldn’t.
I have category listing page in which I am extracting all the Product Ranges from my XML file based on the field “ProductRange” in my XML which may contain data like these:
Phones;TopRatedProducts (means Product will belong to both Product Ranges: Phones & TopRatedProducts)
Phones;Accessories (means Product will belong to both Product Ranges: Phones & Accessories)
PSP;TopRatedProducts (means Product will belong to both Product Ranges: Phones & TopRatedProducts)
I have products in my XML that belong to two different product ranges, identified by semicolon.
Question#1: How can I hide the Product Range (ProductRange)that has semicolon(;) in it so that it won’t appear repeatedly with other product ranges e.g Phones and TopRatedProducts etc?
Here is the PHP code that lists all the categories:
Product Range Page Code:
<?
$id=urldecode($this->uri->segment(3)); // $id consists of urldecoded "WebCategory"
$list = groupBy(file_get_contents('XML/products.xml'), "WebCategory");
foreach ( $list[$id] as $product )
{
$results[]=$product->ProductRange;
}
?>
<?
$product_range = array_unique($results);
foreach ($product_range as $range)
{
$try=mysql_real_escape_string($range);
?>
<a class="item" href="/subcategories/listings/<?=$range?>">
<span><? print("{$range}\n\n");?></span>
</a>
<?}?>
<?
function groupBy($xml, $categoryName)
{
$xml = new \SimpleXMLElement($xml);
$category = array();
foreach ( $xml as $row )
{
$attr = $row->attributes();
if (! isset($attr->$categoryName))
{
trigger_error("$categoryName does not exist in XML");
break;
}
$category[(string) $attr->$categoryName][] = $attr;
}
return $category;
}
?>
Note : This code gives me output like this:
- Phones;TopRatedProducts
- Phones;Accessories
- TopRatedProducts
- Accessories
- PSP;TopRatedProducts
But the output I want should be like this:
- Phones
- Accessories
- PSP
- TopRatedProducts
I need to separate the semicolon Product Ranges so that I could display them separately for related products.
Question 2:
How can list my products that belong to both ProductRanges i.e. I want to treat ProductRange=”Phones;TopRatedProducts” as two separate Product Ranges to list all the products that come under “Phones”, “TopRatedProducts” etc and behave like this mysql for such ProductRanges :
*”select * from products where ProductRange=’Phones'”;*
And
*”select * from products where ProductRange=’TopRatedProducts'”;* ?
The code :
// This code list all products based on Product Range
$id=urldecode($this->uri->segment(3)); // $id consists of urldecoded ProductRange
$list = groupBy(file_get_contents('XML/products.xml'), "ProductRange");
foreach ($list[$id] as $product ) {
$img=getImageDirectory($product->Code); ?>
<h3><a href="#"><?=$product->Name?></a></h3>
<p><?=$product->WebDescription?></p>
<img class="" src="<?=$img?>"/>
<?}?>
function getImageDirectory($iId) {
$oDirectory = new RecursiveDirectoryIterator("/var/www/Wha/images/categories/");
$oIterator = new RecursiveIteratorIterator($oDirectory);
foreach($oIterator as $oFile) {
if ($oFile->getFilename() == $iId.'.jpg') {
return $oFile->getFilename();
}
}
}
?>
Here is my XML:
Products.xml
<?xml version="1.0" standalone="yes"?>
<Rows>
<Row Code="23000" Name="HTC Wildfire S-A510E " ProductRange="Phones;TopRatedProducts" ProductSubRange="HTC" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" />
<Row Code="34001" Name="Iphone 4" ProductRange="Phones;Accessories" ProductSubRange="Apple" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" />
<Row Code="45002" Name="Samsung Galaxy S3" ProductRange="Phones;TopRatedProducts" ProductSubRange="Samsung" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" />
<Row Code="10010" Name="Samsung Galaxy earphone" ProductRange="Accessories" ProductSubRange="Samsung" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" />
<Row Code="10011" Name="PSP 3000" ProductRange="PSP;TopRatedProducts" ProductSubRange="Sony" WebCategory="Consoles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" />
<Row Code="10012" Name="Sony Erricsson Satio" ProductRange="Phones" ProductSubRange="Sony Ericsson" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" />
<Row Code="10012" Name="Sony Playstation 4" ProductRange="TopRatedProducts" ProductSubRange="Sony" WebCategory="Consoles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" />
</Rows>
I am not getting a clue how can I filter my results accordingly?
You can use
Your modified function