0

私はphpを使用したXMLデータ抽出に取り組んでおり、対処すべき2つの困難な問題に遭遇しました...私はこれを理解しようとしていましたが、理解できませんでした。

次のようなデータを含む可能性のあるXMLの「ProductRange」フィールドに基づいてXMLファイルからすべての製品範囲を抽出しているカテゴリリストページがあります。

電話;TopRatedProducts (製品が両方の製品範囲に属することを意味します:電話とTopRatedProducts)

電話;アクセサリ(製品が両方の製品範囲に属することを意味します:電話とアクセサリ)

PSP; TopRatedProducts(製品が両方の製品範囲に属することを意味します:電話とTopRatedProducts)

XMLに、セミコロンで識別される2つの異なる製品範囲に属する製品があります。

質問#1:セミコロン(;)が含まれる製品範囲(ProductRange)を非表示にして、電話や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;
   }
     ?>

注: このコードは、次のような出力を提供します。

  • 電話;TopRatedProducts
  • 電話;付属品
  • TopRatedProducts
  • 付属品
  • PSP; TopRatedProducts

しかし、私が望む出力は次のようになります。

  • 電話
  • 付属品
  • PSP
  • TopRatedProducts

関連する商品ごとに別々に表示できるように、セミコロンの商品範囲を区切る必要があります。

質問2:

両方のProductRangesに属する製品をリストするにはどうすればよいですか。つまり、ProductRange = "Phones; TopRatedProducts"を2つの別個の製品範囲として扱い、 "Phones"、 "TopRatedProducts"などの下にあり、このmysqlのように動作するすべての製品をリストします。 ProductRanges:

* "select * from Products where ProductRange ='Phones'"; *

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

コード :

//このコードは、製品範囲に基づいてすべての製品を一覧表示します

    $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です:

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>

それに応じて結果をフィルタリングするにはどうすればよいですか?

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 に答える