doupoji3856 2015-05-25 22:48 采纳率: 100%
浏览 153

php解析多个json文件,循环并在表中显示它

I have a script that parse json files multiple times and show data in a table.

The file to be parsed is composed by a string and than a filename.json that is made of the 2 value tag you want to parse.

Ex. www.........../idr-gold.json (where idr stands for Indonesia currency).

The countries are 155 so I have ID from 1 to 155 that stands for 155 differ tags.

So what I did is a loop that every time is run add +1 to $x, so $x is inserted into a script that find the correspondence between ID and countryName and countryCurrency so that every loop run I compose the url with the missing tag for 155 times. Than the script continues parsing Json and than returns a table.

The problem are 2: 1) I would like that every line of the table is shown after file .json is parsed (so loop $x = 1, parse www.....idr-gold.json ---> returns a table line with data; than $x = 2, www......usd-gold.json and so on...). I've tried moving script parts but nothing worked.

2) The time I need to parse is really high... is a metter of my script or is cross-domain parse that is slow? it seems like I don't "close" or "empty" the script every run so that the data keep on adding making the load heavier. (About this, I've read that it might be a cross-server data transfert problem, so my solution would be the 1st problem. If I show line by line composing table and not all the table at the end of page run, I'll have a php file that will show data avery 1sec x 155 times, in spite of having a page that takes 2 mins (1sec x 155 times) before showing you anything.

So here is the code:

<body>


<?php
// var declaration (here I have ex. $italyID = 7 and $italyCurrency = itl)
include 'var-declaration/var-declaration.php';

echo "<table class='db-table' width='1000'>";
echo "<tr>
<th width='14%'>Buy Currency</th>
<th width='14%'>Sell Currency</th>
<th width='10%'>Sell Rate</th>
<th width='12%'>Sell Amount</th>
<th width='15%'>Seller Name</th>
<th width='35%'>Link to Page</th>
</tr>";

// LOOP da 0 a 155
$x = 0; 
do{
// TEST    echo "The number is: $x <br>";
$iscountryID = ("$country".$x."");

$currencyVarName = ""; 
if ($iscountryID == 0) {
    $currencyVarName = $goldCurrency;
    } elseif ($iscountryID == 1) {
    $currencyVarName = $polandCurrency;
    } elseif ($iscountryID == 2) {
    $currencyVarName = $russiaCurrency;
    } elseif ($iscountryID == 3) {
    $currencyVarName = $germanyCurrency;
    } elseif ($iscountryID == 4) {
    $currencyVarName = $franceCurrency;
    } elseif ($iscountryID == 5) {
    $currencyVarName = $spainCurrency;
    } elseif ($iscountryID == 6) {
    $currencyVarName = $unitedKingdomCurrency;
    } elseif ($iscountryID == 7) {
    $currencyVarName = $italyCurrency;
    ... (up to 155)
    } elseif ($iscountryID == 154) {
    $currencyVarName = $bahrainCurrency;
    } elseif ($iscountryID == 155) {
    $currencyVarName = $kwaitCurrency;
    };

// if Currency name it isn't null
if ($currencyVarName != NULL) {
$urlLoopBuy =  strtolower("....exchange/".$currencyVarName."-gold.json");
$jsonLoopBuy = file_get_contents($urlLoopBuy);
$outLoopBuy = json_decode($jsonLoopBuy, true);

//json data parse
$buycurrency = $outLoopBuy['buy']['currency'];
$sellcurrency = $outLoopBuy['sell']['currency'];
$sellrate = $outLoopBuy['offer'][0]['rate'];
$sellamount = $outLoopBuy['offer'][0]['amount'];
$sellername = $outLoopBuy['offer'][0]['seller']['name'];

// table content
echo "<table class='db-table'  width='1000'>";
echo "<tr>
<td width='14%'>".$buycurrency."</td>
<td width='14%'>".$sellcurrency."</td>
<td width='10%'>".$sellrate."</td>
<td width='12%'>".$sellamount."</td>
<td width='15%'>".$sellername."</td>
<td width='35%'>".$urlLoopBuy."</td>
</tr></table>";

} else { // if currency name is NULL
$buycurrency = "record problem";
$sellcurrency = "record problem";
$sellrate = "record problem";
$sellamount = "record problem";
$sellername = "record problem";

//add +1 to loop
$x++;

};
} while ($x <= 155);


// Link to Page
$linktopage = "...monetarymarket.html?buyerCurrencyId=".$buyercountryID."&sellerCurrencyId=".$sellercountryID."";

?> 
</body>

That's it. Thanks.

Alberto

As requested here is a json file example (idr-gold.json).

{"buy":
{"currency":"IDR","country":"Indonesia"},
"sell":{"currency":"GOLD","country":"Global"},
"offer":[{"amount":0.01,"rate":0.0931,
"seller":
{"id":7181,"type":"citizen","name":"Revenge Hero"}},{"amount":500,"rate":0.0931,"seller":
{"id":13,"type":"citizen","name":"China Org"}},{"amount":30.18999,"rate":0.0932,"seller":
{"id":479821,"type":"citizen","name":"GGininDer"}},{"amount":2305,"rate":0.0999,"seller":
{"id":2019,"type":"citizen","name":"rockbein"}},{"amount":808.43,"rate":0.1,"seller":
{"id":8622,"type":"citizen","name":"haarp"}},{"amount":6703.03,"rate":0.1199,"seller":
{"id":7481,"type":"citizen","name":"edulin"}},{"amount":1,"rate":1,"seller":
{"id":479821,"type":"citizen","name":"GGininDer"}},{"amount":1,"rate":1,"seller":
{"id":144942,"type":"citizen","name":"TW_Jimmy"}}],
"load":1.5037939548492}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?