I'm trying to show table from json but failed, what's wrong with this:
$nip=$_POST['nip'];
$sql = "select satker,shift_description,nip FROM jamkerja
inner join master_shift on master_shift.shiftno=jamkerja.shiftno
inner join tr_jamkerjahdr on jamkerja.id_jamkerja=tr_jamkerjahdr.id_jamkerja
inner join tr_jamkerjamember on tr_jamkerjamember.trno=tr_jamkerjahdr.trno
where nip='$nip' ";
$result = $con->query($sql);
$data = array();
while($row = mysqli_fetch_assoc($result)){
$data[] = $row;
}
print $data;
And this is the table:
$json = $data;
$json_decoded = json_decode($json);
foreach($json_decoded as $data12){
echo '<tr>';
echo '<td>'.$data12[satker].'</td>';
echo '<td>'.$data12[shift_description].'</td>';
echo '<td>'.$data12[nip].'</td>';
echo '</tr>';
}
You don't need the $json_decode.Just loop through your array $data.
remove the :
$json = $data;
$json_decoded = json_decode($json);
and modify the foreach loop, adding the table tag.Also add single qoutes to your array indexes:
echo '<table>';
foreach($data as $data12){
echo '<tr>';
echo '<td>'.$data12['satker'].'</td>';
echo '<td>'.$data12['shift_description'].'</td>';
echo '<td>'.$data12['nip'].'</td>';
echo '</tr>';
}
echo '</table>';
Not sure I fully understand your question.
But let me try to help you. I think your issue is around
$json = $data;
$json_decoded = json_decode($json);
json_decode() is not needed here, the $data that you are assigning to $json is a type array. With the code you shared, I don't see any need for Json, you should directly loop through your $data.
Hope that helps.
Related
how can I output this array into html table?
for each row I would like to output it like this, within the foreach;
echo "<td>".$lat."</td><td>".$long."</td>";
as per example on https://developer.here.com/documentation/routing-waypoints/topics/quick-start-simple-car.html
I have tried the code
$api_url = "https://wse.api.here.com/2/findsequence.json?start=Berlin-Main-Station;52.52282,13.37011&destination1=East-Side-Gallery;52.50341,13.44429&destination2=Olympiastadion;52.51293,13.24021&end=HERE-Berlin-Campus;52.53066,13.38511&mode=fastest;car&app_id=ID&app_code=CODE";
$api_response = file_get_contents($api_url);
$api_response_decoded = json_decode($api_response, true);
foreach($api_response_decoded as $api_response_decoded_row){
print_r($api_response_decoded_row[0][waypoints]);
}
and also tried
print_r($api_response_decoded_row[0][waypoints][id]);
and also tried
echo($api_response_decoded_row[0][waypoints][id]);
and also tried
implode($api_response_decoded_row[0][waypoints][id]);
Here's one way you could do it if the comments didn't already help you enough.
foreach($api_response_decoded as $api_response_decoded_rows){
foreach ($api_response_decoded_rows[0]['waypoints'] as $waypoint) {
$html = '
<td>'.$waypoint['lat'].'</td>
<td>'.$waypoint['lng'].'</td>
';
echo $html;
}
}
Thanks to commenters and answerers. In case it helps someone else, full working code is therefore;
$api_url = "https://wse.api.here.com/2/findsequence.json?start=Berlin-Main-Station;52.52282,13.37011&destination1=East-Side-Gallery;52.50341,13.44429&destination2=Olympiastadion;52.51293,13.24021&end=HERE-Berlin-Campus;52.53066,13.38511&mode=fastest;car&app_id=ID&app_code=CODE";
$api_response = file_get_contents($api_url);
$api_response_decoded = json_decode($api_response, true);
echo "<table>";
foreach($api_response_decoded as $api_response_decoded_rows){
foreach ($api_response_decoded_rows[0]['waypoints'] as $waypoint) {
$html = '<tr><td>'.$waypoint['sequence'].'</td><td>'.$waypoint['id'].'</td><td>'.$waypoint['lat'].'</td><td>'.$waypoint['lng'].'</td></tr>';
echo $html;
}
}
echo "</table>";
Hi I'm trying get a json from fixer.io and then for each rates echo it but cant get it to work.
the code are
<?php
function usd(){
echo 'HEJ test';
$fixer_access_key = my_access_key;
$url= 'https://data.fixer.io/api/latest?access_key=' . $fixer_access_key;
echo $url;
$json = file_get_contents($url);
$data = json_decode($json);
echo $url . "<br>";
echo 'printing json foreach <br>';
foreach($data as $obj){
echo '...';
$prefix = $obj;
echo $prefix;
echo '<br>';}
echo 'done printing json foreach';
}
usd(); ?>
and the result are:
https://data.fixer.io/api/latest?access_key=my_fixer_key
printing json foreach
done printing json foreach
instead of
$data = json_decode($json);
use
$data = json_decode($json, true);
This should allow foreacha to works - however you will only see first level of json object keys (not nested ones). The second parameter of json_decode change result from object to array.
You will also need to change foreach - to following: foreach($data as $key => $obj) and inside it echo $obj to echo $key;.
Here is simplified working example.
ALTERNATIVE SOLUTION
If working foreach is not your goal but rather pretty printed json, then instead use following code:
$json_string = json_encode($data, JSON_PRETTY_PRINT);
echo $json_string;
How can I parse this JSON, which is supposed to display the items a user has in their Steam inventory.
I have tried this:
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);
echo $data;
It returns the same as just visiting the link. I can't get anything like this to work either:
$id = $json->type;
echo $type;
This is how to get type
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);
foreach ($json->rgDescriptions as $mydata)
{
echo $mydata->type;
}
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);
echo $data;
you are echoing $data that is your input (so you see the same as opening the link directly). To see if the json_decode is working fine you should print $json.
So instead of
echo $data;
use
echo '<pre>'
print_r($json);
echo '</pre>';
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);
Now $json has 2 objects.
you can access like.
$json->rgInventory;
$json->success;
if you want to fetch all data from $json->rgInventory;
foreach($json->rgInventory as $e){
//var_dump($e);
echo $e->id;
echo $e->classid;
echo $e->instanceid;
}
etc.
I need help with fetching only attraction data,
i have this php script on my website:
<?php $json_string = 'http://framecreators.nl/efteling/data.php'; $jsondata file_get_content($json_string); $json_o=json_decode(utf8_encode($data)); $attractie = $json_o['Id']['Type']; echo $attractie ?>
and this json data:
http://framecreators.nl/efteling/data.php
I need to convert only a Id and type, ff somebody can help me.
To access the data you're looking for, you'll need to do something like this:
foreach ($json_o['AttractionInfo'] as $a) {
echo $a['Id']; //or whatever you're planning to do with this data
ech0 $a['Type'];
}
<?php
$json_data = file_get_contents('http://framecreators.nl/efteling/data.php');
$data = json_decode($json_data);
$array = [];
foreach($data->AttractionInfo as $item) {
$array['id'][] = $item->Id;
$array['type'][] = $item->Type;
}
echo "<pre>";
print_r($array);
echo "</pre>";
$url = "http://framecreators.nl/efteling/data.php";
$content = file_get_contents($url);
$data = json_decode($content,TRUE);
$array = $data['AttractionInfo'];
foreach($array as $r)
{
echo "ID->".$r['Id'];
echo '';
echo "Type->".$r['Type'];
echo '';
}
$url = "http://framecreators.nl/efteling/data.php";
$content = file_get_contents($url);
$data = json_decode($content,TRUE);
$array = $data['AttractionInfo'];
if(is_array($array))
{
foreach($array as $r)
{
echo $r['Id'];
echo $r['Type'];
}
}
I'm really stuck trying to resolve what should be quite simple.
I Have this
<?php
$json = json_decode('{
"33540116":
{"person":
{"name":"John", "age":"36"}},
"33541502":
{"person":
{"name":"Jack", "age":"23"}}
}
');
$id = array('33540116', '33541502');
foreach($id as $id) {
echo $json->$id->person->{'name'}. '<br />';
echo $json->$id->person->{'age'}. '<br />';
}
?>
So the code is decoding a json string then using foreach to echo each result.
This json file is rather large and I'm only interested in certain records that match the id's stored in a mysql table.
To do this I have replaced the id array string above with mysql select statement.
<?php
$json = json_decode('{
"33540116":
{"person":
{"name":"John", "age":"36"}},
"33541502":
{"person":
{"name":"Jack", "age":"23"}}
}
');
$result = mysql_query("SELECT id FROM people");
$row = mysql_fetch_array($result);
$id = array($row['id']);
foreach($id as $id) {
echo $json->$id->person->{'name'}. '<br />';
echo $json->$id->person->{'age'}. '<br />';
}
?>
Although this works, it only gives me 1 result.
What I really need is to loop through the results.
Unfortunately I don't know how to construct a while loop together with foreach.
I will greatly appreciate your assistance.
UPDATE (extra question)
Thanks everyone. You have helped me solve the problem.
However, I have another question that relates to this matter.
I mentioned above that I merely wanted to echo the results.
But this isn't exactly true.
What I really want to do is update the same mysql table with the results retreived from the json file.
I have a table called people with fields id, name and age.
How can I update this table with these results?
Thanks again.
mysql_fetch_array only fetches one row at a time. You can use a while loop to continue fetching rows. The mysql_fetch_array function returns false once the whole result set has been fetched, so that will cause the while loop to terminate as desired.
Also, I removed the foreach loop on $ids. Since there will only be one element in the array it's unnecessary to put the code in a loop.
<?php
$json = json_decode('{
"33540116":
{"person":
{"name":"John", "age":"36"}},
"33541502":
{"person":
{"name":"Jack", "age":"23"}}
}
');
$result = mysql_query("SELECT id FROM people");
while ( ( $row = mysql_fetch_array($result) ) ) {
$id = $row['id'];
echo $json->$id->person->{'name'}. '<br />';
echo $json->$id->person->{'age'}. '<br />';
}
?>
Try this:
while($row = mysql_fetch_array($result)) {
$id = array($row['id']);
foreach($id as $id) {
echo $json->$id->person->{'name'}. '<br />';
echo $json->$id->person->{'age'}. '<br />';
}
}
You are using this :
$row = mysql_fetch_array($result);
This will only fetch one row from the database.
If you want to fetch more than one row, you have to call mysql_fetch_array() in a loop :
while ($row = mysql_fetch_array($result)) {
// Work with the current row
}
In your case, you'd have something like this :
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
echo $json->$id->person->{'name'}. '<br />';
echo $json->$id->person->{'age'}. '<br />';
}
You do not need foreach loop the while is just enough
$result = mysql_query("SELECT id FROM people");
$id = array($row['id']);
while($row = mysql_fetch_array($result))
{
echo $json->$row['id']->person->{'name'}. '<br />';
echo $json->$row['id']->person->{'age'}. '<br />';
}
You have a couple of issues. Firstly, you shouldn't be using foreach($id as $id) since you are using the same variable for both. Instead it should be foreach($ids as $id). Secondly, you can get a list of the ids and echo out the correct json values as follows
<?php
$json = json_decode('{
"33540116":
{"person":
{"name":"John", "age":"36"}},
"33541502":
{"person":
{"name":"Jack", "age":"23"}}
}
');
$result = mysql_query("SELECT id FROM people");
$ids = array();
while($row = mysql_fetch_array($result)) {
$ids[] = $row['id'];
}
foreach($ids as $id) {
echo $json->$id->person->{'name'}. '<br />';
echo $json->$id->person->{'age'}. '<br />';
}
?>
Hopefully that will solve it