I am having an issue with my order by in codeigniter - php

For some reason when I do order by Date_Scheduled desc it is still leaving the oldest date first. I am trying to get it so that the newest date shows first and oldest date last. Not sure is I am writing this correctly.
Here is my model it is the get employee function.
<?php
class Employee_model extends CI_Model
{
public function insert_employee($data)
{
$this->db->insert('employee_list',$data);
return $this->db->insert_id();
}
public function get_employee()
{
$this->db->select('*');
$this->db->from('employee_list');
$this->db->where('status',1);
$this->db->order_by ("Date_Scheduled", "desc");
$this->db->order_by("Scheduled_Area", "desc");
$this->db->order_by("Time_Reported", "desc");
$query =$this->db->get();
return $query->result();
}
public function delete_employee($id,$data)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return print_r($data);
}
public function edit_employee($id)
{
$this->db->select('*');
$this->db->from('employee_list');
$this->db->where('id',$id);
$this->db->where('status',1);
$query =$this->db->get();
return $query->result();
}
public function update_employee($data,$id)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return print_r($data);
}
}

This answer solves part of your problem, because you should handle dates with the correct column type date and not varchar.
If the result of your query always returns ORDER BY ASC, you can flip the array.
array_reverse($this->Employee_model->get_employee());

Related

Join 3 tables with codeigniter and display it

I want to create a join database. in the 'relation' table will show 'disease' which can add many 'symptoms'. How should I do it?
Here is my database:
disease : id_disease, name, details
symptoms : id_symptoms, name, details
relation : id_relation, id_disease, id_symptoms
and here is my model
function get_allrelation() {
$this->db->select('*');
$this->db->from('relation');
$this->db->join('disease','disease.id_disease = relastion.id_disease','left');
$this->db->join('symptoms','symptoms.id_symptoms= relation.id_symptoms','left');
$query = $this->db->get();
return $query->result();
}
}
Please help me.
You had a typo: instead of relation you had relastion.id_disease.
Code below should work
function get_allrelation() {
$this->db->select('*');
$this->db->from('relation');
$this->db->join('disease','disease.id_disease = relation.id_disease','left');
$this->db->join('symptoms','symptoms.id_symptoms= relation.id_symptoms','left');
$query = $this->db->get();
return $query->result();
}
The controller below is a example only in the select $this->db->select('relation.*, disease.*, symptoms.*');
Oon the controller you can pass the data to the view the create a foreach loop in the view.
<?php
class Somecontroller extends CI_Controller {
public function index() {
$data['allrelation'] = $this->allrelation();
$this->load->view('example', $data);
}
function get_allrelation() {
$this->db->select('relation.*, disease.*, symptoms.*');
$this->db->from('relation');
$this->db->join('disease','disease.id_disease = relastion.id_disease','left');
$this->db->join('symptoms','symptoms.id_symptoms = relation.id_symptoms','left');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
}
return false
}
}

Codeigniter subtracting time and making an selection from database in model

I am building a library project where a user needs to know which books have not been submitted on time.So for that i have stored book_returning_date already in the database.However my issue here is that i need to know how to select the dates in model which have crossed there return date by comparing with today date.My model code is here as follow:
public function timecalculations($table){
$query= $this->db->get_where('issue_books',array('department_id'=>$table));
return $query->result();
}
and my controller is as here follows:
public function latebooks(){
$this->load->model('Time');
$id=$this->session->userdata('userid');
$this->load->model('Department');
$table=$this->Department->selecttable($id);
foreach($table as $q){}
$table = $q->department_name;
$table = strtolower($table);
$run=$this->Time->timecalculations($table);
$this->load->view('Books/datetime',['query'=>$run]);
}
I am new in codeigniter so please forgive as i may be having a little problem with the logic.Thanks in advance!
try this
public function timecalculations($table){
$query= $this->db->where('book_returning_date < CURRENT_DATE()', NULL, FALSE)->get($table);
return $query->result();
}
add another where clause,
public function timecalculations($table){
$this->db->where('book_returning_date < NOW()');
$query = $this->db->get($table);
return $query->result();
}
this will return the rows where current date is already crossed book_returning_date
i am assuming u are saving the book_returning_date in the following format 'yyyy-mm-dd H:i:s'
probably like this and sorry if i'm mistake
First need to Set Library Session setting in config
for model :
public function timecalculations($table){
$condition = 'department_id = "'.$table.'"';
$this->db->select('issue_books');
$this->db->where($condition);
$query= $this->db->get();
return $query->result();
}
for controller i'm not sure but like this :
public function __construct() {
$this->load->model('Time', 'time');
$this->load->model('Department', 'departement');
$this->load->library('Session');
}
public function latebooks($id){
$id=$this->session->userdata('userid');
$table=$this->department->selecttable($id);
foreach($table as $q){ (probably i'm wrong)
$table = $q->department_name; (probably i'm wrong)
} (probably i'm wrong)
$table = strtolower($table); (probably i'm wrong)
$run=$this->time->timecalculations($table);
$this->load->view('Books/datetime',['query'=>$run]);
}
Try this
In Controller
public function latebooks()
{
$this->load->model('Time');
$this->load->model('Department');
$id = $this->session->userdata('userid');
$result = $this->Department->selecttable($id); # Changed
$table = $result[0]['department_name']; # Changed
$table = strtolower($table); # Changed
$run = $this->Time->timecalculations($table);
$this->load->view('Books/datetime',['query'=>$run]);
}
In Model
public function timecalculations($table)
{
$query = $this->db->get_where('issue_books',array('department_id'=>$table));
$result = $query->result_array();
return $result;
}

Codeigniter JOIN multiple tables

I'm having a little trouble in retrieving data in multiple tables using codeigniter.
This is the code i'm using to retrieve data in my model which is working well.
function retrieve_experience($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;
}
function retrieve_education($alumni_id)
{
$this->db->select('*');
$this->db->from('education');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;
}
Now i tried using a simplified code but fails to display the data. here is the code in my model
function retrieve_all_data($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->join('education','education.alumni_id=experience.alumni_id');
$this->db->where('experience.alumni_id',$alumni_id);
$query=$this->db->get();
return $query->result_array();
}
In my controller, i used this code to retrieving data in my model
function display()
{
$alumni_id = $this->session->userdata('alumni_id');
$data['all_data'] = $this->Alumni_model->retrieve_all_data($alumni_id);
$data['main_content'] = 'alumni_home';
$this->load->view('includes/template', $data);
}
and for the display i used this code
foreach($all_data as $results)
{
/** data from experience table **/
$results['company_name'];
$results['company_address'];
/** data from education table **/
$results['school_name'];
$results['field_of_study'];
}
I cant display anything at all. Please help
Try the below code,
I believe you'll want something like this:
function retrieve_all_data($alumni_id)
{
$this->db->select("e.*,edu.*");
$this->db->from("experience e");
$this->db->join("education edu", "edu.alumni_id = e.alumni_id",'left');
$this->db->where('e.alumni_id',$alumni_id);
$this->db->group_by('e.exp_id');
$query = $this->db->get();
return $query->result_array();
}
Hope below mentioned function should return data that you expected.
function retrieve_all_data($alumni_id)
{
$this->db->select('*');
$this->db->from('experience ex');
$this->db->join('education ed','ed.alumni_id=ex.alumni_id');
$this->db->where('ex.alumni_id',$alumni_id);
$query=$this->db->get();
return $query->result_array();
}

Not Sure How to call Store Procedure in Codeigniter

I am having an issue calling a stored procedure in my model not sure if I am doing it right the screen is coming up blank, also do i need to auto load anything to call a stored procedure?
<?php
class Employee_model extends CI_Model
{
public function insert_employee($data)
{
$this->db->insert('employee_list',$data);
return $this->db->insert_id();
}
public function get_employee()
{
$this->db->query("call {storedprocedure Select_employeelist} ");
$query =$this->db->get();
return $query->result();
}
public function delete_employee($id,$data)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return print_r($data);
}
public function edit_employee($id)
{
$this->db->select('*');
$this->db->from('employee_list');
$this->db->where('id',$id);
$this->db->where('status',1);
$query =$this->db->get();
return $query->result();
}
public function update_employee($data,$id)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return print_r($data);
}
}
To call a procedure from codeigniter
Consider test_proc as your procedure. Then it should be
$this->db->query("call test_proc()");
If you need to to send parameters then
$query = $this->db->query($test_proc,array('id'=>'1','name'=>'test','address'=>'abc'));
To see the result then
print_r($query);

Codeigniter - Calling a model method within same model is buggy

I am trying to call a model method within same model and its not working as intended. Here is my class with two methods which do not work
class mymodel extends CI_Model{
public function __construct(){
parent::__construct();
$this->tablea = 'tablea';
$this->tableb = 'tableb';
}
public function saveData($data){
$dataCopy['revisionkey'] = $this->getRevisionKey($data['id']);
//check and condition revision key to be int with +1 from last one
$this->db->insert($this->tableb, $dataCopy);
$this->db->where('id', $id);
return $this->db->update($this->user_table, $data) ? true : false;
}
public function getRevisionKey($id){
$this->db->select($this->revision_tablea.'.revisions_number as revisions_number')
->from($this->revision_tablea)
->where($this->revision_tablea.'.id', $id)
->order_by($this->revision_table.'.revisions_number', 'asc')
->limit(1);
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->row_array();
}else{
return 0;
}
}
}
Now method getRevisionKey() should produce a query like following
SELECT `tableb`.`revisions_number` as revisions_number FROM (`tableb`) WHERE `tableb`.`id` = '26' ORDER BY `tableb`.`revisions_number` asc LIMIT 1
but it produces a query like following
SELECT `tableb`.`revisions_number` as revisions_number FROM (`tableb`) WHERE `id` = '26' AND `tableb`.`id` = '26' ORDER BY `tableb`.`revisions_number` asc LIMIT 1
This of course is due to same method being called within the model, this method works fine if used outside of model. Any solution to this problem?
EDIT
Rewriting the getRevisionKey() fixes this. Here is the new version
public function getRevisionKey($id){
$sqlQuery = $this->db->select($this->revision_tablea.'.revisions_number as revisions_number')
->from($this->revision_tablea)
->order_by($this->revision_table.'.revisions_number', 'asc')
->limit(1);
$query = $sqlQuery->where($this->revision_tablea.'.id', $id)->get();
if ($query->num_rows() > 0){
return $query->row_array();
}else{
return 0;
}
}
Here is a simple hack that will give you exactly where you are making a mistake.
Go to system/database/DB_active_rec.php Remove public or protected keyword from these functions
public function _compile_select($select_override = FALSE)
public function _reset_select()
And save it. Before running the function i mean calling
$this->db->get() // Use $this->db->from() instead
use
$query = $this->db->_compile_select()
and echo $query;
These two functions also help in subquery in codeigniter active record.
How can I rewrite this SQL into CodeIgniter's Active Records?

Categories