how to count id and shows in tables - codeigniter

how count all id
my model
public function bobot()
{
$this->db->select('nama_kriteria,bobot, count(id_kriteria) as jumlah');
$this->db->from('tb_kriteria');
$this->db->group_by('id_kriteria');
$query = $this->db->get();
return $query->result();
}
I want count all rows and show in views but when I count only counts one in each row

return $this->db->count_all_results('tb_kriteria');

Related

Not getting correct SUM amount for users votes from thread and reply table

I am using codeigniter 3.1.0 and I am trying to get the total SUM of two columns from different tables
If my user_id = 1 it should return the total sum of 421 but returns 431 It is adding the votes from other users on my reply table
Question Using Active Record How can I make sure the SUM is getting the correct amount for the users id.
Model Function
public function thread_reputation($user_id) {
$this->db->select('SUM(reply.votes + thread.votes) AS reputation');
$this->db->from('thread', 'left');
$this->db->join('reply', 'reply.user_id = thread.user_id', 'left');
$this->db->where('thread.user_id', $user_id);
$this->db->where('reply.user_id', $user_id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row();
} else {
return 0;
}
}
Controller section where i echo it
$thread_reputation = $this->thread_analytics_model->thread_reputation('1');
echo $thread_reputation->reputation;
Thread Table
Reply Table
Change your query with this
$query = $this->db->query("SELECT SUM(A.votes) as reputation from
(
SELECT reply.votes FROM reply where reply.user_id = $user_id
UNION ALL
SELECT thread.votes FROM thread where thread.user_id = $user_id
)AS A");
Tested and properly working
Check Screenshot:-
Query using ACTIVE REOCRD
$this->db->select("SUM(A.votes) as reputation");
$this->db->from("(
SELECT reply.votes FROM reply where reply.user_id = $user_id
UNION ALL
SELECT thread.votes FROM thread where thread.user_id = $user_id) AS A");
$query = $this->db->get();
Another Solution using $this->db->get_compiled_select();
public function reputation($user_id) {
$this->db->select('thread.votes');
$this->db->from('thread');
$this->db->where('thread.user_id =' . $user_id);
$thread_query = $this->db->get_compiled_select();
$this->db->select('reply.votes');
$this->db->from('reply');
$this->db->where('reply.user_id ='. $user_id);
$reply_query = $this->db->get_compiled_select();
$query = $this->db->query("SELECT SUM(total.votes) as reputation from ($reply_query UNION ALL $thread_query) as total");
if ($query->num_rows() > 0) {
return $query->row();
} else {
return 0;
}
}
When you will use left join, it will return matched column & all other unmatched column from left table. Use inner join & check your result.

how to use activerecord count_all_result()? with specific column?

I want to get count of specific column of table using codeigniter active record, something like this:
$this->db->count_all_results('t.column1');
so how can i change this db_active_record function? for any help thanks.
To count a specific column, You would do this;
$query = $this->db->get_where('table', array('column' => $column);
return $query->num_rows(); // This is what you're after...
create the query in your model, load it in your controller and then just count the array items in the view.
for example:
Model:
function getAll() {
$this->db->select('*');
$this->db->order_by('user_ad', 'asc');
$query = $this->db->get('users');
if ($query->num_rows() > 0) {
return $query->result();
}
}
controller:
$this->load->model('Users');
$data['allusers'] = $this->Users->getAll();
this->load->view('users', $data);
and in your view you can use
count($allusers);

Get first and last row from result in codeigniter

public function get_task_threads_first_last($task_id)
{
$this->db->select("t.nt_id, t.nt_responsed_by, t.nt_detail 'task_detail', t.nt_response_date, u.ui_full_name 'user_name', u.ui_designation 'user_designation', u.ui_phone 'extension', nt_response_date 'response_date'");
$this->db->from('tm_task_detail t');
$this->db->order_by("nt_response_date");
$this->db->join('ac_user_info u', 'u.ui_id = t.nt_responsed_by');
$this->db->where('t.nt_task_id', $task_id);
$query = $this->db->get();
return $result = $query->result();
}
I need to fetch the first and last row from above query. How is this possible ?
Also i am thinking that it is useless to select all rows than get the first and last rows out from it. I'm new in programming need advice thanks.
Thanks

Codeigniter, how to pass COUNT result too my view

i have problem with passing data from model to my view. In Database i have Project with a few tasks i need to COUNT it. My problem is: How to pass the results to the view with foreach.
(echo $this->db->count_all_results(); in MODEL show good results)
Project 1: 5 tasks
Project 2: 10 tasks
Project 3: 1 task
MODEL:
function wyswietl($arrayid){
$query = $this->db->query("SELECT COUNT( id_zadania ) AS zlicz
FROM projekty_zadania
WHERE id =$arrayid");
return $query->result();
CONTROLLER
function WszystkieProjekty(){
$data['moje'] = $this->Todo_model->wyswietlWszystkieProjekty();
$arrayid = array();
foreach( $data['moje'] as $row ){
$row->id;
$aaa = $row->id;
$arrayid[] = $this->Todo_model->wyswietl($aaa);
}
$data['tabid'] = $arrayid;
$this->load->view('Projekty.html', $data);
}
MODEL:
function wyswietlWszystkieProjekty(){
$query = $this->db->query("SELECT * FROM todo");
return $query->result();
}
You're passing back to your controller a result set, so all you need to do is iterate over the result set, generating actual rows. It's not too hard, and you've got the right idea.
You have:
function wyswietl($arrayid){
$query = $this->db->query("SELECT COUNT( id_zadania ) AS zlicz
FROM projekty_zadania
WHERE id =$arrayid");
return $query->result();
As your model function that is supposed to return the count, right? Easiest way to do this is right from the model to keep your controller from having to do the legwork. When you run the count function in mysql, you'll only get back one row in your case, so using num_rows() isn't going to help because this query will always generate 1 row.
function wyswietl($arrayid){
$query = $this->db->query("SELECT COUNT(id_zadania) AS zlicz
FROM projekty_zadania
WHERE id =$arrayid");
$rs = $query->result();
return $rs[0]['COUNT(id_zaedania)'];
}
In your query, you have counted the data, so i think you can get the total data in the model from $query->rows()->zlics. And in the controller, just pass it to the view like usual
$data['moje'] = $this->Todo_model->wyswietlWszystkieProjekty(); $this->load->view('Projekty.html', $data);

Codeigniter select_max and return other row fields

I'm trying to select the max bid amount on a particular item and return more information from the row that the max bid is found in.
At the moment I have
$item_id = $this->input->post('item_id');
$this->db->from('bids');
$this->db->where('item_id', $item_id);
$this->db->where('outbid_alert', '1');
$this->db->select_max('bid_amount');
$query = $this->db->get();
return $query->result();
This returns the max bid for the item and that's as far as I have gotten. What's the best way to get the rest of the fields from that row? Run another query or use a subquery?
Thanks!
If you want to return the fields from the row with the highest bid_amount, just ORDER BY bid_amount and select only the first row.
$item_id = $this->input->post('item_id');
$this->db->from('bids');
$this->db->where('item_id', $item_id);
$this->db->where('outbid_alert', '1');
$this->db->select('*');
$this->db->order_by('bid_amount', 'DESC');
$this->db->limit(1);
$query = $this->db->get();
return $query->result();

Resources