-2

I have to make a web app that gets information from my database, that gets its info from an API). Then I have to show items under certain conditions.

But when I try to add the data from the API, I got a strange message:

Notice: Trying to get property of non-object in c:\xampp\htdocs\IMP03\inleveropdracht3\libs\php\function.php on line 21
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\IMP03\inleveropdracht3\libs\php\function.php on line 21

Here is my PHP code:

<?php

require_once 'settings.php';
$mysqli = mysqli_connect($db_host, $db_user, $db_password, $db_database);
if (mysqli_connect_error()) {
    echo mysqli_connect_error($mysqli) . "We are not able to connect to the online database";
}
jsondecode($mysqli);
if (isset($_GET['club']) && !empty($_GET['club'])) {
    jsondecode($mysqli);
} else if (isset($_GET['thuisPoint']) && !empty($_GET['thuisPoint']) && ($_GET['uitPoint']) && ($_GET['uitPoint'])) {
    updatePoints($mysqli);
} else {
    getWedstrijd($mysqli);
}

function jsondecode($mysqli) {
    $apiLink = 'http://docent.cmi.hr.nl/moora/imp03/api/wedstrijden?club=';
//    $club = $_GET['club'];
    $data = json_decode(file_get_contents($apiLink . "Ajax"));
    foreach ($data->data as $info) {
        $thuisClub = $info->homeClub;
        $uitClub = $info->awayClub;
        addWestrijden($mysqli, $thuisClub, $uitClub);
    }
}

//querys
function addWestrijden($mysqli, $thuisClub, $uitClub) {
    $query = "INSERT INTO wedstrijd VALUES(null, '$thuisClub', '$uitClub')";
    $resultAddWedstrijd = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
    getWedstrijd($mysqli);
}

function getWedstrijd($mysqli) {
    $query = "SELECT * FROM wedstrijd ORDER BY thuisClub DESC";
    $resultGetWedstijd = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
    while ($result = mysqli_fetch_assoc($resultGetWedstijd)) {
        $rows [] = $result;
    }
    header("Content-Type: application/json");
    echo json_encode($rows);
    exit;
}

function updatePoints($mysqli) {
    $id = $_GET['id'];
    $thuisPoints = $_GET['thuisPoint'];
    $uitPoints = $_GET['uitPoint'];
    $query = "UPDATE wedstrijd "
            . "SET thuisPunt = '$thuisPoints', uitPunt = '$uitPoints') "
            . "WHERE id = '$id'";
    mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
    getWedstrijd($mysqli);
}

I did modify it a bit so it would add data from the API. I really would appreciate it if someone could help me.

Tzar
  • 1,761
  • 2
  • 14
  • 21
Justin
  • 37
  • 1
  • 4
  • possible duplicate of [Invalid argument supplied for foreach()](http://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach) – Marcin Orlowski Apr 25 '14 at 10:43

1 Answers1

0

Change your foreach to:

foreach ($data as $data => $info) 
gbestard
  • 1,177
  • 13
  • 29