1

I am trying to get the data from an API and save it to a MySQL database. The problem is when I want to echo the data to check if it gets all the values I'm getting this error:

Notice: Trying to get property of non-object

My JSON data looks like this:

{"AttractionInfo":[{"Id":"sprookjesbos","Type":"Attraction","MapLocation":"1","State":"open","StateColor":"green","WaitingTime":0,"StatePercentage":0},{"Id":"zanggelukkig","Type":"Show","MapLocation":".","State":"gesloten","StateColor":"clear"},

I think this is because it is in an array. Because when I use this bit of code it gives no errors but I have to define the index of the array. What is the best way to loop through my data and put it in variables?

<?php

    //Get content
    $url = "https://eftelingapi.herokuapp.com/attractions";
    $data = json_decode(file_get_contents($url));

    //Fetch data
    $attractieId = $data->AttractionInfo[0]->Id;
    $attractieType = $data->AttractionInfo[0]->Type;
    $attractieStatus = $data->AttractionInfo[0]->State;
    $attractieStatusKleur = $data->AttractionInfo[0]->StateColor;
    $attractieWachttijd = $data->AttractionInfo[0]->WaitingTime;
    $attractieStatusPercentage = $data->AttractionInfo[0]->StatePercentage;

?>

I tried to find some solutions but all they use is a foreach loop. But i don't think that'll work right. Can anyone help me in the good direction or tell me how I might possibly fix this? I am not very experienced so any advice is welcome. Thanks in advance.

Update code:

require 'database-connection.php';
//Get content
$url = "https://eftelingapi.herokuapp.com/attractions";
$data = json_decode(file_get_contents($url));

//Fetch data
$attractieId = isset($data->AttractionInfo->Id);
$attractieType = isset($data->AttractionInfo->Type);
$attractieStatus = isset($data->AttractionInfo->State);
$attractieStatusKleur = isset($data->AttractionInfo->StateColor);
$attractieWachttijd = isset($data->AttractionInfo->WaitingTime);
$attractieStatusPercentage = isset($data->AttractionInfo->StatePercentage);

$sql =  "INSERT INTO attracties (attractieId, attractieType, attractieStatus, attractieStatusKleur, attractieWachttijd, attractieStatusPercentage) 
VALUES ('$attractieId', '$attractieType', '$attractieStatus', '$attractieStatusKleur', '$attractieWachttijd', '$attractieStatusPercentage')";
if ($db->query($sql) === TRUE) {
    echo "success";
} else {
    echo "Error: " . $sql . "<br>" . $db->error;
}

It says 'success' but when I look into my database it inserted only empty data. I need to add all the attractions to my database so not just one row. So I need to loop through my data.

6
  • 2
    It looks like not all objects in the array have all the same variables. You will need to wrap each assignment with an isset() to avoid the error. You can do a quick debug with print_r($data); as well, and see which seem to be missing which will throw that no property error. Commented Dec 6, 2017 at 15:31
  • When I wrapped the isset() around the assignments it gave no errors anymore. But when I try to echo the variables it shows nothing. Do you know why that might be? @Randall Commented Dec 6, 2017 at 15:36
  • Well, if there is no property, there is no variable data. So it should echo out nothing. I would hope. You could set the non-set variable to something for debugging purpose though: $test = (isset($data->AttractionInfo[0]->StatePercentage) ? $data->AttractionInfo[0]->StatePercentage : 'OH NO!' ); Commented Dec 6, 2017 at 15:38
  • 1
    can you please show how you are trying to echo it and how you are trying to insert to your mysql? do you need to insert only the first row or all of them ? Commented Dec 6, 2017 at 15:38
  • do you need to write exactly $attractieId or you can write just $Id Commented Dec 6, 2017 at 15:40

2 Answers 2

2

try this...

$content = json_decode(file_get_contents($url));

foreach($content->AttractionInfo as $data ){
      $id      = $data->Id;
      $type    = $data->Type;
      $map     = $data->MapLocation;
      $state   = $data->State;
      $color   = $data->StateColor;
      if(!empty($data->WaitingTime)) {
      $time = $data->WaitingTime;
      }
     if(!empty($data->StatePercentage)) {
       $percent = $data->StatePercentage;
     }

  //persist your data into DB....
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this piece of code worked for me! It now saves every attraction to my database
0

I think you need something like this:

    $json = '{"AttractionInfo":[{"Id":"sprookjesbos","Type":"Attraction","MapLocation":"1","State":"open","StateColor":"green","WaitingTime":0,"StatePercentage":0},{"Id":"zanggelukkig","Type":"Show","MapLocation":".","State":"gesloten","StateColor":"clear"}]}';
    $arr = json_decode($json);

     foreach ($arr->AttractionInfo as $key => $attraction) {
       foreach($attraction as $key=> $value) {
          print_r($key.' - '.$value.'<br>');
          $$key = $value;
       }
    }

echo '<br>';
echo $Id; // it will be last item/attraction id.

We can improve this code. Just say where and how do you want to use it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.