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);
}
Related
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();
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
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
I have the folowing query that shows my products.
SELECT DISTINCT productsmap.id, attributes_product.waarde,
attributes_product.att_id, productsmap.name, productsmap.category
AS categoryId, brand.naam AS brand, productsmap.sku, categorie.name AS
category, productsmap.price, productsmap.shops
FROM productsmap
INNER JOIN categorie ON productsmap.category = categorie.id
INNER JOIN brand ON productsmap.brand = brand.id
INNER JOIN attributes_product ON productsmap.id = attributes_product.pmapid
WHERE productsmap.category
IN ( 2, 3, 4, 5, 6, 7, 8 )
AND productsmap.shops >0
AND (
productsmap.price >= 3.94
AND productsmap.price <= 204.99
)
AND productsmap.online =1
LIMIT 0 , 30
It gives the folowing results I cropped a bit:
(id) (waarde) (att_id) (name) (categoryId) (brand) (sku)
2 109 1 Name 4 Merk 70000
2 2013 2 Name etc etc etc
2 123 3 Name etc etc etc
3 103 1 Name2 etc etc
3 2012 2 Name2
3 123 3 Name2
4 110 1 3name
4 2013 2 3name
4 102 3 3name
Whit multiple times the same id and name only diffrent att_id and waarde. But i need to add to my query (attributes_product.att_id = 1 and waarde = 109) and (attributes_product.att_id = 2 and waarde = 2013)
But this is not possible because it are diffrent results. How can I solve this issue? Or on what can i search to solve this issue?
It attributes.product are att_id and waarde
i want to insert the values of two arrays into a table in a single query . Is it possible to do something like that.
table format
id | product_id | category_id | blog_id | updated_by | created_date
category _id
Array
(
[0] => 4
[1] => 15
[2] => 18
)
Product_id
Array
(
[0] => 2260
[1] => 1401
)
Blog id = mysql_insert_id();
result
id | product_id | category_id | blog_id | updated_by
1 2260 4 15 xyz
2 1401 15 15 xyz
3 null 18 15 xyz
Any suggestions for me also to improve the better insert query for this.
INSERT INTO `Table_Name` (`product_id`, `category_id`, `blog_id`, `updated_by`) VALUES (2260, 4, 15, 'xyz'), (1401, 15, 15, 'xyz'), (, 18, 15, 'xyz');
I assumed that id column is an auto incremented one.
You can insert multiple rows at one go in the same query that you are using now. Just add a comma, and put in more values:
Insert into myTable values ('field1-var1','field2-var2'), ('field1-var2','field2-var2') ..
$combain = array_merge($cat , $proid);
for($i = 0; $i <count($combain); $i++ )
{
if(isset($proid[$i])) {
$product_id = $proid[$i];
}
else{
$product_id = "''";
}
if(isset($cat[$i])){
$category_id = $cat[$i];
}
else{
$category_id = "''";
}
if(!empty($cat[$i]) || !empty($proid[$i])) {
#$values[] ="('',". $blogid.",".$category_id.",".$product_id.","."'".$updated_by."'".",now())";
}
}
$query = $this->db->query("insert into blog_details (id , blog_id , cat_id, pro_id, updated_by , created_date) values" . implode(',', $values));