Hy,
I just cant figure it out how to get this to work.
How do I post the home>name, home>score to the database (the same for away) and send the status, rounds and datetime to the database.
But if there is a change in the json url than update the database and if there is a new match insert it.
this all needs to be done automatically.
[
{
"url":"http://www.gosugamers.net/counterstrike/tournaments/10423-starladder-cis-minor-championship-cologne-2016/2865-playoffs/10426-playoffs/matches/113497-worst-players-vs-team-empire",
"home":{
"name":"Worst Players",
"url":"/counterstrike/teams/15191-worst-players",
"country":"Russian Federation",
"rank":57,
"score":1
},
"away":{
"name":"Team Empire.",
"url":"/counterstrike/teams/14464-team-empire",
"country":"Russian Federation",
"rank":40,
"score":2
},
"status":"Complete",
"type":"counterstrike",
"rounds":"Best of 3",
"valueBet":true,
"datetime":1462024800
},
{
"url":"http://www.gosugamers.net/counterstrike/tournaments/10296-esports-championship-series-season-1/2810-online-phase/10298-north-american-league/matches/111367-team-liquid-cs-vs-counter-logic-gaming-cs",
"home":{
"name":"Team Liquid.CS",
"url":"/counterstrike/teams/10561-team-liquid-cs",
"country":"United States",
"rank":10
},
"away":{
"name":"Counter Logic Gaming.CS",
"url":"/counterstrike/teams/10569-counter-logic-gaming-cs",
"country":"United States",
"rank":39
},
"status":"Upcoming",
"type":"counterstrike",
"rounds":"Best of 3",
"valueBet":true,
"datetime":1462068000
}
]
I have tried this code
<?php
include("../../check.php");
$urljson = "http://localhost:5000/matches";
$json = file_get_contents($urljson);
$data = json_decode($json, TRUE);
$home = $data[0]['home'];
$away = $data[0]['away'];
$status = $data[0]['status'];
$type = $data[0]['rounds'];
$timestamp = $data[0]['datetime'];
foreach($data as $array) {
echo $home['name'];
echo '<br>';
echo $home['score'];
echo '<br>';
echo $away['name'];
echo '<br>';
echo $away['score'];
echo '<br>';
echo $status;
echo '<br>';
echo $type;
echo '<br>';
echo $timestamp;
echo '<br>';
echo '<br>';
}
?>
but this is what the webpage gave me (it showed mutiple times the same result)
ANOX
Notice: Undefined index: score in C:\xampp\htdocs\main\dashboard\backend\info.php on line 18
Worst Players
Notice: Undefined index: score in C:\xampp\htdocs\main\dashboard\backend\info.php on line 22
Upcoming
Best of 3
1462100400
I hope someone can help me with this beceause I am stuck for like 4 days now.
You get the same data all the time cause you are using the wrong variables inside the loop. Use the $array variable in your case:
foreach($data as $array) {
echo $array['name'];
echo '<br>';
echo $array['score'];
echo '<br>';
... etc.
}
Maybe it would be good to call the variable $row or $match instead of $array for better readability.
You get the "Notice: Undefined..." because there are some JSON nodes where there is no score. Check for score before using it with
if (isset($array['score'])) {
// use it inside if:
echo $array['score']
}
Here the corrected code:
<?php
include("../../check.php");
$urljson = "http://localhost:5000/matches";
$json = file_get_contents($urljson);
$data = json_decode($json, TRUE);
foreach($data as $match) {
echo $match['home']['name'];
echo '<br>';
if (isset($match['home']['score'])) {
echo $match['home']['score'];
echo '<br>';
}
echo $match['away']['name'];
echo '<br>';
if (isset($match['away']['score'])) {
echo $match['away']['score'];
echo '<br>';
}
echo $match['status'];
echo '<br>';
echo $match['rounds'];
echo '<br>';
echo $match['datetime'];
echo '<br>';
echo '<br>';
}
?>
Related
So I am trying to echo the value of a key if the key is present in the array. I've go this code at the moment:
<?php
$lingos = array(
"en"=>"en_US",
"en-gb"=>"en_GB",
"nl"=>"nl_NL",
"de"=>"de",
"da"=>"da",
"es"=>"es",
"ca"=>"es_MX",
"fr"=>"fr",
"it"=>"it",
"pt-pt"=>"pt_PT",
"no"=>"no",
"sv"=>"sv",
"fi"=>"fi",
"et"=>"en_GB",
"is"=>"en_GB",
"cs"=>"cs",
"pl"=>"pl",
"lv"=>"en_GB",
"lt"=>"lt",
"hu"=>"hu",
"ro"=>"ro",
"sr"=>"en_GB",
"hr"=>"en_GB",
"bg"=>"bg",
"el"=>"el",
"uk"=>"en_GB",
"ru"=>"ru",
"tr"=>"tr",
"ar"=>"ar",
"zh-hans"=>"zh_CN",
"zh-hant"=>"zh_TW",
"ja"=>"ja",
"ko"=>"ko",
"id"=>"in",
"ms"=>"ms",
"th"=>"th",
"vi"=>"vi",
"pt-br"=>"pt_PT"
);
// foreach($lingos as $lingo => $x_lingo) {
// echo "Key=" . $lingo . ", Value=" . $x_lingo;
// echo "<br>";
// }
$wmpl_langcode = ICL_LANGUAGE_CODE;
echo $wmpl_langcode;
if (array_key_exists($wmpl_langcode, $lingos)) {
echo $lingos[1];
} else {
echo "not found";
}
?>
Thing is, $lingos[1] is not returning anything. What am I doing wrong?
Presumably $wmpl_langcode is something like en as you are checking for it with array_key_exists so use that as the index:
$wmpl_langcode = ICL_LANGUAGE_CODE;
echo $wmpl_langcode;
if (array_key_exists($wmpl_langcode, $lingos)) {
echo $lingos[$wmpl_langcode];
} else {
echo "not found";
}
Or simpler:
echo isset($lingos[$wmpl_langcode]) ? $lingos[$wmpl_langcode] : "not found";
//PHP 7+
echo $lingos[$wmpl_langcode] ?? "not found";
I'm working on a site which gives metrics of site, it uses API of gtmetrix. So i want to remove a specific data from the coming result but I just dont know how to do it. Some help would be appreciated!
<?php
require_once('service.inc.php');
$test = new Services_WTF_Test("email", "api_key");
$url_to_test = "https://google.me/";
echo "Testing $url_to_test\n";
$testid = $test->test(array(
'url' => $url_to_test
));
if ($testid) {
echo "Test started with $testid\n";
}
else {
die("Test failed: " . $test->error() . "\n");
}
echo "Waiting for test to finish\n";
$test->get_results();
if ($test->error()) {
die($test->error());
}
$testid = $test->get_test_id();
echo "Test completed succesfully with ID $testid\n'<br>";
$results = $test->results();
if ($results): ?>
<?php
foreach($results as $result => $data): ?>
<strong><?php $ukey = strtoupper($result);
echo $ukey; ?>:</strong>
<?php echo $data; ?><br><br>
<?php endforeach;
endif;
?>
The Output Is:
FIRST_CONTENTFUL_PAINT_TIME: 1495
PAGE_ELEMENTS: 44
REPORT_URL: https://gtmetrix.com/reports/google.me/BFylJNX3
REDIRECT_DURATION: 0
FIRST_PAINT_TIME: 1495
DOM_CONTENT_LOADED_DURATION:
DOM_CONTENT_LOADED_TIME: 1908
I want to remove the 3rd data from the api REPORT_URL:
You can skip REPORT_URL in foreach
$ukey = strtoupper( $result );
if( $ukey != 'REPORT_URL' ) {
echo '<strong>' . $ukey . '</strong>';
echo $data . '<br><br>';
}
I have problem with fetching json array that i send with post by android app.
PHP code :
<?php
$properties_json = json_decode($_POST['property'], true);
foreach ($properties_json->properties as $pro_element) {
echo $pro_element->property_id . '<br/>';
echo $pro_element->property_name . '<br/>';
echo $pro_element->property_value . '---------';
}
JSON array that i post :
{
"properties": [
{
"property_id": "654",
"property_name": "Gender",
"property_value": "Male"
},
{
"property_id": "655",
"property_name": "Name",
"property_value": "Amin"
},
{
"property_id": "656",
"property_name": "Mobile",
"property_value": "654-451-456"
}
]
}
Error :
Invalid argument supplied for foreach()
enter image description here
json_decode($_POST['property'], true); return an array. In your code you're using it as an object. Here is the proper way :
<?php
$properties_json = json_decode($_POST['property'], true);
foreach ($properties_json['properties'] as $pro_element) {
echo $pro_element['property_id'] . '<br/>';
echo $pro_element['property_name'] . '<br/>';
echo $pro_element['property_value'] . '---------';
}
EDIT:
You can check if $_POST['property'] exist and is not null.
<?php
if(isset($_POST['property'])){
if($_POST['property'] != null and $_POST['property'] != ""){
$properties_json = json_decode($_POST['property'], true);
foreach ($properties_json['properties'] as $pro_element) {
echo $pro_element['property_id'] . '<br/>';
echo $pro_element['property_name'] . '<br/>';
echo $pro_element['property_value'] . '---------';
}
}else{
echo "POST['property'] is empty";
}
}else{
echo "Missing POST['property']";
}
You reference $properties_json->properties as if it were an object, but you converted your JSON to an array by doing json_decode($_POST['property'], true); (see the documentation of json_decode)
You either need to change the true to false or do
foreach ($properties_json['properties'] as $pro_element) {
This is the JSON data I get from our ticket-system. I would like to parse it with PHP and save the data in a database to create statistics and a dashboard so everyone can see how many tickets are open and the latest closed tickets.
I can read some of the data but not everything.
{
"address":"belgium",
"workers":{
"peter":{
"worker":"peter",
"open_close_time":"45.6 T/h",
"closed_tickets":841,
"open_tickets":7,
"last_checkin":1498768133,
"days_too_late":0
},
"mark":{
"worker":"mark",
"open_close_time":"45.9 T/h",
"closed_tickets":764,
"open_tickets":2,
"last_checkin":1498768189,
"days_too_late":0
},
"walter":{
"worker":"walter",
"open_close_time":"20.0 T/h",
"closed_tickets":595,
"open_tickets":4,
"last_checkin":1498767862,
"days_too_late":0
}
},
"total_tickets":2213,
"tickets":[
{
"id":2906444760,
"client":"297",
"processed":0
},
{
"id":2260,
"client":"121",
"processed":0
},
{
"id":2424,
"client":"45",
"processed":0
}
],
"last_closed_tickets":[
{
"id":2259,
"client":"341",
"closed_on":"2017-06-25T10:11:00.000Z"
},
{
"id":2258,
"client":"48",
"closed_on":"2017-06-20T18:37:03.000Z"
}
],
"settings":{
"address":"belgium",
"email":"",
"daily_stats":0
},
"open_close_time":"161.1 T/h",
"avgopen_close_time":123298,
"ticket_time":"27.1 T/h",
"stats":{
"time":1498768200087,
"newest_ticket":1498768189000,
"closed_tickets":2200,
"open_tickets":13,
"active_workers":3
},
"avg_paid_tickets":64.55,
"avg_afterservice_tickets":35.45
}
This is the PHP code I tried to get the names of the worker but this doesn't work.
<?php
$string = file_get_contents("example.json");
$json = json_decode($string, true);
echo $json['address'];
foreach($json->workers->new as $entry) {
echo $entry->worker;
}
?>
If I try it like here below it works but then I've got to change the code everytime another employee starts.
echo $json['workers']['mark']['closed_tickets'];
<?php
$string = file_get_contents("example.json");
$json = json_decode($string, true);
foreach($json['workers'] as $entry){
echo $entry['worker'] . " has " . $entry['open_tickets'] . " tickets" . "\n";
}
?>
I am trying to get the names of my friends using FB Graph API with this call :
$friends = file_get_contents('https://graph.facebook.com/me/friendsaccess_token='.$session["access_token"]);
echo "Friends : $friends\n";
This gives me a list of the form :
{"data":[{"name":"ABC XYZ","id":"12212839"},{"name":"PQR GHI","id":"5004678"}]}
I want to be able to store only the NAMES in an array. How do I use $friends to get the names ? Something like $friends['name'] doesn't seem to work.
Please help.
Thank you.
$friends = json_decode($friends);
foreach($friends['data'] as $friend)
{
echo $friend['name'];
}
The return is a json object, you need to decode it. Although I strongly urge you to use an SDK such as http://github.com/facebook/php-sdk/
If this doesn't work try:
$friends = json_decode($friends);
foreach($friends->data as $friend)
{
echo $friend->name;
}
Here is what I did to get the posts information.. crude but it works. Note that the shares likes comments and reactions are one level deeper in the JSON object
$posts = json_decode($output); // from FB Graph v2.8 API call
foreach($posts->data as $post)
{
echo "MESSAGE: ", $post->message, "<br>";
echo "NAME: ", $post->name, "<br>";
echo "TYPE: ", $post->type, "<br>";
echo "ID: ", $post->id, "<br>";
echo "LINK: ", $post->link, "<br>";
echo "PERMALINK: ", $post->permalink_url, "<br>";
echo "CREATED: ", $post->created_time, "<br>";
if($post->shares->count == "") { $shares = "0"; } else { $shares = $post->shares->count; }
echo "SHARES: ", $shares, "<br>";
if($post->reactions->summary->total_count == "") { $reactions = "0"; } else { $reactions = $post->reactions->summary->total_count; }
echo "REACTIONS: ", $reactions, "<br>";
if($post->comments->summary->total_count == "") { $comments = "0"; } else { $comments = $post->comments->summary->total_count; }
echo "COMMENTS: ", $comments, "<br>";
if($post->likes->summary->total_count == "") { $likes = "0"; } else { $likes = $post->likes->summary->total_count; }
echo "LIKES: ", $likes, "<br>";
echo "<br><br>";
}