我想從使用PHP我的XML文件中提取數據。我想根據我的XML文件中的WebCategory
得到唯一的ProductRange
。但是下面編寫的PHP代碼會生成重複/重複的結果。我不知道我在哪裏犯錯!下面是代碼:
XML代碼:
<?xml version="1.0" standalone="yes"?>
<Rows>
<Row Code="10000" Name="HTC Wildfire S-A510E " ProductRange="HTC" ProductSubRange="Wildfire" 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="10001" Name="HTC Wildfire" ProductRange="HTC" ProductSubRange="Wildfire" 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="10002" Name="Samsung Galaxy S3" ProductRange="Samsung" ProductSubRange="Galaxy" 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="10003" Name="Samsung Galaxy S2" ProductRange="Samsung" ProductSubRange="Galaxy" 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="10004" Name="Samsung Galaxy S1" ProductRange="Samsung" ProductSubRange="Galaxy" 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="10005" Name="Samsung Galaxy Tabloid" ProductRange="Samsung" ProductSubRange="Galaxy Tabloids" WebCategory="Gadgets" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" />
<Row Code="10006" Name="Apple Ipad 3" ProductRange="Apple" ProductSubRange="Tabloids" WebCategory="Gadgets" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" />
<Row Code="10007" Name="Apple Iphone 4S" ProductRange="Apple" ProductSubRange="Iphone" 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="10008" Name="Apple Iphone 3G" ProductRange="Apple" ProductSubRange="Iphone" 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="10009" Name="Miscrosoft XBOX 360 Elite" ProductRange="Microsoft" ProductSubRange="XBOX" 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="10010" Name="Sony Playstation 4" ProductRange="Sony" ProductSubRange="Playstation" 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="10011" Name="Sony PSP Go" ProductRange="Microsoft" ProductSubRange="PSP" 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="Sony Ericsson" ProductSubRange="Satio Series" 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="10013" Name="TomTom Go Live Gl2" ProductRange="TomTom" ProductSubRange="Go Live" WebCategory="Navigation" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" />
</Rows>
我想基於WebCategory
從我的XML文件ProductRange
獲得獨一無二的。但是下面編寫的PHP代碼會生成重複/重複的結果。我不知道我在哪裏犯錯!
PHP代碼:
<?
$xml = simplexml_load_string(file_get_contents('XML/products.xml'));
$prifix = '/categories/listings/' ;
$cat=array();
foreach ($xml as $row) {
$attr = $row->attributes();
if (!in_array((string)$attr->WebCategory, $cat)){
printf('<li>%s</li>', anchor($prifix . $attr->Code, $attr->ProductRange));
$cat[] = (string)$attr->WebCategory;
}
}
?>
請注意: 我想基於給定WebCategory
例如,我想告訴根據自己Webcategory這樣選擇SQL查詢中的所有ProductRange提取ProductRange
:
"select ProductRange from XML where WebCategory='Mobiles'"
它可以給我不同的「ProductRange」(不重複的結果)基於這樣的XML:
HTC的
三星
iPhone
等等......我盡我所能,但未能產生使用上述編碼方法基於獨特的「ProductRange」。
請糾正我在哪裏我錯了,請引導我在哪裏我需要做出改變,以獲得獨特的ProductRange
如上所述。
怎麼樣XPath查詢? – JvdBerg
該XML無效。你似乎錯過了'行'的開始標籤 – Quentin
@Quentin對不起。我忘了在xml中添加,而write.i已經按照你所提到的那樣進行了編輯。有什麼辦法可以避免記錄中的重複? –