Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "root";
- $dbname = "testing";
- $infoArray = new stdClass();
- $infoArray->defs = [98, 34, 101, 97, 138];
- $infoArray->fwds = [212, 25, 92];
- $infoArray->gk = [260, 74];
- $infoArray->mid = [16, 228, 409, 454, 453];
- $totalMoney = 100;
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- function getDataFromExternalApi ($url) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $server_output = curl_exec($ch);
- curl_close($ch);
- $data = json_decode($server_output);
- return $data;
- }
- function multiCurlReq($urlArray) {
- $nodes = $urlArray;
- $node_count = count($nodes);
- $curl_arr = array();
- $master = curl_multi_init();
- for($i = 0; $i < $node_count; $i++)
- {
- $url =$nodes[$i];
- $curl_arr[$i] = curl_init($url);
- curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
- curl_multi_add_handle($master, $curl_arr[$i]);
- }
- do {
- curl_multi_exec($master,$running);
- } while($running > 0);
- for($i = 0; $i < $node_count; $i++)
- {
- $results[] = curl_multi_getcontent ($curl_arr[$i]);
- }
- echo '<pre>';
- print_r($results);
- }
- $playerUrl = "https://fantasy.premierleague.com/drf/bootstrap";
- $allPlayerMetaData = getDataFromExternalApi($playerUrl);
- function seedPlayer($allPlayerMetaData, $con) {
- $detailUrlArray = array();
- $sql = "";
- if($allPlayerMetaData && $allPlayerMetaData->elements) {
- foreach ($allPlayerMetaData->elements as $key => $value) {
- $playerId = $value->id;
- $singlePlayerInfo = new stdClass();
- $singlePlayerInfo->player_cost = $value->now_cost ? ($value->now_cost/10) : 0;
- $singlePlayerInfo->player_points = $value->total_points ? $value->total_points: 0;
- $singlePlayerInfo->player_id = $playerId;
- $singlePlayerInfo = json_encode($singlePlayerInfo);
- $sqlSel = "select * from players where player_id = $playerId";
- $results = $con->query($sqlSel);
- if ($results->num_rows > 0) {
- $sql .= "UPDATE players set data = '$singlePlayerInfo' where player_id=$playerId;";
- }
- else {
- $sql .= "INSERT INTO players (data, player_id) VALUES ('$singlePlayerInfo', '$playerId');";
- }
- }
- if (mysqli_multi_query($con,$sql))
- {
- do
- {
- // Store first result set
- if ($result=mysqli_store_result($con)) {
- // Fetch one and one row
- while ($row=mysqli_fetch_row($result))
- {
- printf("%s\n",$row[0]);
- }
- // Free result set
- mysqli_free_result($result);
- }
- }
- while (mysqli_next_result($con));
- }
- mysqli_close($con);
- }
- }
- function calculateAvailableBalance($teamInfoObj, $con, $totalMoney){
- $sqlSel = "select * from players";
- $results = $con->query($sqlSel);
- $totalPlayerCost = 0;
- $availableBalance = 0;
- if ($results->num_rows > 0) {
- while($row = $results->fetch_assoc()) {
- $playerInfo = json_decode($row["data"]);
- $playerId = $playerInfo->player_id;
- foreach($teamInfoObj as $key => $value) {
- if(in_array($playerId, $value)){
- $totalPlayerCost += (float) $playerInfo->player_cost;
- }
- }
- }
- }
- $availableBalance = $totalMoney - $totalPlayerCost;
- return $availableBalance;
- }
- $availabLeBalance = calculateAvailableBalance($infoArray, $conn, $totalMoney);
- // seedPlayer($allPlayerMetaData, $conn);
- // $a = new stdClass();
- // $a->test = 'name';
- // $a->b = new stdClass();
- // $a->b->name = 'what';
- // $a->b->place = 'what';
- // $c = json_encode($a);
- // var_dump(json_decode($c));die();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement