How can I get the value from another form - php

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");

Related

PHP end() function not getting end Array item and object key in Laravel

In my Laravel project, I've got a job set up which runs and attempts to notify a user based on their threshold and chosen alert metrics. I'm using the php end() method to get the last item in an array and then attempting to get whatever metric the user has chosen.
However, upon dumping the data, this isn't returning the last array item, it's returning every item and I'm not sure why?
When I dump my data, I'm getting this format instead of the last item in the array:
[2021-04-13 13:30:45] production.DEBUG: array (
0 =>
(object) array(
'event_category' => 'My Category',
'event_action' => 'My Event',
'event_count' => '2190',
'period_from' => '2021-04-13 00:00:00',
'period_to' => '2021-04-13 13:30:02',
'created_at' => '2021-04-13 13:30:06',
),
1 =>
(object) array(
'event_category' => 'My Category',
'event_action' => 'My Event',
'event_count' => '5184',
'period_from' => '2021-04-12 00:00:00',
'period_to' => '2021-04-12 23:57:02',
'created_at' => '2021-04-12 23:57:07',
),
2 =>
(object) array(
'event_category' => 'My Category',
'event_action' => 'My Event',
'event_count' => '3820',
'period_from' => '2021-04-11 00:00:00',
'period_to' => '2021-04-11 23:57:02',
'created_at' => '2021-04-11 23:57:07',
),
)
I should just be seeing the last item, amongst all of my code, the following is of significant value here:
/**
* Notify if data meets threshold & alert rules
*
* #return void
*/
public function notifyAlertThreshold($alerts, $data)
{
$newestDataPart = end($data) ?? null;
// alerts for data source
foreach ($alerts as $key => $alert) {
Log::debug($newestDataPart);
$metric = !isset($newestDataPart->{$alert->metric}) ? $newestDataPart : $newestDataPart->{$alert->metric};
}
}
In context, here's some mode of the code, but the primary question here, is why is my end() method not returning the last item?
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$filters = json_decode($this->report->discovery_filters, true);
$this->reportStatus = 'complete';
$data = [];
foreach ($filters as $findableKey => $findable) {
/*
** If there are datasets on the findable objec, then we assume
** that we can build up a chart or some data structure.
*/
if (isset($findable['datasets'])) {
$pushableDatasets = [];
foreach ($findable['datasets'] as $datasetKey => $dataset) {
// query data
if (isset($dataset['query'])) {
$chartLabel = $findable['name'] ?? 'Untitled Chart';
$this->setDynamicChartOptions($chartLabel);
$additionFromField = $dataset['query']['additionFromField'] ?? '';
$resultData = [];
if ($dataset['query']['prefersConversionCalculation'] == 'yes') {
$totals = DB::table($dataset['query']['table'])
->select($dataset['query']['columns'])
->where($dataset['query']['calculateConversionFromTotals'])
->orderBy($dataset['query']['orderBy']['field'], $dataset['query']['orderBy']['direction'])
->get()
->chunk(100);
$goal = DB::table($dataset['query']['table'])
->select($dataset['query']['columns'])
->where($dataset['query']['calculateConversionByGoal'])
->orderBy($dataset['query']['orderBy']['field'], $dataset['query']['orderBy']['direction'])
->get()
->chunk(100);
$totals = $totals->flatten();
$goal = $goal->flatten();
$totalsGrouped = $this->groupData(
$totals,
$dataset['query']['groupBy'],
$dataset['query']['groupByFormat'],
$additionFromField
);
$goalsGrouped = $this->groupData(
$goal,
$dataset['query']['groupBy'],
$dataset['query']['groupByFormat'],
$additionFromField
);
$totalsGroupedFlattened = $totalsGrouped->flatten();
$goalsGroupedFlattened = $goalsGrouped->flatten();
$resultData = $this->getStructure($findable, $datasetKey, $goalsGroupedFlattened, $totalsGroupedFlattened);
array_push($pushableDatasets, $resultData);
} else {
$res = DB::table($dataset['query']['table'])
->select($dataset['query']['columns'])
->where($dataset['query']['filterBy'])
->orderBy($dataset['query']['orderBy']['field'], $dataset['query']['orderBy']['direction'])
->get()
->chunk(100);
$res = $res->flatten();
if (isset($dataset['query']['useGrouping']) && $dataset['query']['useGrouping'] == 'yes') {
$results = $this->groupData(
$res,
$dataset['query']['groupBy'],
$dataset['query']['groupByFormat'],
$additionFromField
);
// if we're using an addition function our array is already flattened
if (!empty($additionFromField)) {
$resultData = $results;
} else {
$resultData = $results->flatten();
}
array_push($pushableDatasets, $this->getStructure($findable, $datasetKey, $resultData));
}
}
$dataForAlerts = $resultData;
if ($dataset['query']['prefersConversionCalculation'] == 'yes') {
$dataForAlerts = $dataForAlerts['data'];
}
// alerting
$alerts = $this->getAlertThresholds($dataset['query']['table']);
$this->notifyAlertThreshold($alerts, $dataForAlerts);
}
}
$findable['datasets'] = $pushableDatasets;
}
array_push($data, $findable);
}
// no data or it's empty
if (!isset($data) || empty($data)) {
$this->reportStatus = 'error';
}
// create our report data entry
$this->updateReportData(false, $data);
}

how to cek array php

I have an array that I will insert into the item table, here I use multiple inserts
Array
(
[0] => Array
(
[id_service] => 2
[tracking_number] => RJC219384044389234035
)
[1] => Array
(
[id_service] => 1
[tracking_number] => RJC749944771469498146
)
)
in the item table, there is already id_service: 2
how to make validation, when one of the input is already in the item table, then all the input is false so it fails?
my code
public function nyobain()
{
$resi = $this->input->post('kode'); //tracking_number
$expld = explode(' ', $resi);
$data = $this->M_outbound_destinasi->dbGet($expld); //get 'id_service' where tracking_number
// $db = $this->db->query("SELECT id_service FROM `tabel_items`")->result();
// foreach ($db $key) {
// $temp=array($key->id_service);
// }
// how to cek ? if id_service already in the item table
foreach ($data as $key) {
$idnya = $key['id_service'];
$id_outbound = $key['id_outbound'];
$id_indes = $key['id_indes'];
$id_outbag = $key['id_outbag'];
$data_insert = array(
'jenis' => 'outbound-destinasi',
'id_service' => $idnya,
'id_indes' => $id_indes,
'id_outbound' => $id_outbound,
'id_outbag' => $id_outbag,
'id_admin' => $this->session->userdata('ses_id')
);
$this->db->insert('tabel_items', $data_insert);
}
echo json_encode($data);
}
You can use in_array to achieve this
public function nyobain(){
$resi = $this->input->post('kode');
$expld = explode(' ', $resi);
$data = $this->M_outbound_destinasi->dbGet($expld);
$db = $this->db->query("SELECT id_service FROM `tabel_items`")->result();
if(isset($data) && $data !== ''){//check if $data has a value and it not null
if (in_array($data ,$db, true)) {
return $data.'already exists in the DB!';
}else{
foreach ($data as $key) {
$idnya = $key['id_service'];
$id_outbound = $key['id_outbound'];
$id_indes = $key['id_indes'];
$id_outbag = $key['id_outbag'];
$data_insert = [
'jenis' => 'outbound-destinasi',
'id_service' => $idnya,
'id_indes' => $id_indes,
'id_outbound' => $id_outbound,
'id_outbag' => $id_outbag,
'id_admin' => $this->session->userdata('ses_id')
];
$this->db->insert('tabel_items', $data_insert);
}
return json_encode($data);
}
}
}

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'
);

Issue with saving an update information in Codeigniter

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

Categories