1

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

6
  • 1
    don't use sub query like this IN(SELECT....), you can get them in an array and make a join variable then use it. Commented May 12, 2016 at 5:17
  • Can u please write code of it. I know only this way. Commented May 12, 2016 at 5:20
  • This part doesn't make sense: ...(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). equipmentvalue_id can't be one of those two groups of values at the same time. Commented May 12, 2016 at 5:22
  • make a different query for subquery and fetch them, collect them into an array then $in = join(", ", $fetch_arr); Commented May 12, 2016 at 5:23
  • There are multiple equipments for a particular product so equipmentvalue_id can be one of these values at same time Commented May 12, 2016 at 5:24

2 Answers 2

1

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) . ")";
            }
        }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for Your Kind Reply but using this code i am able to join the first array index value only zero index value is not coming. The output of the above code is :- select * from tbl_product where id IN(select product_id from tbl_vehicleproductequipments where equipmentvalue_id = 5 OR equipmentvalue_id = 6 OR equipmentvalue_id = 8)
Ohh Sorry...my mistake.. just place the $equipcarr = array(); immediately after the if (!empty($_REQUEST["equipmentarr"])) { and see what happens
Its working fine. Thanks very much for your kind reply. I am accepting this as answer. Thanks very much. I really appreciate it.
Thanks..glad to help
0

Note: I don't think you have a valid query, where equipmentvalue_id IN (1,2,3,4) AND equipmentvalue_id IN (5,6,7) How it is possible?

Check it out: Online test

This is your desire solution, using some implode function..

$arr = array(
        array(1, 2, 3, 4),
        array(5, 6, 7)
    );

Subquery using OR

$sql = 'select product_id from tbl_vehicleproductequipments where (equipmentvalue_id = '.implode(" OR equipmentvalue_id = ", $arr[0]).') AND (equipmentvalue_id = '.implode(" OR equipmentvalue_id = ", $arr[1]).")";

Subquery using IN

$sql = 'select product_id from tbl_vehicleproductequipments where equipmentvalue_id IN ('.implode(",", $arr[0]).') AND equipmentvalue_id IN ('.implode(",", $arr[1]).')';

Then finally use the $sql.

$main_sql = select * from tbl_product where id IN($sql);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.