Codeigniter subtracting time and making an selection from database in model - php

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;
}

Related

How to call 2 model function in one function controller in codeigniter

I am new to codeigniter, so please help me to solve my problem.
I want to call my two model function in one function controller. Heres my two model function:
public function getposts($postid){
$this->db->select('*');
$this->db->from('tbl_endangered');
$this->db->where(['endangeredid'=>$postid]);
$query = $this->db->get();
return $query->result();
}
public function getSinglePost($postid){
$this->db->select('*');
$this->db->from('tbl_images');
//$this->db->join('tbl_images');
$this->db->where(['endangeredid'=>$postid]);
$query = $this->db->get();
return $query->result();
}
And here is my function controller:
public function viewAnimals($postid){
if(!$this->session->userdata("id") )
return redirect("AuthCon/login");
$this->load->model('Show');
$posts = $this->Show->getSinglePost($postid);
//$posts = $this->Show->getposts($postid);
$this->load->view('Showimage',['posts'=>$posts]);
}
In that code above I always get the first one that I declared. I want to fetch the getposts and getSinglePost inside the function viewAnimals controller.
Hopeyou can help me. Thanks in advance
I think using same variable name is creating a mess.
you can try assigning to posts array with output in it.
$this->load->model('Show');
$posts['getSinglePostData'] = $this->Show->getSinglePost($postid);
$posts['getPostsData'] = $this->Show->getposts($postid);
$this->load->view('Showimage',$posts);
Do like this in the controller
public function viewAnimals($postid){
if(!$this->session->userdata("id"))
return redirect("AuthCon/login");
$this->load->model('Show');
$data['images'] = $this->Show->getSinglePost($postid);
$data['endangered'] = $this->Show->getposts($postid);
$this->load->view('Showimage', $data);
}
Another way is to use join() and only one model function
public function getSinglePost($postid){
$this->db->select('*');
$this->db->from('tbl_images');
$this->db->join('tbl_endangered', 'tbl_endangered.endangeredid = tbl_images.endangeredid');
$this->db->where('tbl_images.endangeredid', $postid);
return $this->db->get()->result();
}

PHP: can't insert data to database in web hosting, but in local it's working using CI

I'm writing PHP using CI. I inserted data, but got an error like this:
Code:
class Recruit extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->model('Resume_model');
$this->load->helper(array('form','url'));
}
public function index() {
$this->data['posts'] = $this->Resume_model->get_resume();
$this->load->view('recruit', $this->data);
}
public function resume_form() {
echo 'OK';
$test = $this->input->post('type');
echo 'test:',$test;
echo 'KO';
$save = array(
'type' =>$this->input->post('type'),
'fullname' =>$this->input->post('fullname'),
'tel' =>$this->input->post('tel'),
'location' =>$this->input->post('location')
);
$this->Resume_model->saveResume($save);
redirect(base_url()."recruit");
}
Class Resume_model extends CI_Model {
function saveResume($data) {
{
$this->db->insert('resume', $data);
$resume_id = $this->db->insert_id();
}
return $resume_id;
}
function get_resume() {
$this->db->select("fullname,type");
$this->db->from('resume');
$query = $this->db->get();
return $query->result();
}
function getResume() {
$query = $this->db->query('SELECT * FROM resume');
if($query->num_rows() > 0 ) {
return $query->result();
} else {
return array();
}
}
public function deleteResume($id) {
$this -> db -> where('id', $id);
$this -> db -> delete('resume');
}
Make your fields in the DB to allow NULL input.
The reason is simple, your localhost environment and your online environment are different. Different MySQL configurations or different versions causes these errors.
Sometimes there is a need for putting in an empty record into a reference table for future use or because of the order of your coding event but to fix it you have to either put data into your columns upon inserting or setting your column (type) to 'NULL' or 'NULLABLE' from the DB.

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();
}

Return a single row in model, pass it to controller and view - Codeigniter

Sorry for posting such a noob question, but I've had hard times when working with returning a single
row from database and pass it to model. Here's my method from my model:
public function test($user_id)
{
$query = $this->db->query("SELECT COUNT(*) AS test FROM test WHERE user_id = '.$user_id.'");
return $query->first_row('array');
}
Here's an example of my controller with some other returned value from my model:
class MY_Controller extends CI_Controller
{
public $layout;
public $id;
public $data = array();
public function __construct()
{
parent::__construct();
$this->output->nocache();
$this->load->model('subject_model');
$this->load->model('user_model');
$this->load->model('survey_model');
$this->id = $this->session->userdata('user_id');
$this->data['check_if_already_posted_it_survey'] = $this->survey_model->checkIfAlreadyPostedSurvey('it_survey', $this->id);
$this->data['check_if_already_posted_lvis_survey'] = $this->survey_model->checkIfAlreadyPostedSurvey('lvis_survey', $this->id);
$this->data['test']= $this->survey_model->test($this->id);
$this->layout = 'layout/dashboard';
}
I can pass all the values from that data array to my view except "test". I've basically tried everything.
CheckIfAlreadyPostedSurvey method will return
number of rows with num_rows and I can easily print the value from them in my view by writing:
<?=$check_if_already_posted_it_survey?>
What should I do to print out that "test" in my view?
Thanks in advance and apologizes...
I probably got the question wrong, but have you tried
echo $this->survey_model->test($this->id);
Try this:
public function test($user_id)
{
$query = $this->db->query("SELECT COUNT(*) AS test FROM test WHERE user_id = '".$user_id."'");
return $query->row()->test;
}
This will return a single row as an object and then the referenced row name, which in this case is test.
The following will also work.
public function test($user_id)
{
$query = $this->db->query("SELECT * FROM test WHERE user_id = '".$user_id."'");
return count($query->result());
}
You also messed up the quoting in your SQL statement.

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