0

我正在使用 php 进行 XML 数据提取,遇到了两个需要处理的具有挑战性的问题......我一直在尝试解决这个问题,但无法解决。

我有类别列表页面,其中我根据我的 XML 中的“ProductRange”字段从我的 XML 文件中提取所有产品范围,其中可能包含如下数据:

Phones;TopRatedProducts (表示产品将同时属于两个产品范围:Phones & TopRatedProducts)

电话;配件(意味着产品将属于两个产品范围:电话和配件)

PSP;TopRatedProducts(意味着产品将属于两个产品范围:电话和 TopRatedProducts)

我的 XML 中有属于两个不同产品范围的产品,用分号标识。

问题#1:如何隐藏其中包含分号(;) 的产品范围 (ProductRange),使其不会与其他产品范围重复出现,例如 Phones 和 TopRatedProducts 等?

这是列出所有类别的 PHP 代码:

产品范围页面代码:

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

注意: 这段代码给了我这样的输出:

  • 电话;顶级产品
  • 电话;配件
  • 顶级产品
  • 配件
  • PSP;顶级产品

但我想要的输出应该是这样的:

  • 电话
  • 配件
  • 游戏机
  • 顶级产品

我需要分隔分号产品范围,以便我可以为相关产品单独显示它们。

问题2:

如何列出属于两个 ProductRanges 的我的产品,即我想将 ProductRange="Phones;TopRatedProducts" 视为两个单独的产品范围,以列出所有属于“Phones”、“TopRatedProducts”等的产品,并像这样的 mysql产品范围:

*"从 ProductRange='Phones' 的产品中选择 *";*

*"select * from products where ProductRange='TopRatedProducts'";* ?

编码 :

// 此代码根据 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();
          }
   }
   }

      ?> 

这是我的 XML:

产品.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>

我不知道如何相应地过滤我的结果?

4

1 回答 1

0

您可以使用

// 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"));

您修改后的功能

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;
}
于 2012-10-22T12:30:33.913 回答