php - Iterating through two POST arrays of different length to find customers who bought x,y,z but didn't buy x,y,x,z -
all answers out there deal iterating 2 or more arrays of same length, or same keys, not here. tried of ideas out there no avail.
i have 2 multiple selects submitted in same post form - products bought , products not bought. contain same products, multi selects have produce same items in array (think of how many combinations there are.) first foreach
works fine , pulls mysql customers had of selected "bought" items. second nested foreach
doesn't iterate - takes first value "hasn't brought" array.
html part:
find customers have bought: <form method="post" action=""> <select name="bought[]" size="5" multiple="multiple"> <option value="rd"> brown t shirt </option> <option value="rdx"> red t shirt </option> <option value="hd"> white t shirt </option> <option value="cb"> black t shirt</option> <option value="da"> green t shirt </option> </select> haven't bought: <select name="notbought[]" size="5" multiple="multiple"> <option value="rd"> brown t shirt </option> <option value="rdx"> red t shirt </option> <option value="hd"> white t shirt </option> <option value="cb"> black t shirt</option> <option value="da"> green t shirt </option> </select> <div class="buttons> <button class="large" type="submit">get csv!</button> </div> </form>
then php:
<?php foreach($_post['bought'] $bought) { foreach($_post['notbought'] $notbought){ $selectsubs = mysqli_query("select first_name, last_name, email, territory customers customer_type = 'active' , products_bought '%$bought%' , products_bought not '%$notbought%' "); while($row = mysqli_fetch_assoc($selectsubs)) { $csvwriter->addrow(); $csvwriter->addcol($row['email']); $csvwriter->addcol($row['first_name']); $csvwriter->addcol($row['last_name']); $csvwriter->addcol($row['territory']); } } } $csvwriter->writefile("customers.csv"); ?>
making assumption products_bought field comma separated list of products tempted in single piece of sql, (not tested):-
foreach($_post['bought'] $key=>$value) $_post['bought'][$key] = mysqli_real_escape_string($con, $_post['bought'][$key]); foreach($_post['notbought'] $key=>$value) $_post['notbought'][$key] = mysqli_real_escape_string($con, $_post['notbought'][$key]); $bought = 'find_in_set("'.implode('", replace(products_bought, "-", ",")) , find_in_set("', $_post['bought']).'", replace(products_bought, "-", ","))'; $notbought = '!find_in_set("'.implode('", replace(products_bought, "-", ",")) , !find_in_set("', $_post['notbought']).'", replace(products_bought, "-", ","))'; $selectsubs = mysqli_query("select first_name, last_name, email, territory customers customer_type = 'active' , $bought , $notbought");
this should build piece of sql following:-
select first_name, last_name, email, territory customers customer_type = 'active' , find_in_set("rd", replace(products_bought, '-', ',')) , find_in_set("rdx", replace(products_bought, '-', ',')) , !find_in_set("hd", replace(products_bought, '-', ',')) , !find_in_set("cb", replace(products_bought, '-', ','))
however better split items bought off table, 1 row per customer / item rather using delimited list.
Comments
Post a Comment