Issue with saving an update information in Codeigniter - php

My goal is to update job_contract.
There are two ways that this SHOULD be done. 1. through the client's page AND 2. through the provider's page.
What I currently have is in my job model is:
public function update_job_contract($post_obj)
{
$id = $post_obj['id'];
$data = array
(
'client_feedback' => $post_obj['client_feedback'],
'client_notetoself' => $post_obj['client_notetoself'],
'contract_status' => $post_obj['contract_status'],
'client_id' => $this->auth_model->get_user_id()
);
$this->db->insert('job', $data);
}
public function provider_update_job_contract($post_obj)
{
$id = $post_obj['id'];
$data = array
(
'provider_feedback' => $post_obj['provider_feedback'],
'provider_notetoself' => $post_obj['provider_notetoself'],
'provider_id' => $this->auth_model->get_user_id()
);
$this->db->insert('job', $data);
}
I have the following lines in my client controller page:
public function update_job_contract()
{
$this->validateRole('client');
$this->load->model('job_model');
$id = $this->uri->segment(3,0);
$data['job'] = $this->job_model->get_job($id);
$this->load->view('client/update_job_contract', $data);
}
public function update_job_contract_submit()
{
$this->validateRole('client');
$this->load->model('job_model');
if ( '0' == $_POST['id'] ) {
$this->job_model->update_job_contract($_POST);
//}
redirect('client/manage_job_contracts?message=Congratulations!');
}
And this in my provider controller page:
public function provider_update_job_contract()
{
$this->validateRole('provider');
$this->load->model('job_model');
$id = $this->uri->segment(3,0);
$data['job'] = $this->job_model->get_job($id);
$this->load->view('provider/provider_update_job_contract', $data);
}
public function provider_update_job_contract_submit()
{
$this->validateRole('provider');
$this->load->model('job_model');
if ( '0' == $_POST['id'] ) {
$this->job_model->provider_update_job_contract($_POST);
}
redirect('provider/job_contracts?message=Congratulations!');
}
Problem is, they don't really update the entries. Please help..

You are using $this->db->insert(), but with Active Record to update the row you should be using $this->db->update()
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
Another words your functions should look like this:
public function update_job_contract($post_obj)
{
$id = $post_obj['id'];
$data = array
(
'client_feedback' => $post_obj['client_feedback'],
'client_notetoself' => $post_obj['client_notetoself'],
'contract_status' => $post_obj['contract_status'],
'client_id' => $this->auth_model->get_user_id()
);
$this->db->where('id', $id);
$this->db->update('job', $data);
}
public function provider_update_job_contract($post_obj)
{
$id = $post_obj['id'];
$data = array
(
'provider_feedback' => $post_obj['provider_feedback'],
'provider_notetoself' => $post_obj['provider_notetoself'],
'provider_id' => $this->auth_model->get_user_id()
);
$this->db->where('id', $id);
$this->db->update('job', $data);
}
Also, inside the controllers $_POST should be replaced with $this->input->post()
Using this one as example:
public function provider_update_job_contract_submit()
{
$this->validateRole('provider');
$this->load->model('job_model');
$post = $this->input->post();
if ( '0' == $post['id'] )
{
$this->job_model->provider_update_job_contract($post);
}
redirect('provider/job_contracts?message=Congratulations!');
}
Make sure you update other controllers in the same way

Related

recursion function on tree data to get path

I want to write function which receive me path to top element, but I can't figure out how it should work..
My example data:
$data = array(
3546456 => array(
5345345,
12312312,
56456546,
),
12312312 => array(
34534534,
5675675,
8678678,
),
567978 => array(
234,
756756,
8678678,
),
);
//I have function to return parent.
$parents = getParents(8678678); // eg. $object_id = 8678678 - return [12312312, 567978] , $object_id = 12312312 - return [3546456]
// and my recursion function I tried.
function getObjectPath($object_id) {
if ($object_id == null) {
return [];
}
$parents = getObjectParents($object_id);
foreach ($parents as $parent) {
return array($object_id => getObjectPath($parent->nid));
}
}
It doesn't work way I need, on the return in getObjectPath(8678678) I'd like to have return array like that:
array(
3546456 => array(
12312312 => array(
8678678
)
),
567978 => array(
8678678
)
);
I think you almost got it, need to have some other check before returning
$data = array(
3546456 => array(
5345345,
12312312,
56456546,
),
12312312 => array(
34534534,
5675675,
8678678,
),
567978 => array(
234,
756756,
8678678,
),
);
//I have function to return parent.
$parents = getObjectPath(8678678, $data); // eg. $object_id = 8678678 - return [12312312, 567978] , $object_id = 12312312 - return [3546456]
echo "<pre>";
print_r($parents);
// and my recursion function I tried.
function getParents($object_id, $data) {
$return = [];
foreach($data as $key => $value) {
if(in_array($object_id, $value)) {
$return[] = $key;
}
}
return $return;
}
// and my recursion function I tried.
function getObjectPath($object_id, $data) {
$return = [];
$parents = getParents($object_id, $data);
foreach($parents as $parent) {
$temp = getObjectPath($parent, $data);
if(!empty($temp)) {
$return[key($temp)][$parent] = $object_id;
} else {
$return[$parent] = $object_id;
}
}
return $return;
}

How can I get the value from another form

now i'm trying to get the value from another form. But i don't know how to make it.
This is my first form:
and this is method, when we click submit in the the form1, the page will redirect to this method.
public function input_data_inserted()
{
$id_data_inserted = $this->input->post('id_data_inserted');
$date = $this->input->post('date');
$id_data = $this->input->post('id_data');
$amount= $this->input->post('amount');
$id_room = $this->input->post('id_room');
$passed = FALSE;
$checkIdData = $this->modelku->cek_idData()->result_array();
$checkIdRoom = $this->modelku->cek_idRoom()->result_array();
foreach ($cekIdData as $cID)
{
foreach ($cekIdRoom as $cIR)
{
if ($id_data == $cID['id_data'] && $id_room == $cIR['id_room'])
{
$data = array
(
'id_data_inserted' => $id_data_inserted,
'date' => $date,
'id_data' => $cID['id_data'],
'amount' => $amount,
'id_room' => $cIR['id_room'],
);
$this->modelku->input_data($data, 'table_data_inserted');
$data['table_data_inserted'] = $this->modelku->show_data('table_data_inserted')->result();
$this->modelku->increase_value($amount, $id_data);
//redirect to second form
redirect('admin/Data/data\view_detail_data');
$passed = TRUE;
break;
}
}
}
if(!$passed)
{
echo "ERORR";
}
}
And this is my second form:
Method for second form:
public function input_detail_barang()
{
$id_data = $this->input->post('id_data');
$no_inv = $this->input->post('no_inv');
$condition = $this->input->post('condition');
$data = array
(
'id_data' => $id_data,
'no_inv' => $no_inv,
'condition' => $condition
);
//I need amount from first form, because i will use it. How can i do?
}
Any Solution? Thx
After this line you can save it to session:
$data = array (
'id_data_inserted' => $id_data_inserted,
'date' => $date,
'id_data' => $cID['id_data'],
'amount' => $amount,
'id_room' => $cIR['id_room'], // You missed single quote here
);
$this->load->library('session');
$this->session->set_userdata("Firstformdata",$data);
And for show this data add this line:
$this->session->userdata("Firstformdata");

Insert to db in an iterative way

I want to insert to my database payment table that should be done in this way.
The payment table that I have includes both group_id and member_id as a foreign key related to the table groups and member respectively.
What I want to do is once I hit the button "Pay" it should insert for each and every member_id as a row in the payment table.
Here is my code..
In my controller I have
public function create_action()
{
$this->_rules();
if ($this->form_validation->run() == FALSE) {
$this->create();
} else {
$this->load->model('Member_model');
$memberid = $this->Member_model->paymember();
foreach ($memberid->result_array() as $id){
$memberid[] = $id;
$data = array(
'group_id' => $this->input->post('group_id',TRUE),
'member_id' => $memberid,
'paid_by' => $this->input->post('paid_by',TRUE),
'from_date' => $this->input->post('from_date',TRUE),
'to_date' => $this->input->post('to_date',TRUE),
'amount' => $this->input->post('amount',TRUE),
'reference_number' => $this->input->post('reference_number',TRUE),
//'last_updated_by' => $this->input->post('last_updated_by',TRUE),
//'last_update_time' => $this->input->post('last_update_time',TRUE),
);
}
$this->Payment_model->insert($data);
$this->session->set_flashdata('message', 'Record Created Successfully!');
redirect(site_url('payment'));
}
}
And in my model which is the Member_model I've included in the controller..
function paymember(){
$data= array();
$this->db->where('group_id',$this->input->post('group_id'));
$memberid = $this->db->get('member');
if ($memberid->num_rows() > 0) {
foreach ($memberid->result_array() as $row){
$data[] = $row;
}
}
}
Please help me out. Thank you.
Now I've solved the problem.
In my controller there I have got the array value from the model...
public function create_action()
{
$this->_rules();
if ($this->form_validation->run() == FALSE) {
$this->create();
} else {
$this->load->model('Member_model');
$memberid = $this->Member_model->paymember();
if (count($memberid)) {
foreach ($memberid as $id) {
$data = array(
'group_id' => $this->input->post('group_id',TRUE),
'member_id' => $id,
'paid_by' => $this->input->post('paid_by',TRUE),
'from_date' => $this->input->post('from_date',TRUE),
'to_date' => $this->input->post('to_date',TRUE),
'amount' => $this->input->post('amount',TRUE),
'reference_number' => $this->input->post('reference_number',TRUE),
'last_updated_by' => $this->session->userdata('username'),
//'last_update_time' => $this->input->post('last_update_time',TRUE),
);
$this->Payment_model->insert($data);
echo $data;
}
}
$this->session->set_flashdata('message', 'Record Created Successfully!');
redirect(site_url('payment'));
}
}
Also on my previous model paymember method there was a problem which its not returning any result so I've fixed that with this code....
function paymember(){
$data= array();
$this->db->where('group_id',$this->input->post('group_id'));
$query = $this->db->get('member');
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$data[] = $row['member_id'];
}
}
$query->free_result();
return $data;
}
Thank you for ur support!

Why is my variable data getting dropped after the first foreach loop?

I am using CodeIgniter to insert records in my database. Everything is working perfectly, except I can't seem to understand why the value of a nested array is losing its value after the first foreach loop.
php
// $data is all data (array) passed from controller method.
public function create($data)
{
$my_data = array(
'name' => $data['name'],
....
);
if ($this->db->insert('myTable', $my_data)) {
$insert_id = $this->db->insert_id();
// $data['mySecondCheckbox'] has value here
foreach ($first_data['myFirstCheckbox'] as $cb1) {
$first_data = array(
'fk_foo_id' => $insert_id,
'fk_cb_id' => $cb1
);
$this->db->insert('mySecondTable', $first_data);
}
// $data['mySecondCheckbox'] has no value here
foreach ($data['mySecondCheckbox'] as $cb2) {
$second_data = array(
'fk_foo_id' => $insert_id,
'fk_cb_id' => $cb2
);
$this->db->insert('myThirdTable', $second_data);
}
return $insert_id;
} else {
return false;
}
}
SOLUTION
Thank you #John! Pretty obvious after sleeping on it. At the time, I was thinking that $data was specific to within each of the each scope.
php
// $data is all data (array) passed from controller method.
public function create($data)
{
$my_data = array(
'name' => $data['name'],
....
);
if ($this->db->insert('myTable', $my_data)) {
$insert_id = $this->db->insert_id();
// $data['mySecondCheckbox'] has value here
foreach ($data['myFirstCheckbox'] as $cb1) {
$first_data = array(
'fk_foo_id' => $insert_id,
'fk_cb_id' => $cb1
);
$this->db->insert('mySecondTable', $first_data);
}
// $data['mySecondCheckbox'] has no value here
foreach ($second_data['mySecondCheckbox'] as $cb2) {
$data = array(
'fk_foo_id' => $insert_id,
'fk_cb_id' => $cb2
);
$this->db->insert('myThirdTable', $second_data);
}
return $insert_id;
} else {
return false;
}
}
The first line in your foreach loop is resetting the subject in your loop "$data". Rename the variable and you should be good

CodeIgniter insert two rows instead one

I have an issue with CodeIgniter. For some reason it insert two rows in database instead one.
Below is code from model:
/**
* Add new fuel
*/
public function addNewFuel($id = NULL, $data) {
if ($id) {
var_dump('TEST');
$data = array(
'price' => '333' ,
'fuel' => 'dizelka' ,
'petrol_id' => '66'
);
echo '<pre>';
print_r($data);
echo '</pre>';
$this->db->insert('prices', $data);
echo $this->db->last_query();
$this->db->insert('prices_archive',$data);
return;
}
}
And here is output:
string(4) "TEST"
Array
(
[price] => 333
[fuel] => dizelka
[petrol_id] => 66
)
EDIT:
This is code from controller which calls model's function:
function addnewfuel() {
if ($this->session->userdata('logged_in')) {
$this->load->helper('form');
$id = $this->uri->segment(3);
$fuel = $this->input->post('fuel');
$price = $this->input->post('price');
$addNewFuel = array('fuel' => $fuel, 'price' => $price);
$data['addNewFuel'] = $this->Adminarea_model->addNewFuel($id,$addNewFuel);
$data['getFuels'] = $this->Adminarea_model->getFuels();
$data['getPetrolNameByID'] = $this->Adminarea_model->getPetrolNameByID();
$data['getPetrolInformation'] = $this->Adminarea_model->getPetrolInformation();
$this->load->view('admin/edit', $data);
} else {
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
For some reasons in a table 'prices' I'm getting two rows with same data, and I really don't have idea why that happens.
Thanks
You are inserting data both from controller as well as from model.
remove this from model, hope it ill solve the problem. Thanks
$data = array(
'price' => '333' ,
'fuel' => 'dizelka' ,
'petrol_id' => '66'
);

Categories