I have a table in my database. The table structure is as follows
I need to insert data to this table as a batch, but avoid duplicate entry (here duplicate means same combination of intCampaignID and intMobileNumber).
The following is the insert function in my model
function addToCampaign($data){
$voiceengine = $this->load->database('voiceengine', TRUE);
$scheduleData = array();
$mobiles = explode(',', $data['destinations']);
$mobilesData = array();
foreach($mobiles as $mobile){
if (trim(strlen($mobile)) > 10)
$mobile = substr(trim($mobile), -10);
else
$mobile = trim($mobile);
if(!in_array($mobile, $mobilesData)){
array_push($mobilesData, $mobile);
}
}
foreach($mobilesData as $mobile){
$campaignData = array(
'intCampaignID' => $data['campaign'],
'intMobileNumber' => $mobile);
array_push($scheduleData, $campaignData);
}
$voiceengine->insert_batch('Voice.CampaignNumbers', $scheduleData);
}
I am not sure how to do this. I searched for a solution, but all the solutions are avoiding duplicates based on key. But these fields are not key fields and I have no privilege to change the structure. Someone please help me to do this
UPDATE
I have updated my function in model
function addToCampaign($data){
$voiceengine = $this->load->database('voiceengine', TRUE);
$scheduleData = array();
$mobiles = explode(',', $data['destinations']);
$mobilesData = array();
foreach($mobiles as $mobile){
if (trim(strlen($mobile)) > 10)
$mobile = substr(trim($mobile), -10);
else
$mobile = trim($mobile);
if(!in_array($mobile, $mobilesData)){
array_push($mobilesData, $mobile);
}
}
foreach($mobilesData as $mobile){
$voiceengine->where('intCampaignID', $data['campaign']);
$voiceengine->where('intMobileNumber', $mobile);
$count = $voiceengine->count_all_results('Voice.CampaignNumbers'); // need a simple method
if($count <= 0) {
$campaignData = array(
'intCampaignID' => $data['campaign'],
'intMobileNumber' => $mobile);
array_push($scheduleData, $campaignData);
}
}
$voiceengine->insert_batch('Voice.CampaignNumbers', $scheduleData);
}
The updated function will work. But I am looking for a simple solution
Before inserting remove the duplicate values :
$scheduleData = array_map("unserialize", array_unique(array_map("serialize", $scheduleData)));
and then run the insert_batch function
Related
I use CodeIgniter, and when an insert_batch does not fully work (number of items inserted different from the number of items given), I have to do the inserts again, using insert ignore to maximize the number that goes through the process without having errors for existing ones.
When I use this method, the kind of data I'm inserting does not need strict compliance between the number of items given, and the number put in the database. Maximize is the way.
What would be the correct way of a) using insert_batch as much as possible b) when it fails, using a workaround, while minimizing the number of unnecessary requests?
Thanks
The Correct way of inserting data using insert_batch is :
CI_Controller :
public function add_monthly_record()
{
$date = $this->input->post('date');
$due_date = $this->input->post('due_date');
$billing_date = $this->input->post('billing_date');
$total_area = $this->input->post('total_area');
$comp_id = $this->input->post('comp_id');
$unit_id = $this->input->post('unit_id');
$percent = $this->input->post('percent');
$unit_consumed = $this->input->post('unit_consumed');
$per_unit = $this->input->post('per_unit');
$actual_amount = $this->input->post('actual_amount');
$subsidies_from_itb = $this->input->post('subsidies_from_itb');
$subsidies = $this->input->post('subsidies');
$data = array();
foreach ($unit_id as $id => $name) {
$data[] = array(
'date' => $date,
'comp_id' => $comp_id,
'due_date' => $due_date,
'billing_date' => $billing_date,
'total_area' => $total_area,
'unit_id' => $unit_id[$id],
'percent' =>$percent[$id],
'unit_consumed' => $unit_consumed[$id],
'per_unit' => $per_unit[$id],
'actual_amount' => $actual_amount[$id],
'subsidies_from_itb' => $subsidies_from_itb[$id],
'subsidies' => $subsidies[$id],
);
};
$result = $this->Companies_records->add_monthly_record($data);
//return from model
$total_affected_rows = $result[1];
$first_insert_id = $result[0];
//using last id
if ($total_affected_rows) {
$count = $total_affected_rows - 1;
for ($x = 0; $x <= $count; $x++) {
$id = $first_insert_id + $x;
$invoice = 'EBR' . date('m') . '/' . date('y') . '/' . str_pad($id, 6, '0', STR_PAD_LEFT);
$field = array(
'invoice_no' => $invoice,
);
$this->Companies_records->add_monthly_record_update($field,$id);
}
}
echo json_encode($result);
}
CI_Model :
public function add_monthly_record($data)
{
$this->db->insert_batch('monthly_record', $data);
$first_insert_id = $this->db->insert_id();
$total_affected_rows = $this->db->affected_rows();
return [$first_insert_id, $total_affected_rows];
}
AS #q81 mentioned, you would split the batches (as you see fit or depending on system resources) like this:
$insert_batch = array();
$maximum_items = 100;
$i = 1;
while ($condition == true) {
// code to add data into $insert_batch
// ...
// insert the batch every n items
if ($i == $maximum_items) {
$this->db->insert_batch('table', $insert_batch); // insert the batch
$insert_batch = array(); // empty batch array
$i = 0;
}
$i++;
}
// the last $insert_batch
if ($insert_batch) {
$this->db->insert_batch('table', $insert_batch);
}
Edit:
while insert batch already splits the batches, the reason why you have "number of items inserted different from the number of items given" might be because the allowed memory size is reached. this happened to me too many times.
function practise()
{
$this->load->database();
$qry = mysql_query("select * from demmo");
if (mysql_num_rows($qry) > 0)
{
while ($row = mysql_fetch_array($qry))
{
$created = $row['created'];
//from here
$qry = mysql_query("select * from demmo where created = '$created'");
while ($res = mysql_fetch_array($qry))
{
$user_id = $res['id'];
$name = $res['name'];
$created2 = $res['created'];
$users[] = array('user_id' => $user_id, 'name' => $name);
}
$dotts[] = array('created' => $created2);
//till here
}
return array ($dotts,$users);
}
}
in demmo table i am trying to fetch data and showing that data according to date .the problem is that the code is only selecting one date from the table from created rows and showing that data only .fortunately data shown is not only last but the data with actual date.
You need to create an array and use array_push to get more than one result. Right now your code is only returning the last result of the while loop:
For example, to get all of the dates:
$dotts = array();
$allusers = array();
while ($res = mysql_fetch_array($qry))
{
$user_id = $res['id'];
$name = $res['name'];
$created2 = $res['created'];
array_push($dotts, $created2);
$users[] = array('user_id' => $user_id, 'name' => $name);
array_push($allusers, $users);
}
//
return array ($dotts,$allusers);
You need to create an array and use array_push function , then only it will have more than one value.
example:
create an empty array as
$allUser = array();
then after this line
$users[] = array('user_id' => $user_id, 'name' => $name);
use array_push as
array_push($allUser, $users);
}
return array($dots, $allUser);
I am using the below code to get the ids from the pr_users table and store it in pr_notification_table,but unable to store the values separated by comma into pr_notifications table. I want to store $notification_data['show_users'] as 1,2,3,4 etc so that notifications are sent to these ids. Its inserting NULL on executing this , I have attached table images also,
pr_notifications table is as below:
My controller code is:
if($data['rows'][0]['last_status'] == 'Accepted')
{
$ids= '22';
$data['success_message'] = $this->exit_common->send_notification_to_all_roles($ids);
echo "Success";
}
My model code is:
function send_notification_to_all_roles($ids)
{
global $USER;
$post_arr = $this->input->post();
$this->db->select('g.*,id');
$this->db->from('pr_users as g');
$this->db->where('userroleid', $ids);
//$this->db->join($this->myTables['pr_users_details'].' as ud','ud.userid = g.userid');
//$this->db->join('pr_users_details as ud','ud.userid = g.userids');
/* $this->db->join($this->myTables['users_details'].' as ud','ud.userid = g.userid');
$this->db->join('pr_resignation_type as gt','gt.id = g.sr_type');*/
$query=$this->db->get();
$return = $query->result_array();
$arr = explode(',',$return);
foreach($arr as $num)
{
echo $num."<br>";
}
print_r($num);
die;
$manager_id = $this->get_value_by_id('managerid','users',$this->session->userdata('admin_id'));
$user_id='1';
$v_memberid = $manager_id . "," . $user_id;
//$manager_id = $this->get_value_by_id('managerid','users',$this->session->userdata('admin_id'));
$notification_data['ref_table'] = 'pr_resignation_requests';
$notification_data['ref_id'] = '1';
$notification_data['modifier_id'] = $USER->id;
$notification_data['show_users'] = $num;
$notification_data['notification_descr']= "A new Job has been created" ;//$manager_id;
$notification_data['notification_text'] = "A new Job has been created";
$notification_data['added_on'] = date("Y-m-d H:i:s");
$notification_data['url'] = 'exits';
$notification_data['uurl'] = 'exits';
$this->db->insert($this->myTables['notifications'],$notification_data);
return 'Resignation Request submitted successfully';
}
I think you have to get notification_id from pr_users table, and then use the following code for get notification_id comma seprated.Assume than your notification id array is :- $user_notification_ids_info
Now go with this code.
$ids = ''; $notification_ids = '';
for($i=0; $i<count($user_notification_ids_info); $i++)
{
$ids = $user_notification_ids_info[$i]['notification_id'];
$notification_ids.= $ids.", ";
}
$notification_ids = substr(trim($notification_ids), 0, -1);
Now simply echo $notification_ids; it will return your comma seprated notification id.
It will helps you try this one.
You want to store $ids comma separated? then use implode().
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(",",$arr);
I hope this will help.
I'm trying to create a single array of status id's and their names pulled from the database. My array needs to come out looking like this
$options = array("1" => "Active", "2" => "Inactive");
I've been trying to use array_push and array merge but it keeps going wrong. Here is what I have so far...
$allStatus = implode(',', array('1', '2'));
$statuses = $clCont->getAllRecords('*', '`status` WHERE idstatus IN ('.$allStatus.')');
$status = mysqli_fetch_assoc($statuses);
$statusOpt = array();
do
{
array_push($statusOpt[$status['idstatus']],$status['statusName']);
}
while ($status = mysqli_fetch_assoc($statuses));
$rows = mysqli_num_rows($statuses);
if($rows > 0)
{
mysqli_data_seek($statuses, 0);
$status = mysqli_fetch_assoc($statuses);
}
print_r($statusOpt);
Ok, let's try to modify your code:
$allStatus = implode(',', array('1', '2'));
$statuses = $clCont->getAllRecords('*', '`status` WHERE idstatus IN ('.$allStatus.')');
// you don't need to fetch first item.
// $status = mysqli_fetch_assoc($statuses);
// and use a do-while loop
// use just while:
$statusOpt = array();
while ($status = mysqli_fetch_assoc($statuses))
{
$id = $status['idstatus'];
// The first time you try to put a value in a
// $statusOpt[$status['idstatus']] it's not exists,
// so you should define it explicitly:
if (!isset($statusOpt[$id])) {
$statusOpt[$id] = array();
}
// after that it should be okay:
array_push($statusOpt[$id],$status['statusName']);
}
print_r($statusOpt);
Can't find quite the right answer so hope someone can help. Basically want to create an array and then return the results from a search e.g.
$tsql = "SELECT date, staffid, ID,status, eventid, auditid from maincalendar";
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$stmt = sqlsrv_query( $conn, $tsql , $params, $options);
$calarray=array();
while($row = sqlsrv_fetch_array($stmt)) {
$rowresult = array();
$rowresult["status"] = $row['status'];
$rowresult["eventid"] = $row['eventid'];
$rowresult["caldate"] = $row['date'];
$rowresult["staffid"] = $row['staffid'];
$rowresult["ID"] = $row['ID'];
$rowresult["auditid"] = $row['auditid'];
$calarray[] = $rowresult;
}
I would then like to search for values matching 'caldate' and 'staffid' and return the associated entry in $calarray
I suggest the following,
Fetch all data needed for the current month you are showing, using col BETWEEN x AND y
Add them to a array in PHP, with staffid and caldate as key
Something like so;
$calarray[$row['staffid'] . '-' . $row['date']][] = $row;
Not sure if a single staffid/date combination can have one or more events per day, if not you can remove the []
To check if we have information for a specific staffid/date combination, use isset
if (isset($calarray[$staffid . '-' . $mydate]) { ... }
Add indexes to the fields you're going to query, and then move the search to the sql query. You can also make simple filtering inside the loop. So you'll be populating several arrays instead of one, based on the search you need.
try this:
$matches = array();
$caldate = //your desired date;
$staffid = //your desired id;
foreach($calarray as $k => $v){
if(in_array($caldate, $v['caldate']) && in_array($staffid, $v['staffid'])){
$matches[] = $calarray[$k];
}
}
$matches should be and array with all the results you wanted.
also:
while($row = sqlsrv_fetch_array($stmt)) {
$rowresult = array();
$rowresult["status"] = $row['status'];
$rowresult["eventid"] = $row['eventid'];
$rowresult["caldate"] = $row['date'];
$rowresult["staffid"] = $row['staffid'];
$rowresult["ID"] = $row['ID'];
$rowresult["auditid"] = $row['auditid'];
$calarray[] = $rowresult;
}
can be shortened into:
while($row = sqlsrv_fetch_array($stmt)) {
$calarray[] = $row;
}
Maybe this code snipplet solves your problem.
I am not a PHP programmer, so no warrenty.
function searchInArray($array, $keyword) {
for($i=0;$i<array.length();$i++) {
if(stristr($array[$i], $keyword) === FALSE) {
return "Found ".$keyword." in array[".$i."]";
}
}
}