我有一个脚本,它可以获取远程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
+++++++++++++++++++
如您所见,ID {2} Name2
是相同的产品,但可以选择不同的月份和不同的价格。
我只需要显示一次相同的产品名称,并为其选择下拉菜单(因此价格将转到该菜单的值)
有人可以帮我写一些PHP函数吗?它不应该使用mysql数据库,也许是php数组...
非常感谢!
++++++++++++++++++++++++++++++++++++
非常感谢您的关注,非常感谢! JanTotoň的功能看起来像一个解决方案,但我很难实现它..这是我的实际代码:
<?
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: %s\n",$e->__toString());
}
exit;
?>
以下是实例:http://webservice.ge/eus/TestPHPAPIProductDetails.php
感谢您的帮助!
答案 0 :(得分:0)
// 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>";
}