Update Query in codeigniter model is not working - php

In the function update_status the 2nd update query is not working. The values in array are properly getting and correct. Can somebody please help me with that?
Here my code:
function update_status($appno,$value,$user_note,$date)
{
$status = array(
'app_status' => $value, );
$this->db->where('app_no',$appno);
$this->db->update('tbl_application', $status);
$today = date('Y-m-d');
$emp = $this->session->userdata('username');
$this->db->select('*');
$this->db->from('tbl_employee');
$this->db->where('Name', $emp);
$query = $this->db->get();
foreach($query->result() as $row)
{ $emp_code = $row->EmployeeCd; }
$data = array(
'app_no' => $appno,
'office_code' => 'KL08',
'verifi_status' => $value,
'view_status' => 1,
'view_by' => $emp_code,
'first_view' => $date,
'last_view' => $today,
'modification' => $user_note,
'modifi_status' => '1',
'notes' => 'NA'
);
$this->db->update('tbl_appverification', $data);
$this->db->where('app_no',$appno);
}

Where clause must be written before udpate.
$this->db->where('app_no',$appno);
$this->db->update('tbl_appverification', $data);

Please write the Where clause before the Update query, as you have written in your first update.

Related

how to check where condition using foreach

I want to check whether there are more than three teachers for a single subject. So I am trying to select ids from the table for some conditions. the fact is that I have more than one or two classes and indices. so how to check for all classes and indices? I couldn't make it, how can it possible, I am not familiar with CodeIgniter
controller
$checkr = $this->class_model->check($class_data,$subject);
model
function check($class_data,$subject)
{
foreach($class_data as $key => $value)
{
$this->db->select('s_id');
$this->db->from('table1');
$this->db->where('type',2);
$this->db->where('subject',$subject);
$this->db->where('status',1);
$condition= array(
'class' => $key,
'stream_index' => $value
);
$this->db->where($condition);
}
$rows = $this->db->get();
$count= $rows->num_rows() ;
return $count
}
I think this will help you..
function check($class_data,$subject){
$class_teacher_count = array();
foreach($class_data as $key => $value){
$this->db->select('s_id');
$this->db->from('table1');
$this->db->where('type',2);
$this->db->where('subject',$subject);
$this->db->where('status',1);
$this->db->where('class', $key);
$this->db->where('stream_index',$value);
$query = $this->db->get();
$rows = $query->result();
$count = count($rows);
$class_teacher_count[$key] = $count;
}
return $class_teacher_count;
}
function check($class_data,$subject){
foreach($class_data as $key => $value){
$this->db->select('s_id');
$conditions = array(
'type' => 2,
'subject' => $subject,
'status' => 1,
'class' => $key,
'stream_index' => $value
);
$teacher_count[$key] = $this->db->get_where('table1',$conditions)->num_rows();
}
return $teacher_count;
}

update column with aggregate function in codeigniter

I want to change this format to codeigniter format which is like
this->db->where('','')
This is my code
$data = "UPDATE dbhpl.hplpb SET hplpb.PEL_4 = (select COUNT(*) FROM pelayanan
WHERE pelayanan.ID_AREA=hplpb.ID_AREA AND pelayanan.ID_VERIFIKASI='4'
AND pelayanan.TRANSAKSI='PENYAMBUNGAN BARU' AND pelayanan.ESTIMASI='4'
AND pelayanan.ID_STATUS='1')";
$this->db->query($data);
How can I change it?
You might want to try this.
$data = array(
'ID_VERIFIKASI' => '4',
'TRANSAKSI' => 'PENYAMBUNGAN BARU',
'ESTIMASI' => '4',
'ID_STATUS' => '1'
);
$this->db->where($data)
->from('pelayanan')
->join('hplpb', 'hplpb.ID_AREA = pelayanan.ID_AREA', 'inner');
$count = $this->db->count_all_results();
$data = array(
"PEL_4" => $count
);
$this->db->update('hplpb', $data);

How to Update Two table Data one after other In codeigniter?

how to update plan with vendor_plan_task_status_mapp table.
the model for plan update is
public function updatePlanData(){
$planId = $this->input->post('plan_id');
$data = array(
'plan_title' => $this->input->post('plan_title'),
'plan_price' => $this->input->post('plan_price'),
'plan_desc' => $this->input->post('plan_desc')
);
$this->db->where('plan_id', $planId);
$this->db->update('tbl_plan', $data);
$this->db->where('plan_id',$planId);
$this->db->delete('plan_task_mapping');
foreach ($this->input->post('task_id') as $key => $value)
{
$data2 = array(
'plan_id' => $planId,
'task_id' => $value
);
// echo "Index {$key}'s value is {$value}.";
$this->db->insert('plan_task_mapping', $data2);
}
//-------- HEAR I NEED A CODE TO UPDATE The V_T_S_M table-----------
}
after 1st table update i want to update the data in vendr_task_status_mapping table?????
IN THIS ANSWER YOU GET AN ERROR IF YOU CHANGE THE PLAN BUT USING THIS CODE YOU HAVE TO SIMPLY EDIT AND UPDATE IT ANGIN AND THIS MODEL IS CREATE A NEW ID FOR YOUR TASK AND INSERT IT AGAIN.
public function vendorUpdateModel($data)
{
$vendor_id = $this->input->post('vendor_id');
$data = array(
'category_id' => $this->input->post('category_id'),
'plan_id' => $this->input->post('plan_id'),
'city_id' => $this->input->post('city_id'),
'business_name' => $this->input->post('business_name'),
'owner_name' => $this->input->post('owner_name'),
'contact_no1' => $this->input->post('contact_no1'),
'contact_no2' => $this->input->post('contact_no2'),
'vendor_email' => $this->input->post('vendor_email'),
'subscription_date' => $this->input->post('subscription_date'),
'vendor_description' => $this->input->post('vendor_description'),
'vendor_address' => $this->input->post('vendor_address')
);
$this->db->where('vendor_id', $vendor_id);
$this->db->update('vendor',$data);
$this->db->where('vendor_id',$vendor_id);
$this->db->delete('vendor_task_status_mapping');
$this->db->select('task_mapp_id');
$this->db->where('plan_id',$this->input->post('plan_id'));
$query = $this->db->get('plan_task_mapping');
foreach($query->result() as $row)
{
$data2 = array(
'task_mapp_id' => $row->task_mapp_id,
'vendor_id' => $vendor_id,
'task_status' => '0'
);
$this->db->insert('vendor_task_status_mapping', $data2);
}
return;
}
If you added one field in plan_task_mapping table for add unique number then... As you mention your code:
$unique=array();
foreach ($this->input->post('task_id') as $key => $value)
{
$no=rand(0, 15);
$data2 = array(
'unique_no'=>$no,
'plan_id' => $planId,
'task_id' => $value
);
$unique[] = $no; //store no in array to use it.
// echo "Index {$key}'s value is {$value}.";
$this->db->insert('plan_task_mapping', $data2);
}
Before deleting plan_task_mapping table data. fetch that data.
function select()
{
$this->db->where('plan_id',$planId);
$Q = $this->db->get('plan_task_mapping');
if($Q->num_rows() > 0)
{
foreach ($Q->result_array() as $row)
{
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
Then delete the data as you mention in you code:
$this->db->where('plan_id',$planId);
$this->db->delete('plan_task_mapping');
Then delete that old data from VTSM table:
$this->db->where('vendor_plan_task_mapping_id',$data[0]['id']); //this data[0]['id'] come form select(). change field name if required.
$this->db->delete('VTSM');
Here fetch that new inserted data by that unique no: //which we stored it in array.
foreach($unique as $u_no)
{
$this->db->where('unique_no',$u_no);
$Q = $this->db->get('plan_task_mapping');
if($Q->num_rows() > 0)
{
foreach ($Q->result_array() as $row1)
{
$plan[] = $row1;
}
}
$Q->free_result();
return $plan;
}
In above code we have fetched that new inserted data to get their id to insert status.
Now inserting status:
foreach($plan as $a)
{
$Sdata=array(
"plan_task_mapping_id"=>$a['id'], //this is new id change name if required
"status"="your new status");
$this->db->insert('VTSM',$Sdata);
}
Use $this->db->insert_batch(); instead of inserting in foreach loop.
Check at this link how to use it.

How to create multiple conditioner login in codeigniter

my model is here.....but i need to select status of admin ....... but
i m new in codeigniter....and don't no how to select... my need is...
select admin whole detail from table on condition admin status =
active and id=1...
my model is :
public function login($value) {
$query = $this->db->get_where('tbl_admin', $value, 1, 'active');
if ($query->num_rows() > 0) {
$row = $query->row_array();
$sess_arr = array(
'admin_user' => $row['fld_admin_username'],
'adm_key' => $row['fld_admin_key'],
'admin_type' => $row['fld_admin_type'],
'admin_id' => $row['fld_admin_id'],
'admin_logged_in' => TRUE
);
$this->session->set_userdata($sess_arr);
//echo "<pre>";print_r($this->session->all_userdata());exit;
}
else{
$this->session->set_flashdata('error', 'Invalid username/password');
redirect('adminzone');
}
}
The correct syntax for the first line would be:
$query = $this->db->get_where('tbl_admin', array('id' => 1, 'status' => 'active'));
I.e. The second parameter to get_where is an associative array of fields and their values..
Edit: Or perhaps it should be
$query = $this->db->get_where('tbl_admin', array('id' => $value, 'status' => 'active'));
(I am not sure what the $value variable is for here).

Is there a way to return all database columns automatically in CodeIgniter?

This is how I currently do a query and return a dataset in CodeIgniter:
$sql = "SELECT `user_id`, `username`
FROM `users`
LIMIT 10";
$query = $this->db->query($sql);
$users = array();
foreach($query->result() => $row) {
$users[] = array(
'user_id' => $row->user_id,
'username' => $row->username
);
}
return $users;
As you can see I explicitly write what fields I want to return:
$users[] = array(
'user_id' => $row->user_id,
'username' => $row->username
);
Is there a way to have this be done automatically. So all fields that are selected in the sql query will be listed as the key and value of the array being returned?
Yes, there is. Use result_array().
foreach ($query->result_array() as $row)
...
You can find out more about this here.

Categories