使用unique_array制作类别列表

时间:2014-03-19 13:08:17

标签: php xml arrays

我对PHP和XML有疑问。我有一个xml产品列表,其中包含30个项目,我想按类别链接它们,例如,如果我点击鞋子链接,鞋子列表将会出现。 这是我的代码:

<?php
$xml = simplexml_load_file("product.xml");        
echo " Here is my Product category : <br/>" ;
echo "<ol>";

$uniquecategory = array();
$i=0;

foreach ($xml->category as $ourcategory){
$uniquecategory[$i]=$ourcategory;
$i++;
}
$newcategory =array_unique($uniquecategory);

在此之前编码很好..

foreach ($newcategory as $anewcategory){
?>
<li><a href="displayproduct.php?category=<?php echo $anewcategory ?>">
<?php echo $anewcategory;
 ?>
</a></li>
<?php
}

echo "</ol>";
?>

我不确定它出错的地方..当我在浏览器上查看它时,它只显示&#34;这是我的产品类别&#34; 请帮帮我,谢谢。

这是xml示例数据

<product>
<category>Watches</category>
<product_id>0010</product_id>
<title>
Burberry Silver Dial Stainless Steel Mens Watch BU9300  </title>
<description>
Stainless steel case with a stainless steel bracelet. Fixed stainless steel bezel. 
Silver check stamped dial with silver-tone hands and index hour markers. Minute markers around the outer rim.
 Dial Type: Analog. Date display at the 3 o'clock position. Automatic movement. Scratch resistant sapphire crystal. 
 Pull / push crown. Skeleton case back. Case diameter: 42 mm. Round case shape. Band width: 22 mm. Deployment clasp.
 Water resistant at 50 meters / 165 feet. Functions: date, hour, minute, second. Casual watch style.
 Burberry Silver Dial Stainless Steel Mens Watch BU9300.
 </description>
<price>$813.93 </price>
<image_path></image_path>
</product>

<product>
<category>Shades</category>
<product_id>0101</product_id>
<title>
PERSOL - PO0714SM (54) </title>
<description>
 </description>
<price>$380.00 </price>
<image_path></image_path>
</product>

2 个答案:

答案 0 :(得分:0)

变量$ i中缺少

$ $。更改此行

$uniquecategory[i]=$ourcategory;

$uniquecategory[$i]=$ourcategory;

答案 1 :(得分:0)

使用此代码,这将有效。 您必须将所有产品详细信息包含在标记之类的基本标记内。

<?php
$xml = "<?xml version='1.0' encoding='UTF-8'?>
<products>
<product><category>Watches</category>
<product_id>0010</product_id>
<title>
Burberry Silver Dial Stainless Steel Mens Watch BU9300  </title>
<description>
Stainless steel case with a stainless steel bracelet. Fixed stainless steel bezel. 
Silver check stamped dial with silver-tone hands and index hour markers. Minute markers around the outer rim.
 Dial Type: Analog. Date display at the 3 o'clock position. Automatic movement. Scratch resistant sapphire crystal. 
 Pull / push crown. Skeleton case back. Case diameter: 42 mm. Round case shape. Band width: 22 mm. Deployment clasp.
 Water resistant at 50 meters / 165 feet. Functions: date, hour, minute, second. Casual watch style.
 Burberry Silver Dial Stainless Steel Mens Watch BU9300.
 </description>
<price>$813.93 </price>
<image_path></image_path>
</product>

<product>
<category>Shades</category>
<product_id>0101</product_id>
<title>PERSOL - PO0714SM (54) </title>
<description>ewer</description>
<price>234</price>
<image_path>ww</image_path>
</product>

<product>
<category>Shades</category>
<product_id>010d1</product_id>
<title>PERSOL - dPO0714SM (54) </title>
<description>ewer</description>
<price>23f4</price>
<image_path>wdw</image_path>
</product>
</products>";
$xml = simplexml_load_string($xml);

echo " Here is my Product category : <br/>" ;
echo "<ol>";

$uniquecategory = array();
$i=0;

foreach ($xml->product as $product){

$uniquecategory[$i]=$product->category;
$i++;
}
$newcategory =array_unique($uniquecategory);

foreach ($newcategory as $anewcategory){
?>
<li><a href="displayproduct.php?category=<?php echo $anewcategory ?>">
<?php echo $anewcategory;
 ?>
</a></li>
<?php
}

echo "</ol>";