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.
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);
}