multiple join in codeigniter - php

Here am having three tables namely tbl_users,tbl_account_details,tbl_attendance.table tbl_users looks like this
user_id username role_id activated banned
1 admin 1 1 0
36 siraj 3 1 0
37 faizal 3 1 0
38 nesru 3 1 0
40 jaseer 3 1 0
42 maltu 3 1 0
43 shahul 3 1 0
table tbl_account_details looks like this
account_details_id user_id fullname designations_id
1 1 Administrator 1
33 36 siraj 2
34 37 faizal 3
35 38 nesru 3
37 40 jaseer 4
39 42 maltu 4
40 43 shahul 2
my tbl_attendance table looks like this
attendance_id user_id date_in attendance_status status 0=absent 1=present 3 = onleave 4 = onoff
1 1 2017-02-05 1
2 36 2017-02-11 4
3 36 2017-02-11 4
4 36 2017-02-11 3
5 1 2017-02-02 1
6 36 2017-02-01 1
i want to get all users where users is not in onleave and onoff on the current day in the tbl_attendance.the attendance status for leave is 3 and for off is 4.
in order to get i wrote a query which looks like this
public function get_manager_cordinator_users($date)
{
$this->db->select('tbl_account_details.*', FALSE);
$this->db->select('tbl_users.*');
$this->db->join('tbl_account_details', 'tbl_users.user_id = tbl_account_details.user_id', 'left');
$this->db->where(array('designations_id' => 2));
$this->db->or_where(array('designations_id' => 3));
$this->db->where('tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in='.$date.' && attendance_status=3 OR attendance_status=4 )');
$query_result = $this->db->get('tbl_users');
$result = $query_result->result();
return $result;
}
my controller looks like this
$date=date('Y-m-d');
$data['assign_user'] = $this->Project_model->get_manager_cordinator_users($date);
please help me to solve...

I've created query which will return the data of users with designation_id 2 and 3, also which don't have attendance_status 3 and 4
Your controller code is fine -
$date = date('Y-m-d');
$data['assign_user'] = $this->Project_model->get_manager_cordinator_users($date);
Just change your model code with below one -
public function get_manager_cordinator_users($date)
{
$this->db->select( 'tbl_users.user_id, tbl_users.username,tbl_account_details.designations_id, tbl_attendance.date_in, tbl_attendance.attendance_status' );
$this->db->from( 'tbl_users' );
$this->db->join( 'tbl_account_details', 'tbl_account_details.user_id = tbl_users.user_id' );
$this->db->join( 'tbl_attendance', 'tbl_attendance.user_id = tbl_users.user_id' );
$this->db->where( 'tbl_attendance.date_in', $date );
$this->db->where('tbl_users.banned', 0);
$this->db->where('tbl_users.activated', 1);
$this->db->where_in( 'tbl_account_details.designations_id', array( '2', '3' ) );
$this->db->where_not_in( 'tbl_attendance.attendance_status', array( '3', '4' ) );
return $this->db->get()->result();
}
It will provide you required user data.

now i got it
public function get_manager_cordinator_users()
{
$date=date('Y-m-d');
$this->db->select('tbl_users.*');
$this->db->join('tbl_account_details', 'tbl_users.user_id = tbl_account_details.user_id', 'left');
$this->db->where('tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in= "'.$date.'" AND tbl_attendance.attendance_status IN (3,4) ) ');
$this->db->where_in( 'tbl_account_details.designations_id', array( '2','3' ) );
$this->db->where('tbl_users.banned', 0);
$this->db->where('tbl_users.activated', 1);
$query_result = $this->db->get('tbl_users');
$result = $query_result->result();
return $result;
}

use this
$this->db->select('tbl_account_details.*', FALSE);
$this->db->select('tbl_users.*');
$this->db->join('tbl_users', 'tbl_users.user_id = tbl_account_details.user_id', 'left');
$this->db->where('(designations_id=2 or designations_id=3 )');
$this->db->where('tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in='.$date.' && attendance_status=3 OR attendance_status=4 )');
$query_result = $this->db->get('tbl_users');
$result = $query_result->result();
return $result;

$this->db->select('*)->from('tbl_users')->where('tbl_attendance.attendance_status <= 2')->where('tbl_attendance.data_in =',date('Y-m-d'))->join('tbl_attendance','tbl_attendance.user_id = 'tbl_users.user_id ','left')->get()->result();

Related

Problem with foreach loop with group by one column but have multiple values another column in php

db_table => commitment
ref_no
comm_date
1
2022-10-05
2
2022-10-05
3
2022-10-06
4
2022-10-07
5
2022-10-07
6
2022-10-08
db_table => collection
ref_no
amount
trnx_date
1
500
2022-10-05
2
100
2022-10-05
1
700
2022-10-06
3
400
2022-10-07
3
600
2022-10-08
5
800
2022-10-08
1
700
2022-10-08
I want to achieve something like this in datatable:
ref_no
comm_date
collection summary
1
2022-10-05
500 (2022-10-05) + 700 (2022-10-06) + 700 (2022-10-08) = 1900
2
2022-10-05
100 (2022-10-05) = 100
3
2022-10-06
400 (2022-10-07) + 600 (2022-10-08) = 1000
4
2022-10-07
0
5
2022-10-07
800 (2022-10-08) = 800
6
2022-10-08
0
How can I achieve this with php and mysql and show it to datatable. Thanks in advance!
What I have tried in sql in codeigniter model:
SELECT c.*, t.*
FROM commitment c
LEFT JOIN collection t ON c.ref_no = t.ref_no
WHERE c.ref_no IN (SELECT ref_no FROM collection)
GROUP BY c.ref_no
In controller:
public function collection_statement_list() {
// Datatables Variables
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$fetch = $this->Project_model->get_collection_statement();
$data = array();
foreach($fetch->result() as $r) {
$ref_no = $r->ref_no;
$comm_date = $this->Project_model->set_date_format($r->comm_date);
$coll_date = $this->Project_model->set_date_format($r->trnx_date);
$coll_summary = $r->amount.'<span class="text-primary"><small>('.$coll_date.')</small></span>';
$data[] = array(
$ref_no,
$comm_date,
$coll_summary,
);
}
$output = array(
"draw" => $draw,
"recordsTotal" => $fetch->num_rows(),
"recordsFiltered" => $fetch->num_rows(),
"data" => $data
);
echo json_encode($output);
exit();
}
And the output in datatable is:
| ref_no | comm_date | collection summary |
| ------ | ---------- | ------------------ |
| 1 | 2022-10-05 | 500 (2022-10-05) |
| 2 | 2022-10-05 | 100 (2022-10-05) |
| 3 | 2022-10-06 | 400 (2022-10-07) |
| 4 | 2022-10-07 | 0 |
| 5 | 2022-10-07 | 800 (2022-10-08) |
| 6 | 2022-10-08 | 0 |
And so in SQL only this query corresponds to your solution with group_concat...? I'm trying to answer to help those who would be looking for the solution in SQL only.
select COM.ref_no,
if(COL.ref_no is not null,group_concat(COL.trnx_date,' (',COL.amount,')' separator '+'),'') as 'collection summary details',
if(COL.ref_no is not null,sum(COL.amount),0) as 'collection summary'
from commitment as COM
left join collection as COL on COM.ref_no=COL.ref_no
group by COM.ref_no
I think such way. Imagine you have a table about amounts. It is will be solution by sql:
Select ref_no, comm_date, sub(summary) as collect_summary from amount;
However you may use alternative way to be group with php such:
<?php
$amount = [
['ref_no'=> 1, 'amount'=>500 , 'date'=>'2022-10-05'],
['ref_no'=> 2, 'amount'=>100 , 'date'=>'2022-10-05'],
['ref_no'=> 1, 'amount'=>700 , 'date'=>'2022-10-05'],
['ref_no'=> 3, 'amount'=>400 , 'date'=>'2022-10-05'],
['ref_no'=> 3, 'amount'=>600 , 'date'=>'2022-10-05'],
['ref_no'=> 5, 'amount'=>800 , 'date'=>'2022-10-05'],
['ref_no'=> 1, 'amount'=>700 , 'date'=>'2022-10-05'],
];
$result = [];
foreach($amount as $item) {
$ref = $item['ref_no'];
if(isset($result[$ref])) {
$result[$ref]['collect_amount'] = $result[$ref]['collect_amount'] + $item['amount'];
}else{
$result[$ref] = [
'ref_no' => $ref,
'date' => $item['date'],
'collect_amount' =>$item['amount']
];
}
}
echo '<pre>';
print_r($result);

nested where condition in codeigniter

from this data how can i fetch all users where user_id in tbl_attendance not having current date with application_status 3 and 4
attendance_id user_id date_in attendance_status status 0=absent 1=present 3 = onleave 4 = onoff
1 1 2017-02-05 1
2 36 2017-02-11 4
3 36 2017-02-11 4
4 36 2017-02-11 3
5 1 2017-02-02 1
6 36 2017-02-01 1
my code is like this
$date=date('Y-m-d');
$this->db->where('tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in= "'.$date.'" )');
$this->db->where_in( 'tbl_attendance.attendance_status', array( '3', '4' ) );
want to combine those two lines and produce the output like if current date is present in 3 or 4 please help me
You can try like this.To combine two conditions
$this->db->where("tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in='$date' AND tbl_attendance.attendance_status IN(3,4))");
seeing you're using query builder...
$this->db->group_start()
$this->db->where_in( 'tbl_attendance.attendance_status', array( '3', '4' ) );
$this->db->where_not_in('tbl_users.user_id', 'SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in = '. $date, false)
$this->db->group_end()
should work

Codeigniter sorting data

I have a ptoblem with sorting $_post data in codeigniter.
Controller:
public function filter(){
...
$this->model->method($this->request->post);
...
}
Model:
$keys = $_POST-data
public function($keys) {
$this->db->select('*');
$this->db->from('table1 t1');
$this->db->join('table2 t2', 't2.id = t1.id', 'left');
if(!empty($keys['value1']){
$this->db->where('t1.column', $keys['value1']);
}
if(!empty($keys['value2']){
$this->db->where('t1.column', $keys['value2']);
}
if($keys['value3'] || $keys['value4'] || $keys['value5']){
$array = array($keys['value3'], $keys['value4'], $keys['value5']);
$this->db->where_in('t2.column', $array);
}
$query = $this->db->get()->result();
return $query;
}
table1
id value1 valu2
1 23 24
2 12 15
table2
t1_id value (this contain value of value3, value4, value5)
2 7
2 19
2 13
2 3
1 7
1 13
1 3
It works, but if set a value3..value5, then selects all values, not a priority table1 value, I need to to do value1 and value2 it has been a priority before value3..value5
Sry for my bad english, tnx!
For example:
products_table
product_id value1 value2
1 13 9
2 13 9
3 14 10
product_to_atributes
product_id atribute_id
1 2
1 3
1 4
2 3
2 4
product_atributes
atribute_id atribute_name
2 atr_name
3 atr_name2
4 atr_name3
I need select list of product, where value1 = '13' and value2 = '9' and product_to_atributes.atribute_id IN(2,4)
you can use this:
$this->db->or_where_in();
your code should be:
if($keys['value3'] || $keys['value4'] || $keys['value5']){
$array = array($keys['value3'], $keys['value4'], $keys['value5']);
$this->db->or_where_in('t2.column', $array);
}

SORT a specific value in mysql_fetch_array

I have data set with full of data...
Id | loc |SubLoc |beds
1 15 5803 1
2 15 5803 1
3 16 2813 1
4 16 2813 2
5 16 2830 1
6 20 2020 4
7 19 2513 3
I have this php/mysql code to traverse it
select id,loc,SubLoc,beds from table where id != 144 AND price > 140000 AND price < 210000 AND category_id=1
while($rw = mysql_fetch_array($rs2)){}
Now i want to sort $rw in this manner
I have three values
loc=16,SubLoc=2813,beds=1
I want to show same result but show all results in this manner
1- having loc==16 on top then
2- having SubLoc = 2813 then
3- having beds = 1
and then rest of results
How this can be done in php?
UPDATED
I want to get these Ids
3 as it matches 3 parameters
4 as it matches 2 parameters
5 as it matches 2 parametrs
1 as it matches 1 parameter
and so on
Try that:
select id,loc,SubLoc,beds from table1
where loc = 16 or subloc = 2813 or beds =1
order by
case when loc = 16 then 0
when subloc = 2813 then 1
when beds = 1 then 2 end asc
DEMO
to get other results also without matching then use this:
select id,loc,SubLoc,beds from table1
order by
case when loc = 16 then 0
when subloc = 2813 then 1
when beds = 1 then 2
else 3 end asc
DEMO
EDIT: to include all cases use this:
select id,loc,SubLoc,beds from table1
order by
case when loc = 16 and subloc=2013 and beds = 1 then 0
when loc = 16 and subloc = 2813 then 1
when loc = 16 and beds = 1 then 2
when subloc = 2813 and beds = 1 then 3
when loc = 16 then 4
when subloc = 2813 then 5
when beds = 1 then 6
else 7 end asc
Try with order
ORDER BY SubLoc=16 DESC,loc, beds

Top 5 Rankings Issue

So I'm wondering if this is even the best way to manage my data for this table because with my query I'm not getting the correct results I want.
I am wanting it to grab the top5 rankings from the last date entry in the table. A problem is if there is a 0 for the character_id. When I do my php to see if there isn't a value then it should echo TBD but its still not showing it in the array to be 0.
Table: top5
id ranking # character_id status_id date_created
1 1 1 1 2011-10-17 17:18:54
2 2 2 1 2011-10-17 17:18:54
3 3 3 1 2011-10-17 17:18:54
4 4 4 1 2011-10-17 17:18:54
5 5 5 1 2011-10-17 17:18:54
6 1 6 1 2011-10-24 12:18:54
7 2 7 1 2011-10-24 12:18:54
8 3 8 1 2011-10-24 12:18:54
9 4 9 1 2011-10-24 12:18:54
10 5 0 1 2011-10-24 12:18:54
function getTop5()
{
$this->db->select('characters.character_name, top5.character_id');
$this->db->from('top5');
$this->db->join('characters', 'characters.id = top5.character_id');
$this->db->where('top5.status_id', '1');
$this->db->order_by('top5.date_created','desc');
$this->db->limit(5);
$query = $this->db->get();
return $query->result_array();
}
Array ( [0] =>
Array ( [character_name] => \"Mr. Magnificent\" Matt Sharp
[character_id] => 9 )
[1] =>
Array ( [character_name] => \"The Unforgettable\" Jimmy Watkins
[character_id] => 8 )
[2] =>
Array ( [character_name] => Romie Rains
[character_id] => 7 )
[3] =>
Array ( [character_name] => Monica Dawson
[character_id] => 6 )
[4] =>
Array ( [character_name] => \"The Outlaw\" Mike Mayhem
[character_id] => 5 ) )
EDIT: Anyone else want to give it a try?
So lost and can't get the desired results still
You'd want to order by top5.date_created in descending order. If you really only want the last day, you'd also want a WHERE condition for that.
Not sure what db class is it, but just append
AND date(date_created) = CURDATE();
to the query
or append DESC to ORDER clause
So something like
$this->db->select('characters.character_name, top5.character_id');
$this->db->from('top5');
$this->db->join('characters', 'characters.id = top5.character_id');
$this->db->where('top5.status_id', '1');
// WHERE date(top5.date_created) = CURDATE()
$this->db->order_by('top5.date_created');
$this->db->limit(5);
$query = $this->db->get();
function getTop5()
{
$this->db->select('characters.character_name, top5.character_id');
$this->db->from('top5');
$this->db->join('characters', 'characters.id = top5.character_id');
$this->db->join( '( SELECT DATE(MAX(date_created)) AS lastdate
FROM top5
WHERE status_id = 1
) AS tm'
, 'top5.created_at >= tm.lastdate
AND top5.created_at < tm.lastdate + INTERVAL 1 DAY');
$this->db->where('top5.status_id', '1');
$this->db->order_by('top5.ranking','asc');
$this->db->limit(5);
$query = $this->db->get();
return $query->result_array();
}

Categories