I am working on e-commerce site in php. I've multidimensional php array like this :-
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[1] => Array
(
[0] => 5
[1] => 6
[2] => 7
)
)
I am working on Advance search option. I've two table first one is product and another one is productattributes. I want to implode zero index array value by OR operator, One Index array value by OR
Condition and then final array with zero index AND first index like this :-
select * from tbl_product where id IN(select product_id from tbl_vehicleproductequipments where (equipmentvalue_id = 1 OR equipmentvalue_id = 2 OR equipmentvalue_id = 3 OR equipmentvalue_id = 4) AND (equipmentvalue_id = 5 OR equipmentvalue_id = 6 OR equipmentvalue_id = 7)
I've tried this code :-
$eqpcond = "";
if(!empty($_REQUEST["equipmentarr"])){
foreach($_REQUEST["equipmentarr"] as $y => $equipval){
$eqpcond = "select * from tbl_product where id IN (select product_id from tbl_vehicleproductequipments where ";
foreach($equipval as $s => $vl){
$equipcarr[] = " OR equipmentvalue_id = $vl";
}
}
if(!empty($equipcarr)){
$eqpcond = implode(" AND ",$equipcarr).")";
}
}
and i got the query like this which is not correct.
select * from tbl_product where id IN(select product_id from tbl_vehicleproductequipments where equipmentvalue_id = 1 AND OR equipmentvalue_id = 2 AND OR equipmentvalue_id = 3 AND OR equipmentvalue_id = 4 AND OR equipmentvalue_id = 5 AND OR equipmentvalue_id = 6 AND OR equipmentvalue_id = 7)
Please help me as I got stuck in this situation and I don't know how to do this. Any Help will be appreciated.
Thanks in Advance
Try this..I thinks this will produce the desired query. If not, please post the generate query and will make necessary changes.
$eqpcond = "";
if (!empty($_REQUEST["equipmentarr"])) {
foreach ($_REQUEST["equipmentarr"] as $y => $equipval) {
$equipcstr = "";
$equipcarr = array();
$eqpcond = "select * from tbl_product where id IN (select product_id from tbl_vehicleproductequipments where ";
foreach ($equipval as $s => $vl) {
$equipcstr .= " OR equipmentvalue_id = $vl";
}
$equipcstr = trim($equipcstr, 'OR');
$equipcarr[] = $equipcstr;
}
if (!empty($equipcarr)) {
$eqpcond = implode(" AND ", $equipcarr) . ")";
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments