【发布时间】:2015-12-27 05:21:56
【问题描述】:
我正在使用 php 进行 XML 数据提取,遇到了两个需要处理的具有挑战性的问题...我一直在尝试解决这个问题,但无法解决。
我有一个类别列表页面,我在其中根据我的 XML 中的“ProductRange”字段从我的 XML 文件中提取所有产品范围,其中可能包含如下数据:
Phones;TopRatedProducts(表示产品将属于两个产品范围:Phones 和 TopRatedProducts)
Phones;Accessories(表示产品将属于两个产品范围:Phones & Accessories)
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;顶级产品
但是我想要的输出应该是这样的:
- 电话
- 配件
- PSP
- 顶级产品
我需要分隔分号产品范围,以便我可以为相关产品单独显示它们。
问题 2:
如何列出属于这两个 ProductRanges 的产品,即我想将 ProductRange="Phones;TopRatedProducts" 视为两个单独的产品范围,以列出属于“Phones”、“TopRatedProducts”等的所有产品并表现得像这样此类 ProductRanges 的 mysql :
*"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>
我不知道如何相应地过滤我的结果?
【问题讨论】:
-
至少有一个地方您正在关闭并立即重新打开服务器标签。
-
见 .. 问题是太长了...让它射击并直接输入您预期的输入和输出 ..例如您添加了两次
groupBy???我确定我戴着那个功能 -
@Asad 我已经修改了代码。我希望这会让你更清楚。bless
-
已经解决了 2 小时前你还想要什么?
-
@Baba 谢谢Baba。感谢你的帮助。祝福