Get the latest record with limit in codeigniter - php

My model
public function getLogs()
{
$this->db->limit(5);
$query = $this->db->get('tbllogs');
return $query->result();
}

Try this
$this->db->order_by("id", "desc");
$this->db->limit(5);
$query = $this->db->get('tbllogs');
return $query->result();
Or for old and latest logic make two links in your view with orderBy query parameter in it.
Old
Latest
In your controller method
if ($this->input->get('orderBy') == 'old') {
$this->db->order_by("id", "asc");
} else {
$this->db->order_by("id", "desc");
}
$this->db->limit(5);
$query = $this->db->get('tbllogs');
return $query->result();

With where condition
$result = $this->db->order_by('id', 'DESC')->limit(5)->get_where($tablename,$where);

Related

how to get json value on condition from database in codeigniter

public function cmplFirstRdDetails($userMobileNo) {
$this->db->from('records');
$this->db->like('player1', '{"PhoneNumber":"$userMobileNo"}');
$query = $this->db->get();
return $query->result();
}
This code is not working for me.

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 model function with and / or query

I have this function in model
function select($table,$match,$or){
$this->db->select('*');
$this->db->from($table);
$this->db->where($match)->or_where($or);
$query = $this->db->get();
$query_result = $query->result();
return $query_result;
}
and in controller
$post = array("class"=>"XI","section"=>"A","stream"=>"Medical");
$data = $this->main_model->select("class_subjects",array("class"=>$post['class'],"section"=>"all","stream"=>$post['stream']),$post);
print_r($data);
I am not getting any error but this is printing whole data of table. how can i match class='XI' and stream='Medical' and (section='A' or section='all')
Try this
function select($table,$match,$or)
{
$query = $this->db
->select("*")
->from($table)
->group_start()
->where($match)
->group_end()
->group_start()
->or_where($or)
->group_end()
->get();
$strSql = $this->db->last_query();
return $query->result();
}
if that won't work - print out the $strSql Variable and post the Query here.

fetching products of current user_id

I have function to fetch the products from ci_products table
the code is as follows
function get_product_selections($product_ids = array())
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where_in('product_id', $product_ids);
$products = $this->db->get();
return $products;
}
now I want to fetch from where user_id is current user_id
I have tried few things but not working
what I have tried`
function get_product_selections($product_ids = array())
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where_in('product_id', $product_ids);
$this->db->where('user_id',$this->session->userdata('user_id'));
$products = $this->db->get();
return $products;
}
$data = $this->db->get_where('product_ids',$product_ids);
$this->db->where_in('product_ids',$data);
$this->db->where('user_id',$this->session->userdata('user_id'));
$query = $this->db->get();
return $query->result_array();
Try this one :
In your first code:
function get_product_selections($product_ids)
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where('product_id', $product_ids);
$products = $this->db->get();
return $products->result_array();
}
what do you mean when you put array() in the parameter? you want to retrieve an array() of products?
function get_product_selections()
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where('user_id',$this->session->userdata('user_id')); // the user who is currently login
$products = $this->db->get();
return $products->result_array(); // return the results
}

I am having an issue with my order by in codeigniter

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

Categories