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'])); } 
  • When you refresh the page, delete everything from the database for this sale and re-add those that are contained in JSON. As a solution. You can still not add items to the database until the entire list of items is formed. Use in this case ajax - ArchDemon
  • @ArchDemon, the problem is that deleting everything from the database is not an option, because full parsing is very demanding of resources and very rarely necessary, since this JSON is completely rarely updated, usually adding 2-3 elements, or decreasing - 100ROZH
  • Well, so what's the problem of deleting 5 records from the database and adding 2-3 instead of 5? I will not force you to delete all items at all, but only those that are currently involved in the sale (for example, a particular user) - ArchDemon
  • You can set the cron task, which will create a new json file once every 30 seconds using file_put_contents and a request to the database just let it check this file and update the table. - Bim Bam
  • Can you see an example? - 100ROZH

0