How do I display data from cURL into a table in PHP? - php

I need to get data from a website and display in a table in my website. I am getting the data correctly but now just need to add it to a table so that it is more legible.
This is the code for some of the information I am getting:
echo "<div style='font-weight: bold;'>getProvinces</div>";
$args = array();
$args["strSessionId"] = $session;
$result = CurlFunction($args, "getProvinces");
echo '<pre>';
var_dump($result);
echo '</pre>';
And
case "getProvinces":
{
$Url = "https://apitest.axxess.co.za/" .
"calls/rsapi/getProvinces.json";
$curl->setBasicAuthentication($Username, $Password);
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
$curl->setOpt(CURLOPT_SSL_VERIFYHOST,2);
$curl->get($Url, $d);
break;
}
I just need some help on how to get the above info into a table. And if possible only sections. I have attached an image to show what I am getting at the moment as well as which data I would like to have in a Table format.
Thanks
Link to complete code I am using: https://transfernow.net/923hx9h1f2er

Use json_decode to obtain an array representing your response data, and use a foreach loop to create key-value pairs inside a table for each data item.
The code could look like:
$jsoned = json_decode($result, true)
?>
<table>
<?php foreach ($jsoned as $k => $v) {?>
<tr><td> <?=$k?> </td><td> <?=$v?> </td></tr>
<?php } ?>
btw, <?= $var ?> is a shortcut for <? echo $var; ?>

First of all, convert your json response into an array by using json_decode() like:
// assuming `$jsonResponse` is your json response variable
$decoded = json_decode($jsonResponse,true); // here true will use return response in array format
Then, you can use your response check like:
if($decoded['intCode'] == 200){
print_r($decoded['arrProvinceId']); // this will return you your response in array
}
now, you can you foreach() loop with html whatever you need with $decoded['arrProvinceId'] result.

Related

Accessing JSON array data in PHP

I am trying to get some data from an external API. From the JSON data I want to retrieve some data as marked in the image.
I can access the "teams" data by using this code -
foreach( $data->data as $info ) {
echo '<li class="team-1">'. $info->teams[0].'</li>';
echo '<li class="team-2">'. $info->teams[1].'</li>';
};
But when I try to access data more deep from array of objects it doesn't work & gives me error -
foreach( $info->sites as $site) {
foreach( $site->odds as $odd) {
echo $odd->h2h[0];
}
}
So my question is what is the best way to loop through the data to access these arrays. I am using this code in Wordpress but I think it will be same as PHP.
You should access h2h directly from odds, since it's not an array, but an object
foreach( $info->sites as $site) {
echo $site->odds->h2h[0];
}
It looks like odds is an object rather than an array.
If h2h will always be the only property in odds then you can try:
foreach( $info->sites as $site) {
echo $site->odds->h2h[0];
}
Alternatively, if h2h will not always be the only property in odds, then you may want to try casting it into an array:
foreach( $info->sites as $site) {
foreach( (array)$site->odds as $odd) {
echo $odd[0];
}
}
You get this json from an external api and convert it using json_decode, right? If that is so, just use the second parameter "$assoc" and set it to true to not get an object, but an associative array that you can use like this:
$info = json_decode($api_answer,true);
if(isset($info['sites']['odds'])){
foreach($info['sites']['odds'] as $odd){
echo $odd['h2h'][0]
}
}

Loop through each element under "Data"

I'm using Kucoin's API to get a list of coins.
Here is the endpoint: https://api.kucoin.com/v1/market/open/coins
And here's my code:
$kucoin_coins = file_get_contents('https://api.kucoin.com/v1/market/open/coins');
$kucoin_coins = json_decode($kucoin_coins, true);
print_r($kucoin_coins);
I can see how to target one coin like so:
echo "name: " . $kucoin_coins['data'][0]['name'];
But I can't see how to loop through them.
How can I loop through each of the "coins" returned here? They are under the "data" part that is returned. I'm sorry, I'm just not seeing how to do it right now. Thank you!
You can loop through the decoded elements using the foreach command:
foreach ($kucoin_coins['data'] as $coin) {
//do your magic here.
}
But I usually prefer using json_decode($kucoin_coins) rather than the one for arrays. I believe this:
$item->attribute;
Is easier to write than this one:
$item['attribute'];
foreach($kucoin_coins['data'] as $data) {
echo $data['name']."\n";
}
You can loop through your data using foreach() like this
<?php
$kucoin_coins = file_get_contents('https://api.kucoin.com/v1/market/open/coins');
$kucoin_coins = json_decode($kucoin_coins, true);
print '<pre>';
print_r($kucoin_coins);
print '</pre>';
foreach($kucoin_coins['data'] as $key=>$value){
echo $value['name']. "<br/>";
}
?>
See DEMO: http://phpfiddle.org/main/code/q6kt-dctg

Getting nested array data from JSON

I am currently trying to retrieve some info from some json code in PHP. I have some of it achieved but I am trying to get a players name from a nested array and I am having a bit of trouble. This is the json info:
Sorry I had to post on pastebin as JSON wouldnt embed in the code box. ht
tps://pastebin.com/6RZAgKFa
I have pulled in the info like so:
$json_string2 = 'URL TO JSON DATA';
$jsondata2 = file_get_contents($json_string2);
$serverinfo2 = json_decode($jsondata2,true);
And I can view it by printing it.
Now I am trying to loop through it all to just retrieve names, I am trying the following:
foreach($serverinfo2 as $playerinfo) {
foreach($playerinfo as $playerstuff) {
echo $playerstuff;
}
}
But that returns the full array. I then add [name] to the end of echo $playerstuff and it returns a number 1 and a D. Not even relevant, and $playerstuff->name also doesn't work. Can anyone help me at all. Thanks :)
You can try this to achieve data from nested data from json :
<?php
$res = '[{"endpoint":"127.0.0.1","id":"2","identifiers":["STEAMID HERE","LICENSE HERE"],"name":"Deadpool","ping":"85"},{"endpoint":"127.0.0.1","id":"1","identifiers":["STEAMID HERE","LICENSE HERE"],"name":"Logan","ping":"73"}]';
$result = json_decode($res,true);
// echo '<pre>'; //print_r($result);
foreach ($result as $key => $value) {
// print_r($value);
echo $endpoint = 'endpoint : ' .$value['endpoint'];echo "<br/>";
echo $name = 'name :' .$value['name'];echo "<br/>";
}
?>

I want a query result using codeigniter

i have an array result. i want to print 2 rows(product data) in first page.
next 2 rows in second page and so on. if anybody knows this,please help me to solve it
my array
$data['product_list']
foreach($data['product_list'] as $dat)
{
echo $dat->prd_id;
echo $dat->prd_name;
}
You are doing a foreach loop on an associative array and then trying to access the the contents as objects by using ->. I can only given assumption of what you might be doing. If your array is already populated with a name and id like you have described in your foreach loop this is how you would access the contents in the loop:
foreach($data['product_list'] as $dat)
{
echo $dat['prd_id'];
echo $dat['prd_name'];
}
That is how you would print out the contents providing you had the data stored in your array like so:
$data['product_list'][0] = array('prd_id'=>'id0','prd_name'=>'name0');
$data['product_list'][1] = array('prd_id'=>'id1','prd_name'=>'name1');
$data['product_list'][2] = array('prd_id'=>'id2','prd_name'=>'name2');
Better you try with array_slice();
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>

super nested json array

Please help me pull this into a php variable... I have the following json decoded code:
"account_id":12345,
"name":"Example account",
"state":"active",
"balances":[
{
"currency":"USD",
"balance":390.50,
"incoming_pending_amount":635.30,
"outgoing_pending_amount":210.00,
"reserved_amount":0,
"disputed_amount":0,
"withdrawal_period":"daily",
"withdrawal_next_time":1370112217,
"withdrawal_bank_name":"WellsFargo XXXXX3102"
}
I want to bring out the balance into a php variable and am not sure how to.
when I do
foreach ($accounts as $a)
I can echo $a->account_id just fine - but when I try to echo $a->balances->balance it does not give me the 390.50 for the result...
any kind of help I can get on this is great appreciated!
In your json, balance is also an array so you need to have one more loop to traverse balances objects like:
foreach($accounts as $a){
echo $a->account_id;
echo $a->name;
echo $a->stat;
foreach($a->balances as $b) //loop if you want to print all balances
{
echo $b->currency;
echo $b->balance;
...
...
}
// echo $a->balances[0]->balance; //a single statement if you want to access just first balance object
}

Categories