0

Possible Duplicate:
distinct in Xpath?

I am trying to extract data from my XML file using PHP. I want to get unique ProductRange based on WebCategory from my XML file. But the PHP code written below generate duplicate/repetitive results. I don't know where I am making mistake! Here is the code :

XML code:

<?xml version="1.0" standalone="yes"?>
<Rows> 
<Row Code="10000" Name="HTC Wildfire S-A510E " ProductRange="HTC" ProductSubRange="Wildfire" 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="10001" Name="HTC Wildfire" ProductRange="HTC" ProductSubRange="Wildfire" 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="10002" Name="Samsung Galaxy S3" ProductRange="Samsung" ProductSubRange="Galaxy" 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="10003" Name="Samsung Galaxy S2" ProductRange="Samsung" ProductSubRange="Galaxy" 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="10004" Name="Samsung Galaxy S1" ProductRange="Samsung" ProductSubRange="Galaxy" 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="10005" Name="Samsung Galaxy Tabloid" ProductRange="Samsung" ProductSubRange="Galaxy Tabloids" WebCategory="Gadgets" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10006" Name="Apple Ipad 3" ProductRange="Apple" ProductSubRange="Tabloids" WebCategory="Gadgets" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10007" Name="Apple Iphone 4S" ProductRange="Apple" ProductSubRange="Iphone" 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="10008" Name="Apple Iphone 3G" ProductRange="Apple" ProductSubRange="Iphone" 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="10009" Name="Miscrosoft XBOX 360 Elite" ProductRange="Microsoft" ProductSubRange="XBOX" 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="10010" Name="Sony Playstation 4" ProductRange="Sony" ProductSubRange="Playstation" 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="10011" Name="Sony PSP Go" ProductRange="Microsoft" ProductSubRange="PSP" 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="Sony Ericsson" ProductSubRange="Satio Series" 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="10013" Name="TomTom Go Live Gl2" ProductRange="TomTom" ProductSubRange="Go Live" WebCategory="Navigation" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
</Rows>

I want to get unique ProductRange based on WebCategory from my XML file. But the PHP code written below generate duplicate/repetitive results. I don't know where I am making mistake!

PHP Code:

  <?
 $xml =  simplexml_load_string(file_get_contents('XML/products.xml'));
 $prifix = '/categories/listings/' ;
 $cat=array();
 foreach ($xml as $row) {
 $attr = $row->attributes();
 if (!in_array((string)$attr->WebCategory, $cat)){
  printf('<li>%s</li>', anchor($prifix . $attr->Code, $attr->ProductRange));
  $cat[] = (string)$attr->WebCategory;
 }
 }
 ?>

PLEASE NOTE: I want to extract ProductRange based on given WebCategory e.g I want to show all ProductRange according to their Webcategory like this select SQL query:

     "select ProductRange from XML where WebCategory='Mobiles'"

and it could give me distinct "ProductRange"(Not repetitive results) based on XML like this:

Htc
Samsung
Iphone

and so... I tried my best but failed to generate unique "ProductRange" based using the coding approach mentioned above.

Please correct me where i am wrong and kindly guide me where I need to make change to get unique ProductRange as mentioned above.

Community
  • 1
  • 1
Bilal Khalid
  • 95
  • 5
  • 22

1 Answers1

1

You can try

$list = groupBy($xml, "WebCategory");
foreach ( $list['Mobiles'] as $product ) {
    printf('<li>%s %s</li>', $product->Code, $product->Name);
}

Output

  • 10000 HTC Wildfire S-A510E
  • 10001 HTC Wildfire
  • 10002 Samsung Galaxy S3
  • 10003 Samsung Galaxy S2
  • 10004 Samsung Galaxy S1
  • 10007 Apple Iphone 4S
  • 10008 Apple Iphone 3G
  • 10012 Sony Erricsson Satio
  • Function Used

    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;
    }
    

    See live Demo

    Baba
    • 94,024
    • 28
    • 166
    • 217
    • Brilliant & logical answer..thank you Baba.I really appreciate your help.bless – Bilal Khalid Oct 14 '12 at 20:30
    • You are welcome anytime .. is there any other thing i can help you with @Bilal Khalid – Baba Oct 14 '12 at 20:31
    • @Baba.i have tried $xml = simplexml_load_string(file_get_contents('XML/products.xml')); before using groupBy function as i am embedding external XML file but it gave me error.What will i need to do if i use values from query string??I mean i am using codeigniter and want to get different webcategories from my uri segment.I appreciate your help.Bless – Bilal Khalid Oct 15 '12 at 10:22
    • Just use `groupBy(file_get_contents('XML/products.xml'), "WebCategory")` fixed to code for namespace – Baba Oct 15 '12 at 10:26
    • @Baba.Brilliant .Bless you Baba.you are a magician. – Bilal Khalid Oct 15 '12 at 10:57
    • Coool .. glad i was able to help – Baba Oct 15 '12 at 11:09
    • @Babai need your help for this:http://stackoverflow.com/questions/13394651/updating-mysql-database-using-php-codeigniter-xml – Bilal Khalid Nov 15 '12 at 12:24