I want to insert multiple rows in the database on one click if row is checked using multiple checkbox
Here is my code=>
1)Controller: guard.php
(Here I take a array of list of student id's and pass them one by one to the another function get_leave_data which take id of a student and return more information about the student from another table leave_application).
public function students_out(){
$request=$this->input->post('approve_leave_status');
if($request=="OUT"){
$check_list_out[]=$_POST['check_list_out'];
if(!empty($check_list_out)){
foreach ($check_list_out as $check_list_id) {
$student_data=$this->get_leave_data($check_list_id);
$this->insert_student_outside($student_data);
}
$this->load->helper('url');
$this->load->view('view_guard');
}
}else{
$this->load->helper('url');
$this->load->view('view_guard');
}
} `
public function get_leave_data($id){
$this->load->model('model_guard');
$data=$this->model_guard->get_data($id);
return $data;
}
public function insert_student_outside($std_data){
$this->load->model('model_guard');
$data=$this->model_guard->insert_student_out($std_data);
return $data;
}
2)Model: model_guard.php
(The functions get_data() and get_data2() return more informations about student and function insert_student_out() insert the student to the student_outside table)
public function get_data($id){
$this->db->select('leave_id,leave_from_roll_no,leave_student_name,leave_going_to,leave_from_date,leave_till_date,leave_hostel_no,leave_status');
$this->db->from('leave_application');
$this->db->where('leave_id',$id[0]);
$query=$this->db->get();
$data1=$query->result();
$data2=$this->get_data2($data1);
$final_array=array_merge($data1,$data2);
return $final_array;
}
public function get_data2($array){
foreach ($array as $key) {
$roll_no=$key->leave_from_roll_no;
}
$this->db->select('student_year,student_semester,student_parent_email');
$this->db->from('students');
$this->db->where('student_roll_no',$roll_no);
$query=$this->db->get();
$data=$query->result();
return $data;
}
public function insert_student_out($std_data){
$roll_no=$std_data[0]->leave_from_roll_no;
$id=$std_data[0]->leave_id;
$date_out=date('Y-m-d');
$inside_date=NULL;
$date_allowed=$std_data[0]->leave_till_date;
$array=array(
'outside_id'=>$id,
'outside_roll_no'=>$roll_no,
'outside_date_out'=>$date_out,
'outside_date_in'=>$inside_date,
'outside_date_allowed'=>$date_allowed
);
if($this->db->insert('students_outside',$array)){
return true;
}else{
false;
}
}
you are trying to insert batch_array using codeignator
the right syntax to using batch_array is:-
$array=array(
'coloum_name'=>$id,
'coloum_name'=>$roll_no,
'coloum_name'=>$date_out,
'coloum_name'=>$inside_date,
'coloum_name'=>$date_allowed
);
if($this->db->insert_batch('students_outside',$array)){
return true;
}else{
false;
}
you can read manual insert_batch here
ok, i am assuming that in $std_data you have more than one record.then in your model's insert_student_out() function try this-
public function insert_student_out($std_data){
foreach($std_data as $row){
$roll_no=$row->leave_from_roll_no;
$id=$row->leave_id;
$date_out=date('Y-m-d');
$inside_date=NULL;
$date_allowed=$row->leave_till_date;
$array=array(
'outside_id'=>$id,
'outside_roll_no'=>$roll_no,
'outside_date_out'=>$date_out,
'outside_date_in'=>$inside_date,
'outside_date_allowed'=>$date_allowed);
$this->db->insert('students_outside',$array);
}
}
Related
I am new to Laravel. I have a existing and working query that allow me to get all the item present in my Mysql Table.
File --> ProductRepostitory.php
Public function getAll(){
$data = Product::paginate(10);
if(empty($data)){
return false;
}else{
return $data;
}
}
In that table I have a column named PR_ACTIF --> 1 or 0.
I would like to display in the query only PR_ACTIF = 1
Thanks for you help!
Try this
...
Public function getAll(){
$data = Product::where('PR_ACTIF', 1)->paginate(10);
if(empty($data)){
return false;
}else{
return $data;
}
}
...
I Have this 3 tables like below :
Tools
Parts
Part_details
it is my the table structure :
Tool -> has many -> parts. part -> has many->part_details.
Tool : id*, name; Parts : id*, name, tool_id; part_details: id, part_id, total;
Question :
Using laravel Model, how can I get Tool with One part that has biggest total on parts_details ??
// Tool Model
public function parts(){
return $this->hasMany(Part::class);
}
// Part Model
public function part(){
return $this->belongsTo(Tool::class);
}
public function part_details(){
return $this->hasMany(PartDetail::class);
}
// PartDetail Model
public function part(){
return $this->belongsTo(Part::class);
}
Now query the Tool model
$tools = Tool::with('parts')->withCount('parts.part_details')->get();
$toolWithMaxCount = $tools->filter(function($tool) use ($tools){
return $tool->parts->max('par_details_count') === $tools->max('parts.part_details_count');
})->first();
You can improve this with adding some raw bindings to optimise it. I think you got the idea.
Tool model
public function parts() {
return $this->hasMany('App\Part');
}
Part Model
public function details() {
return $this->hasMany('App\PartDetail');
}
public function tool() {
return $this->belongsToMany('App\Tool');
}
Detail Model
public function part() {
return $this->belongsToMany('App\Part');
}
Controller
$tools = Tool::with('parts', 'parts.details')
->find($id)
->max('parts.part_details');
Use the the hasManyThrough Relationship to get the all part details related to tool and then you can check the one by one record and get the highest total of the tool part.
// Tool Model
public function partsdetails()
{
return $this->hasManyThrough('App\PartDetail', 'App\Part','tool_id','part_id');
}
In Your controller
$data = Tool::all();
$array = [];
if(isset($data) && !empty($data)) {
foreach ($data as $key => $value) {
$array[$value->id] = Tool::find($value->id)->partsdetails()->sum('total');
}
}
if(is_array($array) && !empty($array)) {
$maxs = array_keys($array, max($array));
print_r($maxs);
}
else{
echo "No Data Available";
}
in model file i write this code:
public function maxcode()
{
$this->db->select (max(ClassID)+1 as ClassID)-> from ('class');
$query=$this->db->get();
if($query->num_rows>0)
{
$row = $query->row_array();
return $row;
}
echo $row['ClassID'];
}
in controller function i write.....this code
public function maxcode()
{
$this->insert->maxcode();
}
Now I understand what your problem is.
You need to transfer this value to the template. This is done in the page controller
Example
$data['ClassID'] = $this->insert->maxcode();
$this->view('tenplate Name', $data);
And used in template
<?php echo $ClassID; ?>
I am a new to php, have this code in controller:
public function selectquery($id){
$this->load->database();
$this->load->model('aboutmodel');
$data['h'] = $this->aboutmodel->selectquery($id);
$this->load->view('aboutpageview', $data);
}
and this in model:
public function selectquery($id){
$query =$this->db->get_where('phone_mobileinfos',array('id'=>$id));
// return $query->row_array();
return $query;
}
this in view:
{
<p>Mobile_name:<?php echo $h['mobile_name'];?></p>
<p>Price:<?php echo $h['price'];?></p>
}
Just want to search by name instead of id any suggestions..?
In model:
public function selectByName($name){
$query =$this->db->get_where('phone_mobileinfos',array('name'=>$name));
return $query->result_array();
}
use in controller:
$data['by_name'] = $this->aboutmodel->selectquery($id);
in view:
<p>Mobile_name:<?php echo $by_name['mobile_name'];?></p>
<p>Price:<?php echo $by_name['price'];?></p>
So, If I get you correctly and Adding to j.Litvak's code, you want to search the db for a name rather than an id? If so you would need to pass the controller method the name variable i.e. http://localhost/controller_name/method/search_parameter, in your case localhost/controller/selectquery/samsung
Your controller will be
public function selectquery($id){
$this->load->database();
$this->load->model('aboutmodel');
$data['h'] = $this->aboutmodel->selectByName($id);
$this->load->view('aboutpageview', $data);
}
And Model
public function selectByName($name){
$query =$this->db->get_where('phone_mobileinfos',array('name'=>$name));
return $query->result_array();
}
And View
foreach($h as $key => $value){ The code here is obviously in php tags but I cant show them
Mobile: echo $value['mobile_name'];
Price: echo $value['price'];
}
So you create a loop through the result, iterating each result found.
CAVEAT: you will need to test for a NULL result or you will get an error and be careful passing strings through the URL, codeigniter has sql injection protection but I always like to be safe
I have db table images with primary key ID and auto increment. Am adding new images with ajax > controller > model.
My model for create has method create
public function create($data = array())
{
$this->db->insert($this->_table, $data);
return $this->db->insert_id();
}
public function getLastInserted() {
return $this->db->insert_id();
}
And in controller i create test metod to check what is last id inserted:
public function lastID()
{
var_dump($this->photo->getLastInserted());
}
// int(0)
But record is succeffull inserted in database. What can be problem ?
From the official docs: mysqli::$insert_id - Returns the auto generated id used in the LAST query
Make sure you are not doing any other queries AFTER your create() method.
I would suggest to place a last inserted ID into class variable, to avoid problems like these in the future:
private $lastInsertId = null;
public function create($data = array())
{
$this->db->insert($this->_table, $data);
$this->lastInsertId = $this->db->insert_id();
return $this->lastInsertId;
}
public function getLastInserted() {
return $this->lastInsertId;
}
Your create function return last insert id. To see your last insert id just use this code in controller function
public function lastID()
{
var_dump($this->photo->create('table_name',$data));
}
photo model function
function create($tableName, $data)
{
$this->db->insert($tableName, $post);
return $this->db->insert_id();
}
lastInsertid only works when you use it on your db connection variable like
dbconnection->lastInsertId()