Select query with Date of Today not working - codeigniter

I'm trying to select from table Documents where Date is Today's date. I'm using Codeigniter.
But it shows only these documents where there isn't hour. I mean where in database date is: 2015-06-25 00:00:00. But it doesn't display these documents where there is an hour - for example : 2015-06-25 08:34:00.
How to display all documents where Date is current date , no matter what is the hour.
<?php
$date = new DateTime("now");
$curr_date = $date->format('Y-m-d ');
$this->db->select('*');
$this->db->from('documents');
$this->db->where('Date', $curr_date);
$query = $this->db->get();
return $query->result();

Try this
<?php
$date = new DateTime("now");
$curr_date = $date->format('Y-m-d ');
$this->db->select('*');
$this->db->from('documents');
$this->db->where('DATE(Date)',$curr_date);//use date function
$query = $this->db->get();
return $query->result();
MySQL date Function

Related

Laravel groupBy date get total column

I want to report before the selected date and the entered day. Dates that are not before the selected date and the number of days are also coming. I don't want empty dates to appear and I want to show the total column data in the database for that date. Where am I doing wrong?
I want to make a comparison between incoming values to change the table background-color
Database Structure
Table Structure
public function listByDaily(Request $request)
{
$endDate = $request->date('date') ?? today();
$startDate = $endDate
->copy()
->subDays($numberofdays);
$dates = CarbonPeriod::create($startDate, $endDate);
$transactions = Transactions::whereBetween('date', [$startDate, $endDate])
->select('product_id', 'supplier_id', 'total', 'date')
->latest('date')
->get();
$productIds = $transactions
->pluck('product_id')
->unique()
->values()
->toArray();
$supplierIds = $transactions
->pluck('supplier_id')
->unique()
->values()
->toArray();
$productselect = Product::whereIn('id', $productIds)
->select('id', 'product_name')
->pluck('product_name', 'id');
$suppliers = Supplier::whereIn('id', $supplierIds)
->select('id', 'supplier_name')
->get();
$columns = collect($transactions)
->whereNotNull('date')
->groupBy('date','DESC')
->map(function($items) {
return $items
->groupBy('product_id','total');
})
->pluck('total');
//dd($columns);
/*->toArray();*/
$products = Transactions::join('products', 'products.id', '=', 'transaction.product_id')->get()->unique();
return view('transactions.reportbydates', compact('products', 'productIds', 'dates', 'suppliers', 'columns'));
}

How to convert this SQL Query on Code Igniter Model

Good Day Masters,
Can anyone help me how to convert this SQL Query into Code Igniter format (model).
SELECT firstName, FLOOR(DATEDIFF(CURRENT_DATE, birthDate)/365.25) as age FROM residents_tbl WHERE FLOOR(DATEDIFF(CURRENT_DATE, birthDate)/365.25) >= 18
I don't know how to write it on WHERE clause.
$query = $this->db->select('*');
$query = $this->db->from('residents_tbl');
**$query = $this->db->where('isHead', '1');**
$query = $this->db->order_by('lastName', 'ASC');
$query = $this->db->get('', 15, $this->uri->segment(3));
if ($query->num_rows() > 0) {
return $query->result();
}
TIA.
This is a simplified version with chaining. I just changed the type of 1 from string to number which might caused the problem.
$query = $this->db
->where('isHead', 1)
->get('residents_tbl')
->order_by('lastName', 'ASC');

Codeigniter db_where in 3 joined tables

So this code is a combination of 3 table joined, now i want to filter the query where only the request that a user can have is his/her own request and not from other user.. the problem is in where clause i have duplicated.
$this->db->order_by('loanrequest.ApplicationNo', 'DESC');
$query = $this->db->get('loanrequest','loanapplication','user');
$this->db->join('loanapplication','loanrequest.ApplicationNo = loanapplication.ApplicationNo');
$this->db->join('user', 'user.userId = loanapplication.userId');
$this->db->where('loanapplication.userId', $this->session->userdata('userId'));
$this->db->where('loanapplication.ApplicationNo', 'loanrequest.ApplicationNo');
return $query->result_array();
I can not get what you have done in your join query but Here is how I do join in my CodeIgniter model.
You don't need to put this in where condition:
$this->db->where('loanapplication.ApplicationNo', 'loanrequest.ApplicationNo');
Because you have already done that in the join query.
$query = $this->db->select('*')
->from('loanapplication')
->join('loanrequest', 'loanrequest.ApplicationNo = loanapplication.ApplicationNo', 'left')
->join('user', 'user.userId = loanapplication.userId', 'left')
->where_in('loanapplication.userId',$this->session->userdata('userId'))
->order_by('loanrequest.ApplicationNo', 'DESC')
->get();
return $query->result_array();
I will recommend you should use $this->db->last_query() because it gives you a better idea about your query.
$query = $this->db->select('*')
->from('loanapplication')
->join('loanrequest', 'loanrequest.ApplicationNo = loanapplication.ApplicationNo', 'left')
->join('user', 'user.userId = loanapplication.userId', 'left')
->where_in('loanapplication.userId',$this->session->userdata('userId'))
->order_by('loanrequest.ApplicationNo', 'DESC');
$result = $query->get();
echo $this->db->last_query();exit; //use this to print your query so that you can get the actual issue
if ($result->num_rows() > 0) {
return $result->result_array();
} else
return '';

Getting last inserted records not only one record but multiple records

I am inserting ten to 11 records at a time through a textarea and I want them to show only those records not all other records.
I am using codeigniter. so I need a query in codeigniter.
$this->db->select("*");
$this->db->from('try2check_cardinfo');
$this->db->limit(1);
$this->db->order_by("id","DESC");
$q =$this->db->get();
echo "<pre>";
$r = $q->result_array();
$iddd = $r[0]['id'];
$this->db->select("*");
$this->db->from("try2check_cardinfo");
// $this->db->limit(3);
$this->db->order_by("id","desc");
$this->db->where('id >',$iddd);
$query = $this->db->get();
$result = $query->result();
return $result;

how to fetch all records from order table in Codeigniter

$data is an array which contain user post data for fetch record from orders table
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$this->db->get();
$data = array(
'customer_id' => $this->input->post('custId')],
'paided' => 2
);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$this->db->get();
try this :
public function function_name (){
$data = array (
'customer_id' => $this->input->post('custId'),
'paided' => 2
);
$this->db->select('*');
$this->db->from('ordere');
$this->db->where($data);
$query = $this->db->get();
return $query->result_array();
}
You have done all good just need to put result() if you get multiple row or row() if you get one row
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$result= $this->db->get()->result(); //added result()
print_r($result);
as simple use
$custId = $_post['custId'];
$query = $this->db->query("SELECT * FROM orders WHERE customer_id= '$custId' AND paided='2'");
$result = $query->result_array();
return $result;//result will be array
This is the plus of using framework, you don't need to write that much of code,
$where = array('customer_id' => $this->input->post('custId'),'paided'=>2)
$result = $this->db->get_where('orders', $where);
and for fetching them, use $result->row() for single record retrieval.
If you want all records, use $result->result()
Here is documentation link, if you want to learn more.
What You should need to be Correct And Why
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*'); // by defaul select all so no need to pass *
$this->db->from('orders');
$this->db->where($data);
$this->db->get(); // this will not return data this is just return object
So Your Code Should be
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select(); // by defaul select all so no need to pass *
$this->db->from('orders');
$this->db->where($data);
$query = $this->db->get();
$data = $query->result_array();
// or You can
$data= $this->db->get()->result_array();
here result_array() return pure array where you can also use result()
this will return array of object

Resources