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();
Related
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
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
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;
}
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);
}
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']
);
}