I was thinking about getting a member_id from my database Incident table which is a foreign key related to another table Member.
What I wanted to get is for all member_id in the Incident table that are related to Members table so I wanted to display the Name fields of the member_id. Here is My codes are
// My Model
function get_mid(){
$data = array();
$query = $this->db->get('Incident');
foreach($query->resul_array as $row) {
$data[] = $row['member_id'];
}
return $data;
}
In my Controller I have
public function index() {
$member_id = $this->incident_model->get_mid();
if(count($member_id)){
foreach($member_id as $id){
$f_name =$this->incident_model->get_fname($id);
$m_name =$this->incident_model->get_mname($id);
$l_name =$this->incident_model->get_lname($id);
$data = array(
'first_name' => $f_name,
'middle_name' => $m_name,
'last_name' => $l_name
);
}
$this->load->view('incident_list', $data);
}
}
That gives me all records having the same name in my view.
instant use code
public function index(){
$member_id = $this->incident_model->get_mid();
if(count($member_id)){
foreach($member_id as $id){
$f_name =$this->incident_model->get_fname($id);
$m_name =$this->incident_model->get_mname($id);
$l_name =$this->incident_model->get_lname($id);
$data = array(
'first_name' => $f_name,
'middle_name' => $m_name,
'last_name' => $l_name
);
}
$this->load->view('incident_list', $data);
}
replace by
public function index(){
$member_id = $this->incident_model->get_mid();
$temp = array();
$data = array();
if(count($member_id)){
foreach($member_id as $id){
$f_name =$this->incident_model->get_fname($id);
$m_name =$this->incident_model->get_mname($id);
$l_name =$this->incident_model->get_lname($id);
$temp = array(
'first_name' => $f_name,
'middle_name' => $m_name,
'last_name' => $l_name
);
$data[] = $temp;
}
$this->load->view('incident_list', $data);
}
try code.
Related
daily_attendances database
below is under crud_model
public function take_attendance()
{
$students = $this->input->post('student_id');
$data['timestamp'] = strtotime($this->input->post('date'));
$data['class_id'] = html_escape($this->input->post('class_id'));
$data['section_id'] = html_escape($this->input->post('section_id'));
$data['school_id'] = $this->school_id;
$data['session_id'] = $this->active_session;
$check_data = $this->db->get_where('daily_attendances', array('timestamp' => $data['timestamp'], 'class_id' => $data['class_id'], 'section_id' => $data['section_id'], 'session_id' => $data['session_id'], 'school_id' => $data['school_id']));
if($check_data->num_rows() > 0){
foreach($students as $key => $student):
$data['status'] = $this->input->post('status-'.$student);
$data['student_id'] = $student;
$attendance_id = $this->input->post('attendance_id');
$this->db->where('id', $attendance_id[$key]);
$this->db->update('daily_attendances', $data);
endforeach;
}else{
foreach($students as $student):
$data['status'] = $this->input->post('status-'.$student);
$data['student_id'] = $student;
$this->db->insert('daily_attendances', $data);
endforeach;
}
$this->settings_model->last_updated_attendance_data();
$response = array(
'status' => true,
'notification' => get_phrase('attendance_updated_successfully')
);
return json_encode($response);
}
public function get_presentcount_by_student_id($student_id="",$status ="") {
$checker = array(
'student_id' => $student_id,
'status' => 1
);
$presentcount_by_student_id = $this->db->get_where('daily_attendances', $checker);
return $presentcount_by_student_id->num_rows();
}
under the list.php where it shows the student name and the number of days present for each student.The code is below but it does not count number of presents for each student.It still remains 0
<td>
<?php echo $this->crud_model->get_presentcount_by_student_id($student['user_id'],'status');?>
<br>
</td>
Student detail list shown in table format
student_detail_list
use this
$count=DB::select("SELECT COUNT(*) as num FROM 'your-table-name' where status = 1 ")->get();
and you can access this by $count[0]->num
below is the code for crud_model.php
Required to add code to generate present percentage for each student into database when submit attendance
public function take_attendance()
{
$students = $this->input->post('student_id');
$data['timestamp'] = strtotime($this->input->post('date'));
$data['class_id'] = html_escape($this->input->post('class_id'));
$data['section_id'] = html_escape($this->input->post('section_id'));
$data['school_id'] = $this->school_id;
$data['session_id'] = $this->active_session;
$check_data = $this->db->get_where('daily_attendances', array('timestamp' => $data['timestamp'], 'class_id' => $data['class_id'], 'section_id' => $data['section_id'], 'session_id' => $data['session_id'], 'school_id' => $data['school_id']));
if($check_data->num_rows() > 0){
foreach($students as $key => $student):
$data['status'] = $this->input->post('status-'.$student);
$data['student_id'] = $student;
$attendance_id = $this->input->post('attendance_id');
$this->db->where('id', $attendance_id[$key]);
$this->db->update('daily_attendances', $data);
endforeach;
}else{
foreach($students as $student):
$data['status'] = $this->input->post('status-'.$student);
$data['student_id'] = $student;
$this->db->insert('daily_attendances', $data);
endforeach;
}
$this->settings_model->last_updated_attendance_data();
$response = array(
'status' => true,
'notification' => get_phrase('attendance_updated_successfully')
);
return json_encode($response);
}
database fields
Here is the idea of what you want. You will have to analyze it and put it in your code.
if($check_data->num_rows() > 0){
foreach($students as $key => $student):
//add this in your code-----------
$checker = array(
'school_id' => $this->school_id,
'status' => 1
);
$days_a_week = 5;
$present_days = $this->getStudentDaysByID($checker);
$data['present'] = ($present_days / $days_a_week) * 100;
//--------------------------------
$data['status'] = $this->input->post('status-'.$student);
$data['student_id'] = $student;
$attendance_id = $this->input->post('attendance_id');
$this->db->where('id', $attendance_id[$key]);
$this->db->update('daily_attendances', $data);
endforeach;
}else{
foreach($students as $student):
$data['status'] = $this->input->post('status-'.$student);
$data['student_id'] = $student;
$this->db->insert('daily_attendances', $data);
//add this in your code-----------
$last_id = $this->db->insert_id();
$checker = array(
'school_id' => $this->school_id,
'status' => 1
);
$days_a_week = 5;
$present_days = $this->getStudentDaysByID($checker);
$data2['present'] = ($present_days / $days_a_week) * 100;
$attendance_id = $this->input->post('attendance_id');
$this->db->where('id', $last_id);
$this->db->update('daily_attendances', $data2);
//--------------------------------
endforeach;
}
//put this outside your public function
function getStudentDaysByID($checker) {
$StudentDaysByID = $this->db->get_where('daily_attendances', $checker);
return $StudentDaysByID->num_rows();
}
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!
I need to save in database category like "a & b" but the model save in database just "a" miss the blank space and the &.
This is the array:
$data = array('avenvu' => $_POST['avenvu'],
'brand' => $_POST['brand'],
'category' => $_POST['category'],
'description' => $_POST['description'],
'man_women_shop'=> $_POST['man_women_shop'],
'postdatetime' => date("Y-m-d H:i:s",time()),
'publish' => 1,
'user_id' => $this->session->userdata('user_id'));
$result = $this->personal_closest->insertCloset($data);
And this is the model:
function insertCloset($data) {
$this->db->insert('personal_closest',$data);
}
Change your insertCloset function:
function insertCloset($data) {
$personal['avenvu'] = $data['avenvu'];
$personal['brand'] = $data['brand'];
$personal['category'] = $data['category'];
$personal['description'] = $data['description'];
$personal['man_women_shop'] = $data['man_women_shop'];
$personal['postdatetime'] = $data['postdatetime'];
$personal['publish'] = $data['publish'];
$personal['user_id'] = $data['user_id'];
$this->db->insert('personal_closest',$personal);
}
This is in my model.. it says Object of class CI_DB_mysql_result could not be converted to int.. the problem is in the select_max('id');
and it always result to 1. Please help i'm new to codeigniter
public function addEstablishment()
{
$capacity = $this->input->post('capacity');
$name = $this->input->post('name');
$curfew = $this->input->post('curfew');
$price = $this->input->post('price');
$gender = $this->input->post('gender');
$type = $this->input->post('type');
$this->db->select_max('id');
$result = $this->db->get('establishment');
$query = $result + 1;
$owner = $this->session->userdata('id');
$data = array(
'id' => $query,
'name' => $name ,
'capacity' => $capacity ,
'curfew' => $curfew ,
'gender' => $gender ,
'type' => $type ,
'owner' => $owner
);
if($this->db->insert('establishment', $data))
{
echo "Successfully added";
}
}
You have not get max ID in result, please try this way
change
$this->db->select_max('id');
$result = $this->db->get('establishment');
to
$this->db->select_max('id');
$result = $this->db->get('establishment')->row()->id;