0

I have script which fetches remote XML file and displays table with product data. data has following format:

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

+++++++++++++++++++

as you see Name2 with ID 2 are same product, but with choice of different months and different price.

I need to display same product name only once and have a drop-down menu for it with the month choice (so the price will go to that menu's value)

Can anybody help me writing some PHP function for it? It shouldn't use mysql database, maybe php arrays...

Thank you very much!

++++++++++++++++++++++++++++++++++++

Thanks so much for your attention, really appreciating this! Function by Jan Turoň looks as a solution, but I have trouble to implement it.. here's my actual code:

<? 
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;
?>

And here's the live example: http://webservice.ge/eus/TestPHPAPIProductDetails.php

Thanks for your help!

Besik
  • 43
  • 7

1 Answers1

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>";
}
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
  • The point is a bit unclear so I hope I caught it - see my updated code. It might not be the exact thing you need, but I believe you can put it together already. Good luck. – Jan Turoň Nov 17 '11 at 12:47
  • Thanks again, unfortunately i couldn't make it working. It says 'Fatal error: Cannot use object of type stdClass as array' for the line: $data[] = get_object_vars($x[$i]); and also 'Parse error: syntax error, unexpected '[' for that line: $result = []; But thanks anyway for your time and help! – Besik Nov 17 '11 at 13:59