While using update_batch function for codeigniter batch update I get this error
Undefined index: admission_no
function test_update(){
$updateArray = array();
$elefee = $this->input->post('effe');
$extra = $this->input->post('extra');
$messfe = $this->input->post("messfee");
$students = $this->Messmodel->get_each_student();
foreach ($students as $row){
$newdue = abs($row->amount - ($row->mess_fee + $row->ele_fee + $row->dues));
$finaldays = ($row->messdays - $row->messcut) * $messfe;
$messfee = $finaldays + $extra;
$updateArray[]=array('admission_no'=>$row->ADMISSION_NUMBER,'mess_fee' => $messfee);
}
$this->db->update_batch('mess',$updateArray,'admission_no');
}
this is my db snap
When i print update array I get this
Related
So I'm trying to fetch a points table for users to add points in by garnering their total points and adding it with the installation points, however by trying to fetch their "latest" points, new users who do not have an existing row in the table will throwback an error "InvalidArgumentException; Data Missing". This would be my code below, and are there any other ways around this?
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first();
$points['user_id'] = $input['user_id'];
$points['points_add'] = $battery['point_installation'];
$points['points_subtract'] = 0;
$points['points_total'] = $currentpoint + $battery['point_installation'];
$points['points_source_id'] = 1;
$points['created_at'] = $mytime;
$points['updated_at'] = $mytime;
$point = Points::create($points);
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first(); of your code return an object and you are try to perform math (addition) operation on that object which is not possible. You can update your code like below.
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first();
$points['user_id'] = $input['user_id'];
$points['points_add'] = $battery['point_installation'];
$points['points_subtract'] = 0;
if($currentpoint != null)
{
$points['points_total'] = $currentpoint->points_total + $battery['point_installation'];
}
else {
$points['points_total'] = $battery['point_installation'];
}
$points['points_source_id'] = 1;
$points['created_at'] = $mytime;
$points['updated_at'] = $mytime;
$point = Points::create($points);
I want to get value from my SQL and then show it to my view and still getting error could not be converted to int
public function v_tambah_barang_dipinjam() {
$nama_barang['nb'] = $this->input->post('nama_barang');
$jsa = $this->db->query("SELECT jumlah_barang FROM stok WHERE nama_barang='$nama_barang'");
$data['stok'] = $this->M_admin->lihat_barang()->result();
$this->load->view('v_tambah_barang_dipinjam', $data + $nama_barang + $jsa);
}
I think you're trying to add your mysqli result object $jsa on this line so php is trying to convert it to an int.
$this->load->view('v_tambah_barang_dipinjam', $data + $nama_barang + $jsa);
You should pass $data only. And this is invalid $data + $nama_barang + $jsa
. You cannot add this because $data is not an int.
Change your code from this:
$this->load->view('v_tambah_barang_dipinjam', $data + $nama_barang + $jsa);
To this:
$this->load->view('v_tambah_barang_dipinjam', $data);
Whole Code
public function v_tambah_barang_dipinjam() {
$nama_barang['nb'] = $this->input->post('nama_barang');
$jsa = $this->db->query("SELECT jumlah_barang FROM stok WHERE nama_barang='$nama_barang'")->result();
//This will check first if $jsa is empty
if(!empty($jsa)){
$jsa = $jsa[0]->jumlah_barang;
}else{
$jsa = '';
}
$data['stok'] = $this->M_admin->lihat_barang()->result();
$data['nama_barang'] = $nama_barang;
$data['jsa'] = $jsa;
$this->load->view('v_tambah_barang_dipinjam', $data);
}
Just call $nama_barang $jsa in your view
I'm struggling to update an existing record through an Eloquent Model in Laravel 5.4
I have for creating a record that works perfectly fine, I took it and modified it to try and update the record:
public function commitEdit ($char_edit_id)
{
$edited_character = \DB::table('characters')->where('char_id', $char_edit_id)->first();
$edited_character->campaign_id = 1;
$edited_character->character_name = request('characterName');
$edited_character->Race = request('race');
$edited_character->Sub_Race = request('subRaceField');
$edited_character->Class = request('class');
$edited_character->Level = request('level');
$edited_character->Strength = request('strength');
$edited_character->Dexterity = request('dexterity');
$edited_character->Constitution = request('constitution');
$edited_character->Intelligence = request('intelligence');
$edited_character->Wisdom = request('wisdom');
$edited_character->Charisma = request('charisma');
$levelVar = request('level');
if ($levelVar >= 4) {
$edited_character->Proficiency = 2;
} else if ($levelVar >= 8) {
$edited_character->Proficiency = 3;
}
$edited_character->Trained_Skills = request('skillsField');
$edited_character->Languages = request('languagesField');
$edited_character->Hit_Die = 1;
$edited_character->max_HP = request('max-hp');
$edited_character->Alignment = request('alignment');
$edited_character->Armor_Class = request('armor-class');
$edited_character->Initiative = request('initiative');
$edited_character->Speed = request('speed');
$edited_character->Background = request('background');
$edited_character->update();
return redirect('./characters');
That gives this error:
Call to undefined method stdClass::update()
I have tried using save() but I get the same error with save() instead of update()
Thanks in advance c:
Documentation
If you just need to retrieve a single row from the database table, you may use the first method. This method will return a single StdClass object:
$edited_character is a stdClass, no Eloquent model.
You can try this code:
public function commitEdit ($char_edit_id)
{
$edited_character = \DB::table('characters')->where('char_id', $char_edit_id)->update([
'campaign_id' => 1,
'character_name' => request('characterName'),
'Race' => request('race'),
//others property
]);
}
Or create Characters model which will be extends from Illuminate\Database\Eloquent\Model and use save method:
public function commitEdit ($char_edit_id)
{
$edited_character = Characters::where('char_id', $char_edit_id)-first();
//your code with properties
$edited_character->save();
}
You can try this way:
public function commitEdit ($char_edit_id)
{
$edited_character = Characters::find($char_edit_id);
$edited_character->character_name = request('characterName');
$edited_character->Race = request('race');
$edited_character->Sub_Race = request('subRaceField');
$edited_character->Class = request('class');
$edited_character->Level = request('level');
$edited_character->Strength = request('strength');
$edited_character->Dexterity = request('dexterity');
$edited_character->Constitution = request('constitution');
$edited_character->Intelligence = request('intelligence');
$edited_character->Wisdom = request('wisdom');
$edited_character->Charisma = request('charisma');
$levelVar = request('level');
if ($levelVar >= 4) {
$edited_character->Proficiency = 2;
} else if ($levelVar >= 8) {
$edited_character->Proficiency = 3;
}
$edited_character->Trained_Skills = request('skillsField');
$edited_character->Languages = request('languagesField');
$edited_character->Hit_Die = 1;
$edited_character->max_HP = request('max-hp');
$edited_character->Alignment = request('alignment');
$edited_character->Armor_Class = request('armor-class');
$edited_character->Initiative = request('initiative');
$edited_character->Speed = request('speed');
$edited_character->Background = request('background');
if($edited_character->save()){
return redirect('./characters');
}else{
// show error message
}
}
I have error in pagination total.
Actually I'm getting the results but the problem is when there is no records then it throws the following error:
Undefined index: inactive_users (View: /home/vagrant/Code/krankontroll/resources/views/customer/inactive.blade.php)
If I don't have any records for inactive_users array I'm getting the above error where actually it should give "No of records 0".
My controller page code is:
$customers = Customer::getCustomerPaginated(Session::get('search'), $data['sortorder'], $data['sortby']);
$data['customers'] = Customer::getCustomerByStatus($customers, (Input::get('page'))?Input::get('page'):1);
And my model page code is:
public static function getCustomerByStatus($customers, $pageStart=1)
{
$customer_status = array();
$customer_result = array();
$newcustomers=array();
foreach($customers as $customer){
$customer_status[$customer->status][] = $customer;
if($customer->status<>2)
$customer_status[3][] = $customer;
$newcustomers[] = $customer;
}
$customer_result = $customer_status;
$perPage = 10;
// Start displaying items from this number;
$offSet = ($pageStart * $perPage) - $perPage;
//Slice the collection to get the items to display in current page
$currentPageSearchResults = $customers->slice($offSet, $perPage, true)->all();
$collection = new Collection($currentPageSearchResults);
// Get only the items you need using array_slice
$pagination['all'] = new LengthAwarePaginator($collection, count($newcustomers), $perPage, Paginator::resolveCurrentPage(), array('path' => Paginator::resolveCurrentPath()));
foreach($customer_status as $status=>$customer_state)
{
$itemsForCurrentPage = new Collection(array_slice($customer_state, $offSet, $perPage, true));
if($status==0)
$label='active_users';
else if($status==1)
$label='inactive_users';
else if($status==3)
$label='nonyearly_users';
else
$label='yearly_users';
$pagination[$label] = new LengthAwarePaginator($itemsForCurrentPage, count($customer_state), $perPage, Paginator::resolveCurrentPage(), array('path' => Paginator::resolveCurrentPath()));
}
var_dump($pagination);
exit;
return $pagination;
}
Can anyone help me???
I have an array containing several variables from my sql database.
{"gold":"0","silver":"0","bronze":"0","gdp":"12959563902","population":"3205000","country_name":"Albania"}, {"gold":"1","silver":"0","bronze":"0","gdp":"188681000000","population":"35468000","country_name":"Algeria"}
I have an additional variable called $score that uses information from the database to calculate this score. I want to know how I can loop through and add the correct score to each country in the array.
My Original Code:
$row = $res->fetchRow();
$resGold = $row['gold'];
$resSilver = $row['silver'];
$resBronze = $row['bronze'];
$resGdp = $row['gdp'];
$resPopulation = $row['population'];
$resCountry = $row['country_name'];
$gold_score = ($resGold * $gold_value);
$silver_score = ($resSilver * $silver_value);
$bronze_score = ($resBronze * $bronze_value);
if($population == true){
$score = (($gold_score + $silver_score + $bronze_score)/$resPopulation);
}
else if($gdp == true){
$score = (($gold_score + $silver_score + $bronze_score)/$resGdp);
}
$result = $res->fetchAll();
$result[] = array('score' => $score);
echo json_encode($result);
Your code will be something like this:
$json_data = '{"gold":"0","silver":"0","bronze":"0","gdp":"12959563902","population":"3205000","country_name":"Albania"},
{"gold":"1","silver":"0","bronze":"0","gdp":"188681000000","population":"35468000","country_name":"Algeria"}';
$countries_info_new = array();
$countries_info = json_decode($json_data);
foreach($countries_info as $country_info){
$country_info['score'] = Get_country_score($country_info['country_name']);
$countries_info_new[]=$country_info;
}
$new_json_data = json_encode($countries_info_new);