Select data from multiple date columns with and conditions in Code Igniter - php

i want to get data from database with multiple AND conditions on multiple datetime columns.
for example
$this->db->select('*');
$this->db->from('BaseTbl');
$this->db->where('remarkDate<=',date('Y-m-d') );
$this->db->where('createdDate!=',date('Y-m-d') );
$query = $this->db->get();
$result = $query->num_rows();
return $result;
but its not fetching correct result. please help.

$this->db->select('*');
$this->db->from('BaseTbl');
$this->db->where('date(remarkDate) <=',date('Y-m-d') );
$this->db->where('date(createdDate) !=',date('Y-m-d') );
$query = $this->db->get();
$result = $query->num_rows();
return $result;
Hope this will work

You can try this by putting a space after remarkDate and createdDate, it should work.
EX::
$this->db->where('remarkDate <=',date('Y-m-d') );
$this->db->where('createdDate !=',date('Y-m-d') );

Related

Currently I get the result of my query but I want to get count of records

I want to get a count of my query in a number currently it shows all records
Controller:
public function fetchCounts(){
$Result = $this->receivedModel->vehicleFreight($this->session->userdata('user_branch'));
}
Model:
public function vehicleFreight($branch_id)
{
$this->db->select('*');
$this->db->from('truck_freight');
$this->db->where('truck_freight_status','truck_freight');
$this->db->where('fr_memo_to',$branch_id);
$query = $this->db->get();
return $query->result();
}
How can I use count() in my query.
In my data base I have 3 records.
Try this
Model
public function vehicleFreight($branch_id)
{
$this->db->select('count(*)');
$this->db->from('truck_freight');
$this->db->where('truck_freight_status','truck_freight');
$this->db->where('fr_memo_to',$branch_id);
$query = $this->db->get();
return $query->result();
}
Many ways to get count
1 . echo count($query->result());
2 . echo $query->num_rows();
3 . You can use `count(*)` in `select` statement
To get the count of fetched records,
$data = $query->result();
echo count($data); // use this as per your requirements
return $data;
You can use following function to return number of results.
return $this->db->count_all_results();
This should be used in place of get()
$query = $this->db->get();
Please check documentation
change the code as follows:
$results = $query->result();
$count = count($results);
return $count

PHP Activerecord Model Count

Get result as below
$result = MyModel::find_by_id($id);
How can I count the result number directly?
Try the simple solution
$count = $this->db->get('table')->num_rows();
But if you want data as well then
$data = $this->db->get('table')->result();
$count = count($data);
Try this,
$this->db->select('*');
$this->db->from('emp');
$query = $this->db->get();
if($query->num_rows()>0){
return count($query->result());//here you can get count
//return count($query->result_array());//here you can get count
}
OR
echo $this->db->count_all('emp');
OR
$this->db->like('fname', 'eve');
$this->db->from('emp');
echo $this->db->count_all_results();

get num_rows and result() data in same function on codeigniter

hı,
how can ı return get result data and result num rows in same functions
ı try with this function but it is not run
$user_id=$this->session->user_sess['id'];
$this->db->select('*');
$this->db->where('uto', $user_id);
$this->db->where('isread','0');
$this->db->from('user_messages');
$query=$this->db->get();
$q['id']=$query->result();
$q['data']=$query->num_rows();
return $q;
$user_id=$this->session->user_sess['id'];
$this->db->select('*');
$this->db->where('uto', $user_id);
$this->db->where('isread','0');
$this->db->from('user_messages');
$query=$this->db->get();
$q['result']=$query->result();
$q['rows']=$query->num_rows();
return $q;
controller:
$result = $this->some_model->my_model_function();
$num_rows = $result['rows'];
$data = $result['result'];
Try this one out...
$user_id=$this->session->user_sess['id'];
$query= $this->db->query("SELECT * from user_messages WHERE uto = '".$user_id."' AND isread = '0' ");
return $query->num_rows();
hope that helps.
you may try with storing result set into two variable like : -
$query=$query1= $this->db->select('*')->from('table_name')->get();
echo $query->num_rows(); //number of rows
print_r($query1->result()); // result object
second approach by using result set object itself like :-
$query=$this->db->select('*')->from('table_name')->get();
echo $query->result_id->num_rows; //number of rows
print_r($query->result()); // result object

Codeigniter db select where as an array

I have this code i need to use to get results from a database, I'm on codeigniter. This is how the query looks like,
$this->db->select('*');
$this->db->where('broadcast_id', $bid);
$query = $this->db->get('ih_noticeboard');
$result = $query->result();
if($query->num_rows() < 1){
$data = "no feed ";
}else{
$data = $result;
}
the $bid in the where clause is got from this code,
$this->db->select('broadcast_id');
$this->db->where('broadcast_subscribers', $id);
$query = $this->db->get('ih_broadcastors ');
if($query->num_rows() < 1){
$data = "user not subscribed to any broadcasting";
}else{
$ress = $query->result();
foreach ($ress as $row){
$bid = $row->broadcast_id;
}`
Now the select uses only one $bid, how can I use the $bid as an array?
It's quite simple .
just use $this->db->where_in('broadcast_id', $bid);
instead if $this->db->where('broadcast_id', $bid);
https://ellislab.com/codeigniter/user-guide/database/active_record.html#select
Wouldn't it be better to have one query to get the feeds?
$this->db->select("ih_noticeboard.*");
$this->db->from("ih_noticeboard");
$this->db->join("ih_broadcastors", "ih_noticeboard.broadcast_id = ih_broadcasters.broadcast_id");
$this->db->where("ih_broadcastors.broadcast_subscribers", $id);
$result->db->get()->result();
Assuming you want to select in the table where
name='jane' and broadcast_id=2 #as an example
$bid=array(
'name'=>'jane',
'broadcast_id'=>'2'
)
$this->db->where($bid);
$this->db->from($table_name);
$query = $this->db->get()->result_array();
return $query;
Please notice that the $bid as array must be formated to suite the next database table to be queried. You can do this using foreach loop E.g
foreach ($ress as $row){
$bid = array(
'name'=>$row['name'],
'broadcast_id'=>$row['id']
);
}

As I am adding select statement in this query it returns nothing?

as i am running this query with select statement its returning nothing.
$this->db->select('emp_edu.qual_id','qual_title','pass_year','institute','percentage');
$this->db->from('emp_edu');
$this->db->join('qualification_mas', 'emp_edu.qual_id = qualification_mas.qual_id', 'INNER');
$this->db->where('emp_edu.emp_id', $empID);
$this->db->where('emp_edu.del_flag', 0);
$this->db->where('qualification_mas.del_flag', 0);
$query = $this->db->get();
return $query->result();
Change
$this->db->select('emp_edu.qual_id','qual_title','pass_year','institute','percentage');
to
$this->db->select('emp_edu.qual_id,qual_title,pass_year,institute,percentage');
as you can see in system\database\active_rec.php
line 77
if (is_string($select))
{
$select = explode(',', $select);
}
$this->db->select(
array('emp_edu.qual_id','qual_title','pass_year','institute','percentage'));
or you could enclose your parameter in array

Categories