Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8748419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:31:21+00:00 2026-06-13T12:31:21+00:00

I am working on XML data extraction using php and came across two challenging

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:31:22+00:00Added an answer on June 13, 2026 at 12:31 pm

    You can use

    // Display XML with ProductRange Phones
    $list = groupBy(file_get_contents('1.xml'), "ProductRange", array("show" => true,"delimiter" => ";","name" => "Phones"));
    
    // Do NOT XML with ProductRange Phones and shwo the rest
    $list = groupBy(file_get_contents('1.xml'), "ProductRange", array("show" => false,"delimiter" => ";","name" => "Phones"));
    
    // Display XML with ProductRange TopRatedProducts
    $list = groupBy(file_get_contents('1.xml'), "ProductRange", array("show" => true,"delimiter" => ";","name" => "TopRatedProducts"));
    
    // Display XML with ProductRange only TopRatedProducts
    $list = groupBy(file_get_contents('1.xml'), "ProductRange", array("only" => "TopRatedProducts"));
    

    Your modified function

    function groupBy($xml, $categoryName, $options = array()) {
        $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;
            }
    
            if (empty($options)) {
                $category[(string) $attr->$categoryName][] = $attr;
            } else {
                if (isset($options['only'])) {
                    if ($attr->$categoryName == $options['only']) {
                        $category[(string) $attr->$categoryName][] = $attr;
                    }
                } else {
                    switch ($categoryName) {
                        case 'ProductRange' :
                            $name = strtolower($options['name']);
                            $parts = explode($options['delimiter'], strtolower($attr->$categoryName));
                            $condition = $options['show'] ? in_array($name, $parts) : ! in_array($name, $parts);
                            if ($condition) {
                                $category[(string) $attr->$categoryName][] = $attr;
                            }
                            break;
                    }
                }
            }
        }
    
        return $category;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on a Web Server which produces XML data to be used
I am working on a javascript project which involves parsing xml data. I have
I have been working with XML for a while now, but some things I
I am using the PHP SimpleXML way of working with XML files on my
I am binding the XML Data to treeview using XMLDataSource. Its working fime, but
I am trying to place a repeater within a repeater using xml data. I
I'm trying to return XML-data from a Python script using jQuery's .ajax: <html><head> <script
I have an issue with transforming an xml data by using xslt template. I
I'm working with XML data from an application where we get XML like this:
I am working on Highcharts application where chart will be displayed from XML data.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.