I want to fetch data from different servers Database and I need to shown in one Centralised server. So I have written a SELECT query in different locations and I am trying to fetch in one Centralised Server.
I am getting a JSON Array response.
But I tried displaying it in a HTML Table format, but I am unable to get the proper result.
Here is my Centralised server PHP code:
<?php
$arr = array (
'http://example.com/pristatus/send-curl.php',
'http://example2.com/pristatus/send-curl.php'
);
for ($i =0; $i<count($arr); $i++)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $arr[$i]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
if($output!="error"){
$data = json_decode($output);
}
echo "<pre>";
print_r($data);
curl_close($ch);
}
?>
Output I am getting:
Array
(
[0] => stdClass Object
(
[id] => 9
[status] => OK
[batch] => 119677
[location] => Hyderabad
[createdDT] => 2015-06-19 20:40:05
)
)
Array
(
[0] => stdClass Object
(
[id] => 1
[status] => OK
[batch] => 56339
[location] => Mumbai
[createdDT] => 2015-06-19 20:40:05
)
[1] => stdClass Object
(
[id] => 2
[status] => OK
[batch] => 56339
[location] => Mumbai
[createdDT] => 2015-06-19 20:40:05
)
)
Please suggest me how I can display Json response in a Table format.
you need to use foreach loop and than get value by like
foreach($array as $val)
$val->property
Example :
echo '<table>';
foreach($data as $key) {
echo '<tr>';
echo '<td>'.$key->{'id'}.'<td>';
echo '<td>'.$key->{'status'}.'</td>';
echo '<td>'.$key->{'batch'}.'</td>';
echo '<td>'.$key->{'location'}.'</td>';
echo '<td>'.$key->{'createdDT'}.'</td>';
echo '</tr>';
}
echo '</table>';
Related
I have json data as shown in the picture below, how do I get highlighted value and change them?
Json Data
EDITED
I try to get the "furniture_id" using the code below...but fails...I don't know what else i can do to get the value
$test = '{"data":[{"owned_id":1,"furniture_id":1,"owned_name":"desk_123"},{"owned_id":2,"furniture_id":2,"owned_name":"chair_123"},{"owned_id":3,"furniture_id":4,"owned_name":"sofa_123"}]}';
foreach ($test as $key => $value) {
dd($furniture_id['id']);
}
$test = '{"data":[{"owned_id":1,"furniture_id":1,"owned_name":"desk_123"},{"owned_id":2,"furniture_id":2,"owned_name":"chair_123"},{"owned_id":3,"furniture_id":4,"owned_name":"sofa_123"}]}';
$response=json_decode($test);
foreach ($response->data as $key => $value) {
echo $value->furniture_id;
echo "<br>";
}
first you have to decode json data using json_encode method.Then if you print json decode response then you will be get an result of standard object
stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[owned_id] => 1
[furniture_id] => 1
[owned_name] => desk_123
)
[1] => stdClass Object
(
[owned_id] => 2
[furniture_id] => 2
[owned_name] => chair_123
)
[2] => stdClass Object
(
[owned_id] => 3
[furniture_id] => 4
[owned_name] => sofa_123
)
)
)
Updated
$test = '{"data":[{"owned_id":1,"furniture_id":1,"owned_name":"desk_123"},{"owned_id":2,"furniture_id":2,"owned_name":"chair_123"},{"owned_id":3,"furniture_id":4,"owned_name":"sofa_123"}]}';
$response=json_decode($test);
echo "<pre>";
print_r($response);
$newArray=[];
foreach ($response->data as $key => $value) {
$row=[];
$value->furniture_id= $value->furniture_id+1;
}
echo "<pre>";
print_r($response->data);
Now output will be
Array
(
[0] => stdClass Object
(
[owned_id] => 1
[furniture_id] => 2
[owned_name] => desk_123
)
[1] => stdClass Object
(
[owned_id] => 2
[furniture_id] => 3
[owned_name] => chair_123
)
[2] => stdClass Object
(
[owned_id] => 3
[furniture_id] => 5
[owned_name] => sofa_123
)
)
Try this code
$test = '{"data":[{"owned_id":1,"furniture_id":1,"owned_name":"desk_123"},{"owned_id":2,"furniture_id":2,"owned_name":"chair_123"},{"owned_id":3,"furniture_id":4,"owned_name":"sofa_123"}]}';
$test = json_decode($test, true);
//if you want to change value of furniture_id in index 0
$test['data'][0]['furniture_id'] = 111;
print_r($test); exit;
$test = '{"data":[{"owned_id":1,"furniture_id":1,"owned_name":"desk_123"},{"owned_id":2,"furniture_id":2,"owned_name":"chair_123"},{"owned_id":3,"furniture_id":4,"owned_name":"sofa_123"}]}';
this $test string itself is a json string.As json is similar to javascript object, there is no way to to store json as we can do in javascript.That's why we put json inside a quote in php and represent them as string.
what you can do is turn the json string into a php object using json_decode() and access them in foreach loop
foreach (json_decode($test)->data as $key) {
echo $key->furniture_id;
}
i am trying to select inserted data from orient db database and to echo it out ?
how can i do this using php ?
This is what i have done so far
require('OrientDB/OrientDB.php');
$db = new OrientDB('localhost', 2424);
$connected = $db->connect('root', 'hello');
$config = $db->DBOpen('tabe', 'root', 'hello');
if($connected)
{
$records = $db->query("select from Nation");
echo $records;
}
else
{
die('Server Error');
}
it just prints 'Array' on screen
i am running a Apache php server on windows machine.
how can i get the data from the databse and print in on the website.
when i use for each loop to print out the element
my script just time out
$records = $db->query("select from Nation");
foreach ($records as $v)
echo $v;
if i use print_r($records);
it gives the following output
Array ( [0] => OrientDBRecord Object ( [isParsed:OrientDBRecord:private] => [className:OrientDBRecord:private] => [type] => d [clusterID:OrientDBRecord:private] => 169 [recordPos:OrientDBRecord:private] => 0 [recordID:OrientDBRecord:private] => 169:0 [version] => 4 [content:OrientDBRecord:private] => Nation#in_partOf:%AQAAAAIAwQAAAAAAAAAAAMIAAAAAAAAAAA==;,Country_Name:"India",Iso_code:"IN" [data] => OrientDBData Object ( [data:OrientDBData:private] => Array ( ) [record:OrientDBData:private] => OrientDBRecord Object *RECURSION* ) ) [1] => OrientDBRecord Object ( [isParsed:OrientDBRecord:private] => [className:OrientDBRecord:private] => [type] => d [clusterID:OrientDBRecord:private] => 170 [recordPos:OrientDBRecord:private] => 0 [recordID:OrientDBRecord:private] => 170:0 [version] => 4 [content:OrientDBRecord:private] => Nation#Country_Name:"United States of America",Iso_code:"US" [data] => OrientDBData Object ( [data:OrientDBData:private] => Array ( ) [record:OrientDBData:private] => OrientDBRecord Object *RECURSION* ) ) [2] => OrientDBRecord Object ( [isParsed:OrientDBRecord:private] => [className:OrientDBRecord:private] => [type] => d [clusterID:OrientDBRecord:private] => 171 [recordPos:OrientDBRecord:private] => 0 [recordID:OrientDBRecord:private] => 171:0 [version] => 2 [content:OrientDBRecord:private] => Nation#Country_Name:"Canada",Iso_code:"CA" [data] => OrientDBData Object ( [data:OrientDBData:private] => Array ( ) [record:OrientDBData:private] => OrientDBRecord Object *RECURSION* ) ) )
how to convert it in to suitable form . like taking only names from it
this is my table
Better use PhpOrient Library it is the officially supported library by orient db
$client = new PhpOrient();
$client->hostname = 'localhost';
$client->port = 2424;
$client->username = 'your_username';
$client->password = 'your_password';
$client->connect();
$client->dbOpen('Your_db_name');
$query = "SELECT FROM NATION";
$result = $client->query($query);
$result = json_encode($result);
$result = json_decode($result);
foreach ($result as $row){
echo $row->oData->Country_Name;
echo $row->oData->Iso_code;
}
phporient : https://github.com/orientechnologies/PhpOrient.git
Print the result. Instead of echo
print_r($records);
In order to iterate over the result, either use foreach or for
First check if we have some results, using foreach as :
if(count($records) > 0){//check if we have a result
foreach($records as $value){//loop
print_r($value);
}
}
I am making a call to some url : http://example.com/Service.asmx/getTodaysDiscussionForum which is xml data please see screenshot :
$response = file_get_contents('http://103.1.115.87:100/Service.asmx/getTodaysDiscussionForum');
$response = new SimpleXMLElement($response);
print_r($response);exit;
it display following output :
SimpleXMLElement Object (
[Table1] => Array ( [0] => SimpleXMLElement Object (
[Id] => 1210 [Title] => Test Discussion, Dont Reply [CreatedDate] => 4/25/2014 10:42:49 AM
[Status] => Not Sent )
[1] => SimpleXMLElement Object (
[Id] => 1182 [Title] => Negotiation Skills discussion [CreatedDate] => 4/25/2014 7:47:51 AM
[Status] => Not Sent )
)
)
How can I store each data in a variables ?
I am new to this xml reading thanks in advance
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
<?php
foreach($response[0] as $data) {
$id = $data->Id;
//...
}
<?php
$requestUrl = 'https://example/sitemap.xml';
$response = file_get_contents($requestUrl);
$responseXml = simplexml_load_string($response);
foreach ($responseXml->Table1 as $Table1) {
echo $Table1->Title;
echo "<br>";
}
?>
some time small things in programming get giants. I am working on 2 dimensional array but I am unable to get what I need.
below is my array structure.
Array
(
[0] => Array
(
[0] => 16
[id] => 16
[1] => 1
[userid] => 1
[2] => abc#gmail.com
[email] => abc#gmail.com
[3] => dffsdf
[message] => dffsdf
[4] => 0
[status] => 0
)
[1] => Array
(
[0] => 17
[id] => 17
[1] => 1
[userid] => 1
[2] => xyz#gmail.comnnn
[email] => xyz#gmail.comnnn
[3] => dffsdfnnnnnnnnnnn
[message] => dffsdfnnnnnnnnnnn
[4] => 0
[status] => 0
)
)
what I am doing here is getting the messages for a user with some id. I am doing it like that
if($get_mails[0]['userid'] == $_GET['userid'])
{
$last_key = end(array_keys($get_mails));
echo '{"Messages":[';
foreach($get_mails as $key => $get_each_mail){
$company_name = $get_each_mail['company_name'];
$email_id = $get_each_mail['id'];
$email_body = $get_each_mail['message'];
}
echo '{"CompanyName":"'.$company_name.'","MessageID":"'.$email_id.'","MessageBody":"'.$email_body.'"';
if ($key == $last_key)
{
echo '}]}';
}else{
echo'},';
}
}
what I am unable to do is so funny that I need a loop for [0] in this line of code
if($get_mails[0]['userid'] == $_GET['userid'])
like
if($get_mails[i]['userid'] == $_GET['userid']) and it give me all the records against specific user.
here is what I want to get for a specific user
{"Messages":[{"CompanyName":"newtech","MessageID":"14","MessageBody":"hi how are you"},{"CompanyName":"newtech","MessageID":"15","MessageBody":"hi how are you"},{"CompanyName":"newtech","MessageID":"24","MessageBody":"asfasdfsdfsdfsdfsdfsdfsdfsd"}]}
respose like that, it will add more and more if more records would available against specific user.
Assuming $get_mails contains the Array you posted above (including company_name), you can write something like this:
$output = Array( "Messages" => Array() );
foreach( $get_mails as $k => $arr ) {
$t = Array();
$t['CompanyName'] = $arr['company_name'];
$t['MessageID'] = $arr['id'];
$t['MessageBody'] = $arr['message'];
$output['Messages'][] = $t;
}
echo json_encode( $output );
First you prepare an Array with the structure of your JSON. The syntax $array[] = a will append a to $array. json_encode( ... ) at the end will take care of turning it into valid JSON, even if one of your keys included a quote or other special character that is invalid in JSON.
I believe you only want to display messages from a certain user, and try to accomplish that with if($get_mails[0]['userid'] == $_GET['userid']). I recommend to change your SQL-query to something that accomplishes that, because the performance of your page will greatly increase if you try to crawl through all messages with the following code:
$output = Array( "Messages" => Array() );
foreach( $get_mails as $k => $arr ) {
if( $arr['user_id'] == $_GET['userid'] ) {
$t = Array();
$t['CompanyName'] = $arr['company_name'];
$t['MessageID'] = $arr['id'];
$t['MessageBody'] = $arr['message'];
$output['Messages'][] = $t;
}
}
echo json_encode( $output );
I am using the Google Analytics V3 PHP OAuht API. When using Simple.php in Google Analytics API Example the data are returned as PHP arrays. I am using the following call to get a more detailed answer to some specific data. It works fine.
$ids = "ga:xxxxxx";
$start_date = "2011-01-01";
$end_date = "2011-11-30";
$metrics = "ga:visits,ga:pageviews";
$dimensions = "ga:browser";
$optParams = array('dimensions' => $dimensions);
$data = $service->data_ga->get($ids,$start_date,$end_date,$metrics,$optParams);
Output of the Array is
Data
Array
(
[kind] => analytics#gaData
[id] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:xxxxxxx&dimensions=ga:browser&metrics=ga:visits,ga:pageviews&start-date=2011-01-01&end-date=2011-11-30&start-index=1&max-results=1000
[query] => Array
(
[start-date] => 2011-01-01
[end-date] => 2011-11-30
[ids] => ga:xxxxxxxx
[dimensions] => ga:browser
[metrics] => Array
(
[0] => ga:visits
[1] => ga:pageviews
)
[start-index] => 1
[max-results] => 1000
)
[itemsPerPage] => 1000
[totalResults] => 220
[selfLink] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:xxxxx&dimensions=ga:browser&metrics=ga:visits,ga:pageviews&start-date=2011-01-01&end-date=2011-11-30&start-index=1&max-results=1000
[profileInfo] => Array
(
[profileId] => xxxxx
[accountId] => xxxxx
[webPropertyId] => UA-xxxxxx-x
[internalWebPropertyId] => xxxxxxxxxx
[profileName] => xxxxx.com
[tableId] => ga:xxxxxxxx
)
[containsSampledData] =>
[columnHeaders] => Array
(
[0] => Array
(
[name] => ga:browser
[columnType] => DIMENSION
[dataType] => STRING
)
[1] => Array
(
[name] => ga:visits
[columnType] => METRIC
[dataType] => INTEGER
)
[2] => Array
(
[name] => ga:pageviews
[columnType] => METRIC
[dataType] => INTEGER
)
)
[totalsForAllResults] => Array
(
[ga:visits] => 36197
[ga:pageviews] => 123000
)
[rows] => Array
(
[0] => Array
(
[0] => (not set)
[1] => 459
[2] => 1237
)
[1] => Array
(
[0] => 12345
[1] => 3
[2] => 3
)
[2] => Array
(
[0] => 440955
[1] => 1
[2] => 1
)
[3] => Array
(
[0] => Alexa Toolbar
[1] => 1
[2] => 1
)
[4] => Array
(
[0] => Android Browser
[1] => 4177
[2] => 9896
)
....
The [Rows] Array has 219 entries.
Now the problem. I have spent the last week trying to parse this into an HTML table or anything that looks presentable. I have come close, but it seems this multi-dimensional array is beyond what I am able to handle. I am also trying to keep the solution flexible enough to handle more metrics or dimensions if they are added as well. I am self-taught PHP, so maybe I am missing a function or two that could make this easier. Thanks again for any hints, tips of ideas to make this work.
I got a bit further, but I does not fully format the way I want...maybe someone can see where I went wrong
$output = $service->data_ga->get($ids,$start_date,$end_date,$metrics,$optParams);
echo'<table style="text-align: left; width: 100%;" border="1" cellpadding="2"
cellspacing="2">
<tbody><tr>';
foreach ($output['columnHeaders'] as $header) {
print "<td>";
printf('%25s', $header['name']);
print "</td>";
}
print "</tr>";
foreach ($output['rows'] as $row) {
print "<td>";
foreach ($row as $column)
printf('%25s', $column);
print "</td>";
}
print "\n";
echo'
</tbody>
</table>';
I still can't seem to get the rows to display right.
To get you on your way use
$data [rows][$i][$j][columnHeaders] [0][name]
$data [rows][$i][$j][columnHeaders] [1][name]
and for rows use something like
$data [rows][0][1]
You will have to get the variable via increment with something like:
var =(count($data [columnHeaders]));
for ($i='0'; $i<=$var; $i++) {
echo '.$data [columnHeaders] [$i][name].';}
This should get you on your way towards building your table. Good Luck!
The issue is in your foreach for the table body rows. You appear to have missed the rows out. Wrap it in another loop to print out tr around the set of tds.
This is what I used for printing out a table of analytics data:
$data = $analytics->data_ga->get('ga:' . $profileId, $dateFrom, $dateTo, $metrics, array('dimensions' => $dimensions));
<table>
<thead>
<tr>
<?php
foreach ($data->columnHeaders as $header) {
$headerName = ucwords(preg_replace('/(\w+)([A-Z])/U', '\\1 \\2', str_replace('ga:', '', $header->name)));
print '<th>';
printf('%s', $headerName);
print '</th>';
}
?>
</tr>
</thead>
<tbody>
<?php
foreach ($data->rows as $row) {
print '<tr>';
foreach ($row as $cell) {
print '<td>';
printf('%s', $cell);
print '</td>';
}
print '</tr>';
}
?>
</tbody>
</table>