How to retrieve data between two dates in PHP codeingiter? - codeigniter

i am retrieving data between two date range and i am getting records also but when i select start date same as end date it is giving me blank even though i have record of that date in database table, only those records have different time.If anyone know solution please let me know.Below is my query to fetch records between date range.
Thanks in advance.
$this->db->select('*');
$this->db->from('transaction_tbl');
$this->db->where('user_id',$user_id);
$this->db->where('date >=',$startDate);
$this->db->where('date <=',$endDate);
$query = $this->db->get();

you need to specify time also between two dates
$startDate=date("Y m d",strtotime($startDate)).' 00:00'; //00:00 start day time
$endDate=date("Y m d",strtotime($endDate)).' 23:59'; //23:59 end day time
$this->db->select('*');
$this->db->from('transaction_tbl');
$this->db->where('user_id',$user_id);
$this->db->where('date >=',$startDate);
$this->db->where('date <=',$endDate);
$query = $this->db->get();

try this
$this->db->query("SELECT * FROM transaction_tbl WHERE user_id = '$user_id' && date BETWEEN '$startDate' AND '$endDate'");

$this->db->select('*');
$this->db->from('transaction_tbl');
$this->db->where('user_id',$user_id);
$this->db->where('date <=',$startDate);
$this->db->where('date >=',$endDate);
$query = $this->db->get();

Try this in your model.
function your_model($user_id,$startDate,$endDate){
$query=$this->db->query("SELECT * FROM transaction_tbl WHERE user_id =
$user_id && date BETWEEN $startDate AND $endDate");
return $query->result_array();
}

Related

Left join not working in codeigniter

I written query to get record from main table tpl_upload_csv_file and get related record from tbl_process_csv. I get all the records but the problem is if i have 1 id from tpl_upload_csv_file in tbl_process_csv for 5 rows its displaying 5 times. Same record is displaying 5 times.
$this->db->select('tbl_process_csv.id, tbl_process_csv.record_no, tbl_process_csv.reason,tpl_upload_csv_file.uploaded_file_name, tpl_upload_csv_file.uploaded_date_time');
$this->db->from('tpl_upload_csv_file');
$this->db->where('tbl_process_csv.process_status', 3);
$this->db->join('tbl_process_csv', 'tbl_process_csv.csv_file_id = tpl_upload_csv_file.id', 'left');
$this->db->order_by('tbl_process_csv.date_of_processing', 'desc');
$query = $this->db->get();
print_r($query->result());die;
return $query->result();
My table structure is
tpl_upload_csv_file :
id
uploaded_file_name
uploaded_date_time
records_available
tbl_process_csv :
id
csv_file_id -->(Reference if for table tpl_upload_csv_file)
record_no
process_status
reason
Try this and let me know if any problem occur:
$this->db->select('tbl_process_csv.id, tbl_process_csv.record_no, tbl_process_csv.reason,tpl_upload_csv_file.uploaded_file_name, tpl_upload_csv_file.uploaded_date_time');
$this->db->from('tpl_upload_csv_file');
$this->db->join('tbl_process_csv', 'tbl_process_csv.csv_file_id = tpl_upload_csv_file.id', 'left');
$this->db->where('tbl_process_csv.process_status', 3);
$this->db->group_by('tbl_process_csv.csv_file_id');
$this->db->order_by('tbl_process_csv.date_of_processing', 'desc');
$query = $this->db->get();
return $query->result();
$this->db->distinct('tbl_process_csv.id');
Add this line in your code.
So, you will get distinct records w.r.t tbl_process_csv.id

How to select all column from multiple tables in codeigniter using join?

I have 4 tables like this:
Now, I want to select all columns from all tables where model.featured=1. i.e.
model.id
model.name
model_attributes.id
model_attributes.attributes_value
model_images.id
model_images.model_images
attributes.id
attributes.name
attributes.value
I can only do basic level queries and I'm not sure if I'm anywhere near to the solution but this is what I have tried (returns nothing):
$this->db->select('*');
$this->db->from('model');
$this->db->join('model_images','model.id = model_images.model_id','RIGHT');
$this->db->join('model_attributes','model.id = model_attributes.model_id','RIGHT');
$this->db->join('attributes','model_attributes.attributes_id = attributes.id','RIGHT');
$this->db->where('model.featured', 1);
$query = $this->db->get();
return $query->result();
How do I achieve what I want ? Or, is there any other better methods to do it ?
// try this query
$this->db->select('*');
$this->db->from('model');
$this->db->join('model_images','model_images.model_id = model.id');
$this->db->join('model_attributes','model_attributes.model_id = model.id');
$this->db->join('attributes','attributes.id = model_attributes.attributes_id');
$this->db->where('model.featured','1');
$query = $this->db->get()->result();
return $query;
Please go through below mentioned solution.
$this->db->select('model.*,model_images.*,model_attributtes.*, attributes.*'):
Let me know if it not works for you.
Please go through below solution. If multiple table has same column name then you have to give alias for each column name which have same name in both the table. because by default CI merge all column name and generate result.
$this->db->select('*,c.name as c_name,s.name as s_name');
$this->db->from('country c');
$this->db->join('state s', 'c.id = s.country_id');
$query = $this->db->get();

2 join conditions codeigniter giving error

I have 2 tables - for orders and for documents for these orders. I want to show all orders from first table and if there are documents for these orders, to show date of documents(I take date from table documents).
I'm trying to make 2 join conditions because in table documents if I only join on idOrder, there are more than 1 row and I have to join on documents.name to be same as $this->uri->segment(3).
But I'm using the following code and it gives me error. Could I use 2 join conditions in this way:
My model is:
<?php
public function get_all_orders($user_id){
$this->db->select('ordersheader.*, customer.name as customerName,documents.date_make');
$this->db->from('ordersheader');
$this->db->join('customer', 'ordersheader.idCustomer = customer.idCustomer');
$this->db->join('documents', 'ordersheader.idOrder = documents.idOrder','left');
$this->db->join('documents as D', 'D.name="declaration"','left');
$this->db->where('ordersheader.user_id', $user_id);
$query = $this->db->get();
return $query->result_array();
}
Edited: I found solution. But it's written in pure sql, how to write it with Codeingiter syntax? My new working code is:
<?php
$query=("SELECT ordersheader.*, customer.name as customerName,documents.date_make,documents.deactivated_at
FROM ordersheader JOIN customer ON ordersheader.idCustomer = customer.idCustomer
LEFT JOIN documents
ON ordersheader.idOrder = documents.idOrder AND documents.name = '".$document."'
WHERE ordersheader.user_id = '".$user_id."' ");
$result = $this->db->query($query);
return $result->result_array();
This is wrong in your query
$this->db->join('documents as D', 'D.name="declaration"','left');
you can compare column using where clause
public function get_all_orders($user_id){
$this->db->select('ordersheader.*, customer.name as customerName,documents.date_make');
$this->db->from('ordersheader');
$this->db->join('customer', 'ordersheader.idCustomer = customer.idCustomer');
$this->db->join('documents', 'ordersheader.idOrder = documents.idOrder','left');
// $this->db->join('documents as D', 'D.name="declaration"','left'); / comment this line
$this->db->where('documents.name', 'declaration');
$this->db->where('ordersheader.user_id', $user_id);
$query = $this->db->get();
return $query->result_array();
}

codeigniter active record class

this is my normal mysql command
$query = $this->db->query("SELECT `house_details`.`houses_id` , `house_details`.`house_dis` , `house_details`.`house_type` , `house_details`.`area` , `house_details`.`cost` , `house_details`.`user_id` , `image`.`thumb_path`
FROM (
`house_details`
)
LEFT JOIN `image` ON `image`.`house_id` = `house_details`.`houses_id`
AND `house_details`.`status` =1
GROUP BY `houses_id` DESC
LIMIT $start,$limit ");
return $query->result();
what is the similar code with CI active record class ??
Try this,
$this->db->select('house_details.houses_id, house_details.house_dis, house_details.house_type,house_details.area,house_details.cost,house_details.user_id,image.thumb_path');
$this->db->from('house_details');
$this->db->join('image', 'image.house_id = house_details.houses_id','left');
$this->db->where('image.status', 1);
$this->db->order_by("house_details.houses_id DESC");
$this->db->limit($limit, $start);
$query = $this->db->get();
return $query->result();
Assumed there is no group by since you wanted to order result set in descending order. If group by is there changed it to group_by and remove 'DESC'.
Try this:
$this->db->select('...')->
from('house_details')->
join('image','image.house_id=house_details.houses_id AND image.status=1','LEFT')->
where('...')->
order_by('house_details.houses_id','DESC')->
limit($start,$limit);

Return the number of rows with where condition

In Codeigniter, how do I return the number of rows with a where condition?
$this->db->where('title', 'Book');
$this->db->from('my_table');
echo $this->db->count_all_results();
// Produces an integer, like 17
You can also do like:
$this->db->where('firstname','gautam');
$query = $this->db->get('rebels');
return $query->num_rows();
it will also return the number of rows resulting... :-)
Use count() :
$this->db->where('title', 'abc');
$this->db->from('articles');
$query = $this->db->get();
return count($query->result());

Resources