user1372212
user1372212

Reputation: 393

foreach not iterating through array

I'm working on a fairy simple saving class using the below code, whenever I run it, only the first if statement is triggered one time, the data is saved in the logs_all table once and once only. All variables passed to the function fine.

I have spent an hour debugging and have honestly no idea what the problem is, could anyone assist?

function saveLog($logtype, $user, $category, $department, $item, $action) {

            $logtype = 'all,buying';
            $a = explode(",",$logtype);

            foreach($a as $value) {

                if($value == 'all') {

                    $db = new mysqli(DB_HOST,DB_USERNAME, DB_PASSWORD, DB_NAME);
                    $sql = "INSERT INTO logs_all (user, category, department, item, action, date) VALUES ('$user', '$category', '$department', '$item', '$action', NOW())";
                    $db->query($sql);
                    return $db->insert_id;


                }

                if($value == 'buying') {

                    $db = new mysqli(DB_HOST,DB_USERNAME, DB_PASSWORD, DB_NAME);
                    $sql = "INSERT INTO logs_buying (user, category, department, item, action, date) VALUES ('$user', '$category', '$department', '$item', '$action', NOW())";
                    $db->query($sql);
                    return $db->insert_id;


                }

            }

}

Upvotes: 0

Views: 269

Answers (3)

Tibor B.
Tibor B.

Reputation: 1690

You are quitting from the function the first time your code reaches return.

Here's a simplified version of your code, which makes a use of that your logtype has the same name as the table name in the database:

function saveLog($logtype, $user, $category, $department, $item, $action) {
    $logtype = 'all,buying';
    $a = explode(",",$logtype);
    $result = array();

    foreach($a as $value) {
        $db = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
        $sql = "INSERT INTO logs_" . $a . " (user, category, department, item, action, date) VALUES ('$user', '$category', '$department', '$item', '$action', NOW())";
        $db->query($sql);
        $result[] = $db->insert_id;
    }
    return $result;
}

Upvotes: 1

Jance
Jance

Reputation: 83

It's because your are using a return. From php.net:

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.

Your function is terminating once it hits that return. If you want to return each item, it would be better to save the ids to an array, then return the array after the loop has finished.

Upvotes: 2

sjagr
sjagr

Reputation: 16502

return $db->insert_id;

This is your problem. The return statement returns out of the context of the function, or in a global case, the PHP script entirely. This means the foreach stops executing along with any other PHP code inside the function.

Upvotes: 2

Related Questions