1
$projects = array('1' => $link1, '2' => $link2);

function server_status($projects) {

    foreach ($projects as $server) {

        $api_status = ping_api($server);

        if ($api_status == 1) {

            $json = json_decode(file_get_contents($server), true);

            foreach ($json as $obj) {
                if ($obj['online'] < 1) {
                    // OFFLINE
                    return -1;
                }
                else {
                    // ONLINE
                    return $obj['online'];
                }
            }
        } else {
            // MAINTENANCE
            return 0;
        }
    }   
}

function cache_status($data) {
    echo $data;

    $servername = "localhost";
    $username = "root";
    $password = "";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        }
    catch(PDOException $e)
        {
        echo "Connection failed: " . $e->getMessage();
        }       

    foreach ($data as $status) {
    // server id + status
    $sql = "UPDATE projects SET status=:status WHERE id=:server_id";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':status', $status, PDO::PARAM_INT);
    $stmt->bindParam(':server_id', 1, PDO::PARAM_INT);
    $stmt->execute();
    }
}

$problem = array(server_status($projects));
print_r($problem);

My problem is, when I print_r the variable $problem it should return two results in array, since there are two results but it only returning the first result from array as an .. example: Array ( [0] => 260 )

IF anyone is kind enough to look at my code and tell me what I am doing wrong, I would be very greatful

3
  • return will terminate a function straight away. So your function will always return the status of the first server it checks. Commented Nov 18, 2017 at 22:32
  • Your question is a little unclear. Please read the guide on MVCE to help make your question clearer. Commented Nov 18, 2017 at 22:34
  • I see Nigel Ren, ok so shall I echo it? or shall I create a variable and set a value to it each time? Commented Nov 18, 2017 at 22:35

1 Answer 1

1

You are using return statement inside your loop that is why it breaks your loop on first iteration and returns what ever you have mentioned to the original call of function. To resolve this issue you need to collect the response from each iteration in array and in the end of function return your response for each iteration something like

function server_status($projects) {
    $response= array();
    $status = 0; // default status
    foreach ($projects as $server) {
        $api_status = ping_api($server);
        if ($api_status == 1) {
            $json = json_decode(file_get_contents($server), true);
            foreach ($json as $obj) {
                if ($obj['online'] < 1) {
                    // OFFLINE
                    $status= -1;
                }
                else {
                    // ONLINE
                   $status = $obj['online'];
                }
            }
        }
        $response[] = array('server'=>$server,'status'=>$status);
    }
    return $response;   
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir for the help, that was the problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.