我有一个脚本,它获取远程XML文件并显示带有产品数据的表。数据格式如下:
ID, name, price, months.
+++++++++++++++++++
1, Name1, $24, 12
2, Name2, $11, 24
2, Name2, $10, 36
3, Name3, $16, 12
2, Name2, $9, 48
4, Name4, $26, 12
+++++++++++++++++++
如您所见,Name2
与ID 2是相同的产品,但有不同月份和不同价格的选择。
我只需要显示一次相同的产品名称,并有一个下拉菜单,可以选择月份(因此价格将转到该菜单的值)
谁能帮我写一些PHP函数吗?它不应该使用mysql数据库,也许php数组…
非常感谢!
++++++++++++++++++++++++++++++++++++
非常感谢您的关注,真的很感谢!函数由Jan turokov看起来是一个解决方案,但我有困难实现它…下面是我的实际代码:
<?
try
{
$client = new soapclient("https://api.thesslstore.com/WBService.svc?wsdl", array('trace' => 1,'soap_version' => SOAP_1_1));
$parameters = array('objAuth'=>array("ResellerUserName"=>"user@domain.net","ResellerPassword"=>"password","PartnerCode"=>000000111));
// get the result, a native PHP type, such as an array or string
$result = $client->GetAllProductPrice($parameters);
$counter=count($result->GetAllProductPriceResult->AllProductPrice->AllProductPricing);
for ( $i=0; $i<$counter; $i+=1) {
printf("<tr><td> %s n", $result->GetAllProductPriceResult->AllProductPrice->AllProductPricing[$i]->NumberOfMonths ."</td>");
printf("<td> %s n", $result->GetAllProductPriceResult->AllProductPrice->AllProductPricing[$i]->Price ."</td>");
printf("<td> %s n", $result->GetAllProductPriceResult->AllProductPrice->AllProductPricing[$i]->ProductCode ."</td>");
printf("<td> %s n", $result->GetAllProductPriceResult->AllProductPrice->AllProductPricing[$i]->ProductName ."</td>");
}
catch (Exception $e)
{
printf("Error:sendSms: %sn",$e->__toString());
}
exit;
?>
下面是一个实例:http://webservice.ge/eus/TestPHPAPIProductDetails.php
谢谢你的帮助!
// prepare $data from your SOAP object
$result = $client->GetAllProductPrice($parameters);
$x = $result->GetAllProductPriceResult->AllProductPrice->AllProductPricing;
$data = array();
for ( $i=0; $i<count($x); $i++) {
$data[] = get_object_vars($x[$i]);
}
// transform to format that suits your purpose
$result = [];
foreach($data as $item) {
$key = $item["name"];
if(!isset($result[$key])) $result[$key] = array();
$result[$key][$item["price"]] = $item["months"];
}
// create your HTML code
ksort($result);
foreach($result as $key=>$item) {
ksort($item); // optional if you want options sorted asc
echo "<select name="$key">";
foreach($item as $value=>$text) echo "<option value="$value">$text</option>";
echo "</select>";
}