2012-02-08 76 views
-1

首先,我有一个来自多个外部xml源的多个产品的首页(此刻有24个不同的源),每个源有1个产品。有些属性具有不同的名称为同样的事情,例如:从多个xmlfeeds通过php获取基于属性值的XML元素

一例喂

<products> 
<product> 
<productID>4943</productID> 
<name>Inbouwspot</name> 
<price currency="EUR">199.00</price> 
<productURL>http://www.example.com</productURL> 
<imageURL>http://static.example.com/45928-1.jpg</imageURL> 
<description><![CDATA[Incl. BTW!! Verkrijgbaar in een voordeelset.]]></description> 
<categories> 
<category path="Aanbiedingen">Aanbiedingen</category> 
<category path="Projectverlichting">Projectverlichting</category> 
</categories> 
<additional> 
<field name="fromPrice">395.00</field> 
<field name="discount">196.00</field> 
<field name="deliveryCosts"></field> 
<field name="deliveryTime">3-5 dagen</field> 
</additional> 
</product> 

另一示例喂

<products> 
<product> 
<productID>1344</productID> 
<name>Displex screen</name> 
<price currency="EUR">6.95</price> 
<productURL>http://www.voorbeeld.nl</productURL> 
<imageURL>http://www.voorbeeld.nl/images/1543.jpg</imageURL> 
<description><![CDATA[Touchscreen voor je smartphone?]]></description> 
<categories> 
</categories> 
<additional> 
<field name="price_from">14.95</field> 
<field name="price_shipping">Gratis!</field> 
<field name="soldout">0</field> 
<field name="startdate">2012-02-07 12:00:00</field> 
<field name="enddate">2012-02-08 12:00:00</field> 
</additional> 
</product> 
</products> 

的不同的进料(产品)我提出在这样的循环内:

<?php 
    // get xml file contents 
    $xmlurl = get_post_meta(get_the_ID(), 'plus_xmlfeed', true); 
    if (!empty($xmlurl)){ 
    $xml = simplexml_load_file($xmlurl); 
    } 
    $xml->product->name = substr($xml->product->name, 0, 30).'...'; 
    $desc = substr($xml->product->name, 0, 50).''; 
?> 

    <li class="span3"> 
     <div class="thumbnail"> 
     <a href="<?php echo $xml->product->productURL ;?>"> 
      <center><img src="<?php echo get_template_directory_uri(); ?>/img/logos/<?php echo get_post_meta(get_the_ID(), 'plus_logos', true); ?>.gif"/></center> 
      <center><img class="mainimg" alt="" src="<?php echo $xml->product->imageURL ;?>"/></center> 
     </a> 
     <div class="caption"> 
      <h5><a href="<?php echo $xml->product->productURL ;?>"><?php echo $xml->product->name; ?></a></h5> 
      <div class="maindesc half clearfix"><div class="prijs">&euro; <?php echo $xml->product->price ;?></div><strike style="color: rgb(248, 148, 6);"> 
      <?php 
      foreach ($xml->product->additional->field as $field) { 
           switch((string) $field['name']) { 
            case 'price_advice': 
             echo '&euro; ' .$field. ''; 
             break; 
            case 'fromPrice': 
             echo '&euro; ' .$field. ''; 
             break; 
            case 'oldPrice': 
             echo '&euro; ' .$field. ''; 
             break; 
            case 'from_price': 
             echo '&euro; ' .$field. ''; 
             break; 
            case 'adviesprijs': 
             echo '&euro; ' .$field. ''; 
             break; 
            case 'advice_price': 
             echo '&euro; ' .$field. ''; 
             break; 
            case 'Fromprice': 
             echo '&euro; ' .$field. ''; 
             break; 
            case 'recommendedPrice': 
             echo '&euro; ' .$field. ''; 
             break; 
            } 
           } 
      ?> 
      </strike> 
      <p style="color: rgb(153, 153, 153);"><?php echo $desc; ?></p></div> 
      <div class="btn-group bot"><div class="pull-right"> </div><a class="btn btn-primary" href="<?php echo $xml->product->productURL ;?>">Kopen</a> <a class="btn" href="#<?php echo 'modal_'.get_the_ID();?>">Info</a></div> 
     </div> 
     </div> 

    </li> 
<?php /*end loop*/ ?> 

我使用wordpress在帖子中输入提要,并将其显示为变量($ xmlurl)。我想输入具体的属性(从价格from_price等),以及在一个帖子中,所以它不必循环槽的所有选项来找到正确的饲料。

我知道上述可以更快,更高效,我希望有人能帮助我。我读了一些关于xpath的内容,我尝试了但没有成功。

回答

0

自己的解决方案:

我找到了解决办法自己

  <?php 
       $FromPrices= get_post_meta(get_the_ID(), 'plus_fromprices', true); 
       if (!empty($FromPrices)){ 
       foreach ($xml->xpath('//field[@name="'.$FromPrices.'"]') as $fromprice) { 
        echo '&euro; '.$fromprice; 
        } 
       } 
      ?>