我正在使用yahoo货币转换器API转换数据库上显示的每种产品的货币。页面上显示该货币。
功能一
function currencyConverter($currency_from,$currency_to,$currency_input){
$yql_base_url = "http://query.yahooapis.com/v1/public/yql";
$yql_query = 'select * from yahoo.finance.xchange where pair in ("'.$currency_from.$currency_to.'")';
$yql_query_url = $yql_base_url . "?q=" . urlencode($yql_query);
$yql_query_url .= "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
$yql_session = curl_init($yql_query_url);
curl_setopt($yql_session, CURLOPT_RETURNTRANSFER,true);
$yqlexec = curl_exec($yql_session);
$yql_json = json_decode($yqlexec,true);
$currency_output = (float) $currency_input*$yql_json['query']['results']['rate']['Rate'];
return $currency_output;
}
功能二
function getProducts($currency)
{
if ($result = $connection->query("SELECT `name`, `price` FROM `products`")) {
$product_count = mysqli_num_rows($result);
if ($product_count >0) {
$output = "";
while ($row = mysqli_fetch_assoc($result)) {
$price = currencyConverter("USD", $currency, $row['item_price']);
$output = $output."<div class='item'><h3 class='product-name'>".$row['item_name']."</h3><h3 class='product-price'>".$currency." ".$price."</h3></div></div>";
}
echo $output;
mysqli_free_result($result);
} else {
echo "nope";
}
} else {
echo "failed";
}
}
产品价格以美元保存在数据库中。问题是它需要很长时间来转换每个价格。有没有更好的方法呢?
答案 0 :(得分:1)
尝试这些:
var resolver = json.ContractResolver as DefaultContractResolver ?? new DefaultContractResolver();
resolver.NamingStrategy = new CustomNamingStrategy();
json.ContractResolver = resolver;
答案 1 :(得分:0)
现在让它工作得更快。
if ($result = $connection->query("SELECT `name`, `price` FROM `products`")) {
$product_count = mysqli_num_rows($result);
if ($product_count >0) {
$output = "";
// get current currency rate
$exchange_rate = currencyConverter("USD", $currency, "1");
while ($row = mysqli_fetch_assoc($result)) {
$price = $row['item_price'] * $exchange_rate;
$output = $output."<div class='item'><h3 class='product-name'>".$row['item_name']."</h3><h3 class='product-price'>".$currency." ".$price."</h3></div></div>";
}
echo $output;
mysqli_free_result($result);
} else {
echo "nope";
}
} else {
echo "failed";
}