Need help with JSON and its interaction with the database. There is for example such JSON.
{ "response": { "trade_offers_received": [ { "tradeofferid": "1537239550", "accountid_other": 169029426 }, { "tradeofferid": "1537239438", "accountid_other": 169029426 }, { "tradeofferid": "1537239334", "accountid_other": 169029426 }, { "tradeofferid": "1537236218", "accountid_other": 169029426 } ] } } The task is to parse it in real time, adding one element to the database with each page refresh, until all elements are added ("tradeofferid"), this problem is solved, but with difficulty, and the main problem now is that I can’t delete those items from the database that are no longer in JSON, help me to make the right option, my code below:
$tradesinfo = json_decode(file_get_contents('http://api.steampowered.com/IEconService/GetTradeOffers/v1/?key='.$userinfo['keyuser'].'&get_received_offers=1&active_only=1'),true); $tradescount = count($tradesinfo['response']['trade_offers_received']); $stmtone = $pdo->prepare('SELECT * FROM trades'); $stmtone->execute(); $tradesinfo = $tradesinfo['response']['trade_offers_received']; //Тут все очень костыльно, работает не так, как хотелось бы $tradeinfo = $tradesinfo[$stmtone->rowCount()]; $steamids = reloads('[U:1:'.$tradeinfo['accountid_other'].']'); $userofferinfo = json_decode(file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.$usersinfo['keyuser'].'&steamids='.$steamids),true); $userinfo = $userofferinfo['response']['players'][0]; // Попытка удалять из БД, а удаляет все строки while($rowone = $stmtone->fetch(PDO::FETCH_ASSOC)){ $arrs = in_array($rowone['tradeid'],$tradesinfo); if($arrs != $rowone['tradeid']){ $stmt = $pdo->prepare('DELETE FROM trades WHERE tradeid = :tradeid'); $stmt->execute(Array('tradeid'=>$rowone['tradeid'])); } } $stmt = $pdo->prepare('SELECT * FROM trades WHERE tradeid = :tradeid'); $stmt->execute(Array('tradeid' => $tradeinfo['tradeofferid'])); if($stmt->rowCount() == 0){ $stmt = $pdo->prepare('INSERT INTO trades (tradeid,mysteamid,steamid,user,avatar,country,message) VALUES (:tradeid,:mysteamid,:steamid,:user,:avatar,:country,:message)'); $stmt->execute(Array('tradeid' => $tradeinfo['tradeofferid'],'mysteamid' => $steamid,'steamid' => $steamids,'user' => $userinfo['personaname'],'avatar' => $userinfo['avatarmedium'],'country' => $userinfo['loccountrycode'],'message' => $tradeinfo['message'])); }