0

I have this 3 insertion to database via function call. This code is working no problem.

                    //* Set the function parameters.
                    $client_id = $_SESSION['user']['client_id'];

                    $params = array(
                            'name' => 'Abu',
                            'data' => 'user',
                            'active' => 'y',
                            'stamp' => date('Y-m-d H:i:s'),
                            );

                    $id = $client->user_add($client_id, $params);                           

                    $params = array(
                            'name' => 'Ali',
                            'data' => 'user',
                            'active' => 'y',
                            'stamp' => date('Y-m-d H:i:s'),
                            );

                    $id = $client->user_add($client_id, $params);           

                    $params = array(
                            'name' => 'Siti',
                            'data' => 'user',
                            'active' => 'y',
                            'stamp' => date('Y-m-d H:i:s'),
                            );

                    $id = $client->user_add($client_id, $params);       

The difference is only in name. Is there anyway I could make only one function call to insert data? With loop or something? Thanks in advance.

1 Answer 1

3

You can put the names in an array and loop over it. You put inside the loop the exact code you repeated 3 times, except you use the loop variable instead of a string as name array item.

$names = array('Abu', 'Ali', 'Siti');

foreach($names as $name) {
    $params = array(
        'name' => $name,
        'data' => 'user',
        'active' => 'y',
        'stamp' => date('Y-m-d H:i:s'),
    );

    $id = $client->user_add($client_id, $params);  
}
Sign up to request clarification or add additional context in comments.

Comments

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.