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;
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
Im having trouble loading an array into the templatevars variable
It seems to be unidentified everytime i run it
and it doesnt seem to be putting the array into the variable
I would appreciate any help i can get with this Thanks
Here is the code I'm using
<?php
require '../template.php';
$title = 'List';
if (isset($_SESSION['login']) && $_SESSION['login'] == true) {
if (isset($_POST['submit'])) {
$jokes = find($pdo, 'jokes');
$applications = $pdo->prepare('SELECT count(*) as count FROM applications WHERE jokeId = :jokeId');
$applications->execute(['jokeId' => $jokes['id']]);
$applicantNumber = $applications->fetch();
$categoryList = $pdo->prepare('SELECT * FROM category WHERE id = :id');
$values = [
'id' => $jokes['categoryid']
];
$categoryList->execute($values);
$categoryName = $categoryList->fetch();
$filterCity = find($pdo, 'jokes');
$templateVars = array(
'jokes' => $jokes,
'category' => $categoryList,
'applications' => $applications,
'jokes' => $filterCity,
'categoryList' => $categoryName,
'applications' => $applicantNumber
);
} else {
$jokes = find($pdo, 'jokes');
$output = loadTemplate('../Templates/jobListing.html.php', $templateVars);
}
} else {
$output = loadTemplate('../Templates/login.html.php', []);
}
require '../Templates/layout.html.php';
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.
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);
}
How can I validate if the data is successful or not using CodeIgniter? I search the codeigniter manual I found out that getting num_rows() and affected_rows() can help me but how to use this for validation purposes? The example in the web is using SELECT statement and not INSERT or UPDATE. Here's a bit of my code.
public function create_user() {
$username = $this->input->post('username');
$password = md5($this->input->post('password'));
$lastname = ucwords($this->input->post('lastname'));
$firstname = ucwords($this->input->post('firstname'));
$email = $this->input->post('email');
$phone1 = $this->input->post('tel1');
$phone2 = $this->input->post('tel2');
$c_firstname = ucwords($this->input->post('c_firstname'));
$c_lastname = ucwords($this->input->post('c_lastname'));
$c_bldg = $this->input->post('c_bldg');
$c_address = $this->input->post('c_address');
$c_barangay = $this->input->post('c_barangay');
$c_city = $this->input->post('c_city');
$c_state = $this->input->post('c_state');
$c_country = $this->input->post('c_country');
$c_postal = $this->input->post('c_postal');
$c_tel = $this->input->post('c_tel');
$date = strtotime(date('Y-m-d H:i:s'));
$insert_user = array(
'user_login' => $username,
'timestamp' => $date,
'password' => $password,
'firstname' => $firstname,
'lastname' => $lastname,
'email' => $email,
'phone' => $phone1
);
$insert_user_data = $this->db->insert('cscart_users',$insert_user);
//validate if data is inserted here. If inserted successfully proceed to next INSERT.
$get_id = $this->db->insert_id();
$insert_user_profile = array(
'user_id' => $get_id,
'b_firstname' => $c_firstname,
'b_lastname' => $c_lastname,
'b_address' => $c_address,
'b_state' => $c_state,
'b_city' => $c_city,
'b_country' => $c_country,
'b_zipcode' => $c_postal,
'b_phone' => $c_tel
);
$this->db->insert('cscart_user_profiles',$insert_user_profile);
}
I assume that your tble has auto inc set in your prmary key, codeigniter's function insert_id returns an int value if I recall it correctly; 0 if insert is unsuccessful and of coarse the incremented id if it is, so it's safe to say that you could use those value instead.
//validate if data is inserted here. If inserted successfully proceed to next INSERT.
$get_id = $this->db->insert_id();
//check the value if it is 0 or not
if($get_id > 0){
$insert_user_profile = array(
'user_id' => $get_id,
'b_firstname' => $c_firstname,
'b_lastname' => $c_lastname,
'b_address' => $c_address,
'b_state' => $c_state,
'b_city' => $c_city,
'b_country' => $c_country,
'b_zipcode' => $c_postal,
'b_phone' => $c_tel
);
$this->db->insert('cscart_user_profiles',$insert_user_profile);
}