Optimize tips for many loops in php?

I am working on this code, putting it together here and there, when it came and works, and the result is VERY MESSY!

I just need advice, what should I do to reduce the number of loops, or maybe there are any loops that you see that shouldn't be needed?

any advice from the code below is welcome.

if (isset($_POST['refresh-history'])):
  $order_id = $_POST['id'];
  $order = $database->get_results('SELECT * FROM `orders` WHERE `order_id`='.$order_id);
  $matches = $database->get_results('SELECT `match_id` FROM `matches` WHERE `order_id`='.$order_id);
  foreach ($order as $o):
    $loluser = $o->loluser;
    $region = $o->region;
    $date_created = $o->date_created;
    $date_completed = $o->date_completed;
  endforeach;
  $api->setRegion($region);
  $matchlistapi = $api->matchlist();
  $matchapi = $api->match();
  $matchlist = $matchlistapi->matchlist($loluser, "RANKED_SOLO_5x5", "SEASON2015", null, null, null, $date_created, $date_completed);
  if ($matchlist->totalGames !== 0):
    foreach ($matchlist as $key):
      $gameIds[] = $key->matchId;
    endforeach;
    $arr_matches = object2array($matches);
    foreach ($arr_matches as $id) {
      $dbMatches[] = (int)$id->match_id;
    }
    $new_array = array_diff($gameIds, $dbMatches);

    foreach ($new_array as $matchId):
      $games[] = $matchapi->match($matchId, false);
    endforeach;
    foreach ($games as $game):
      // Store Games in DB;
    endforeach;
    $_SESSION['api_success'] = "Success: Games Synced to Order.";
  else:
    $_SESSION['error_msg'] = "Error 23: Unable to Find Games.";
  endif;
endif;

To be clear, I DO NOT NEED AN ANSWER! Just PUSH in the right direction, I can go from there. :)

+4
source share
4 answers

Explanation

You can replace the following code

foreach ($order as $o):
    $loluser = $o->loluser;
    $region = $o->region;
    $date_created = $o->date_created;
    $date_completed = $o->date_completed;
endforeach;

from

//get the last order.
$order = end($orders);

//set the information.
$loluser = ($order === false) ? '' : $order->loluser;
$region = ($order === false) ? '' : $order->region;
$date_created = ($order === false) ? '' : $order->date_created;
$date_completed = ($order === false) ? '' : $order->date_completed;

. -, foreach . . -, , script.

, , . , .

.

$new_array = array_diff($gameIds, $dbMatches);

foreach ($new_array as $matchId):
    $games[] = $matchapi->match($matchId, false);
endforeach;

foreach (array_diff($gameIds, $dbMatches) as $matchId):
    $games[] = $matchapi->match($matchId, false);
endforeach;

script :

<?php
if (isset($_POST['refresh-history'])) {
    $order_id = $_POST['id'];
    $orders = $database->get_results('SELECT * FROM `orders` WHERE `order_id` = '.$order_id);
    $matches = $database->get_results('SELECT `match_id` FROM `matches` WHERE `order_id` = '.$order_id);

    //get the last order.
    $order = end($orders);

    //set the information.
    $loluser = ($order === false) ? '' : $order->loluser;
    $region = ($order === false) ? '' : $order->region;
    $date_created = ($order === false) ? '' : $order->date_created;
    $date_completed = ($order === false) ? '' : $order->date_completed;

    $api->setRegion($region);
    $matchlistapi = $api->matchlist();
    $matchapi = $api->match();
    $matchlist = $matchlistapi->matchlist($loluser, "RANKED_SOLO_5x5", "SEASON2015", null, null, null, $date_created, $date_completed);

    //check if a game is available.
    if ($matchlist->totalGames > 0) {

        //initialize the vars.
        $matchIds = array();
        $matchIdsDB = array();

        //collect all match ids from api.
        foreach ($matchlist as $match) {
            $matchIds[] = (int) $match->matchId;
        }

        //collect all match ids from database.
        foreach (object2array($matches) as $match) {
            $matchIdsDB[] = (int) $match->match_id;
        }

        //run through all missing matches.
        foreach (array_diff($matchIds, $matchIdsDB) as $match) {
             $game = $matchapi->match($match, false);

             //store game in database or create a big query to create all in one.
        }

        $_SESSION['api_success'] = "Success: Games Synced to Order.";
    } else {
        $_SESSION['error_msg'] = "Error 23: Unable to Find Games.";
    }
}

, !

+9

:

  • , :

    'SELECT * FROM `orders` WHERE `order_id` = '.$order_id);
    

, order_id , .

  • , , API

      $matchlistapi->matchlist(...)
    
  • ,

  • ,

, . , .

matches

ALTER TABLE matches ADD UNIQUE (order_id, ..., ...)

INSERT IGNORE INTO matches .

, , , .

:

$result = $database->get_results('SELECT loluser, region, date_created, date_completed
             FROM `orders` WHERE `order_id` = '.$order_id);

list($loluser, $region, $date_created, $date_completed)=$result[0];

$matchlistapi = $api->matchlist();
$matchlist = $matchlistapi->matchlist($loluser, "RANKED_SOLO_5x5",
            "SEASON2015", null, null, null, $date_created, $date_completed);

$matchapi = $api->match();
foreach ($matchlist as $m) {
   $match=$matchapi->match($m->match_id, false);
   $stmt=$database->prepare("INSERT IGNORE INTO matches VALUES(?, ?, ?)", ...);
   $database->execute($stmt);
}

. , SQL

+3
 foreach ($order as $o):
    $loluser = $o->loluser;
    $region = $o->region;
    $date_created = $o->date_created;
    $date_completed = $o->date_completed;
  endforeach;

, ?

?. . WordPress , https://codex.wordpress.org/Function_Reference/wp_list_pluck

+2

if else. , , . - .

function getMatchDetail($orderId) {
    $matchDetail = array();
    $order_id = $_POST['id'];
    $order = $database->get_results('SELECT * FROM `orders` WHERE `order_id`='.$order_id);
    $matches = $database->get_results('SELECT `match_id` FROM `matches` WHERE `order_id`='.$order_id);
    // I do not know what do get_results will return. So I am going to use my best guess
    $matchDetail[$match]["loluser"] = $order["loluser"];
    $matchDetail[$match]["region"] = $order["region"];
    $matchDetail[$match]["date_created"] = $order["date_created"];
    $matchDetail[$match]["date_completed"] = $order["date_completed"];
    return $matchDetail;
}

function getMatchListFromAPI($matchDetail) {
    // Again, just giving you a general direction;
    $api = new API();
    $api->setRegion($matchDetail["region"]);
    $matchList = $api->getList($matchDetail["loluser"], $matchDetail["date_created"], $matchDetail["date_completed"]);
    return $matchList;
}

function doStuffWithMatchList($matchList) {
    foreach ($matchlist as $key):
        $gameIds[] = $key->matchId;
    endforeach;
    $arr_matches = object2array($matches);
    foreach ($arr_matches as $id) {
        $dbMatches[] = (int)$id->match_id;
    }
    $new_array = array_diff($gameIds, $dbMatches);

    foreach ($new_array as $matchId):
        $games[] = $matchapi->match($matchId, false);
    endforeach;
    foreach ($games as $game):
        // Store Games in DB;
    endforeach;
}

if (isset($_POST['refresh-history'])):
    $matchDetail = getMatchDetail($_POST["id"]);
    $matchList = getMatchListFromAPI($matchDetail);
    if ($matchlist["totalGames"] !== 0):
        doStuffWithMatchList($matchList);
        $_SESSION['api_success'] = "Success: Games Synced to Order.";
    else:
        $_SESSION['error_msg'] = "Error 23: Unable to Find Games.";
    endif;
endif;

, .

0

Source: https://habr.com/ru/post/1614024/


All Articles