Get first and last row from result in codeigniter - 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

Related

How to find the duplicates in database entries in laravel

I need to find the duplicates in the database entries using laravel.But my result is incorrect please help to find it.
$query = DB::table('tbl_documents as td')
->leftjoin('tbl_document_types as tc','td.document_type_id','=','tc.document_type_id')
//->leftjoin('tbl_documents_columns as tdc','td.document_id','=','tdc.document_id')
->leftjoin('tbl_departments as tdp','td.department_id','=','tdp.department_id')
->leftjoin('tbl_stacks as ts','ts.stack_id','=','td.stack_id')
->select('td.document_name','td.document_file_name','tc.document_type_id','td.document_type_id','td.stack_id','tdp.department_id')->havingRaw('count(*)>1')
->where('td.document_type_id',$doctypeid)
->get();
You should try this
$results = DB::table('tbl_documents')->whereIn('document_file_name', function ( $query ) {
$query->select('document_file_name')->from('tbl_documents')->groupBy('document_file_name')->havingRaw('count(*) > 1');
})->get()->toArray();
return $results;

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

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 LEFT JOIN array issue

Can someone tell me how to write this properly?
function get_tech() {
$this->db->select('u.id
,u.first_name
,us.id
,us.group_id');
$this->db->from('users u');
$this->db->join('users_groups us','us.id = u.id','left');
$records = $this->db->where('us.group_id', '3');
$data=array();
foreach($records->result() as $row)
{
$data[$row->id] = $row->first_name;
}
return ($data);
}
I'm trying to populate a drop down menu using an array, but i need to only grab users that are part of users_group/group_id = 3
therefore in my very limited knowledge I'm needing:
select X from Users LEFT JOIN users_groups WHERE group_ID = 3
You need to call $this->db->get() in order to actually run your query.
function get_tech() {
$this->db->select('u.id
,u.first_name
,us.id
,us.group_id');
$this->db->from('users u');
$this->db->join('users_groups us','us.id = u.id','left');
$this->db->where('us.group_id', '3');
$records = $this->db->get();
$data = array();
foreach($records->result() as $row){
$data[$row->id] = $row->first_name;
}
return $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