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

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

Related

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

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

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

How can we use where condition in array_merge result in codeigniter

Here when I use my code like this am getting error Function name must be a string in,please have a look.
public function get_date_wise_agent_report($start_date,$end_date,$agent = NULL,$abc = NULL)
{
$results1=$this->db->get('datas')->result();
$results2=$this->db->get('abc_datas')->result();
$data=array_merge($results1,$results2);
$this->db->where_in('Date(date) >=',$start_date);
$this->db->where_in('Date(date) <=', $end_date);
return $data();
}
I had used array_merge to combine two results and from that I need to get get results between the two dates hope you can help me. Thanks.
Your code should be corrected as below.
public function get_date_wise_agent_report($start_date, $end_date, $agent = NULL, $abc = NULL)
{
$this->db->where("DATE(date) BETWEEN '{$start_date}' AND '{$end_date}'")
$query = $this->db->get('datas');
$results1 = $query->result_array();
$this->db->where("DATE(date) BETWEEN '{$start_date}' AND '{$end_date}'")
$query = $this->db->get('abc_datas');
$results2 = $query->result_array();
//Here you need to use $query->result_array() instead of $query->result() because result() returns a object.
//Go to following link to learn more about result
//Here I assumed both `datas` and `abc_datas` tables are having `date` column.
$data = array_merge($results1,$results2);
return $data;
}

Print count value in codeigniter

I want to print count of some records in my project , i tried using some code but no result is giving can anyone figure out the mistake please.
controller
function cart_count()
{
$sess = $this->session->userdata('SESS_USER');
$query = $this->product_model->c_count($sess);
$data['count'] = $query->result();
$query = $this->db->get("cart");
$data['records'] = $query->result();
$this->load->view('frontend/menu',$data);
}
Model
public function c_count($sess)
{
$query =$this->db->query("SELECT COUNT(`product_id`) FROM `cart` WHERE `username`='$sess'");
return $query;
}
View
<?php foreach($count as $count){echo $count;}?>
I see your query using count and where. That is mean you just select 1 row of data like this.
username COUNT(product_id)
admin 3
The return data is just 1 row, so you can return the data using row() like this return $query->row().
Model : return your data default as a row() for 1 row of data.
public function c_count($sess)
{
$query = $this->db->query("SELECT COUNT(product_id) as count_id
FROM cart
WHERE username = '$sess'");
return $query->row();
}
Controller : Call your data here.
function cart_count()
{
$sess = $this->session->userdata('SESS_USER');
$query = $this->product_model->c_count($sess);
$data['count'] = $query->count_id; // CHANGE FROM $data['count'] = $query->result();
// If you dont mind, I change your code :
// $query = $this->db->get("cart");
// $data['records'] = $query->result();
$record = $this->db->get("cart");
$data['records'] = $record->result();
$this->load->view('frontend/menu',$data);
}
Views Here is how to call your data, ill give example using <span>.
<span>Admin total product : <?php echo $count; ?> Products</span>
There is so many ways to call returned data from database.
You can also use <?php echo $query->count_id; ?> in your views without set it into $data['count'] in your controller. You can try it now. :) Hope this help.
Note : If you want to call more than 1 data, dont use where but use a group by. I want to give you an example for that, but it's a different problem with your question. :) and if there any typos, please let me know and I will fix it.
$query =$this->db->query("SELECT COUNT(`product_id`) AS count FROM `cart` WHERE
`username`='$sess'");
change the query to
$query =$this->db->query("SELECT COUNT(`product_id`) as count FROM `cart` WHERE `username`='$sess'");
$query->result() will return array of objects
in view you will get as object you can use
<?php foreach($count as $count){echo $count->count;}?>
or
<?php echo $count[0]->count?>
The issue is with your model class where you fetch the number of row counts.
Actually, in CodeIgniter the result set fetched matches with what the columns of DB tables are.For eg. the statement
$query =$this->db->query("SELECT COUNT(`product_id`) FROM `cart` WHERE `username`='$sess'");
will return a result set something like this
Array ( [0] => stdClass Object ( [COUNT(`product_id`)] => 60 ) )
And when you try to display the result with this line <?php foreach($count as $count){echo $count;}?>
you get error because you are asking to show $count data variable of $count array which is not present.
One simple trick to solve this problem without much changes in your code is to use alias in your query.Just change your query to this
$query =$this->db->query("SELECT COUNT(`product_id`) as 'nums' FROM `products` WHERE `service_id`='$sess'");
And fetch the result in the view as <?php foreach($count as $c){echo $c->nums;}?>
However,in my opinion its better to use inbuilt function num_rows() of CI for this.
Simply use PHP count() function after getting the result
CONTROLLER
function cart_count()
{
$sess = $this->session->userdata('SESS_USER');
$query = $this->product_model->c_count($sess);
$data['count'] = count($query->result());
$query = $this->db->get("cart");
$data['records'] = $query->result();
$this->load->view('frontend/menu',$data);
}

Categories