vincej Posted May 17, 2012 Share Posted May 17, 2012 Hi - I am uploading Post variables off a form relating to products and customer orders. If a product has a quantity attached to it then all the POST variables relating to that product need to INSERTED into the DB. If a product has just an empty string in it's quantity variable, then that product does not get INSERTED. So I thought of 2 different approaches: A) I could filter the Quantity Post variable for a positive values then create a new array only containing products with positive quantities. B) I could loop through the product Id (ProdID) POST and using a condition pull out the products with a positive quantity for Insertion. I have been experimenting with B and have failed miserably. However I have succeeded excellently in building some code which INSERTS all the products regardless of their quantities ( see below ) - not ideal as it enters products with empty quantities. I would prefer to adapt what I have rather than throw it away. Can someone kind person give this student a hand with some advice in adapting what I have got ? MANY MANY THANKS ! <?php function insert_tel_order(){ $lastname = $_POST['lastname']; $orderid = strtoupper(substr($lastname,0,3)).(substr(time(),-4)); //Creates a unique order number $customerid = $_POST['customerid']; $pickupdate = $_POST['pickupdate']; $deliverylocationid = $_POST['deliverylocationid']; $orderdate = $_POST['orderdate']; foreach ($_POST['prodid'] as $arrkey => $prodid){ $quantity = $_POST['quantity'][$arrkey]; $prodid = $_POST['prodid'][$arrkey]; $price = $_POST['price'][$arrkey]; $ordervalue = $quantity * $price; $sql =" INSERT INTO `order` (id, orderid,orderdate,customerid, deliverylocationid,pickupdate,prodid,price, quantity, ordervalue, status) VALUES ('', '$orderid',$orderdate,$customerid,$deliverylocationid,$pickupdate,$prodid,$price,$quantity,$ordervalue,'')"; $this->db->query($sql); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262655-how-should-filter-my-post-array/ Share on other sites More sharing options...
smoseley Posted May 17, 2012 Share Posted May 17, 2012 Off topic, why can quantity have an empty string? It should be a nullable int, and contain null if you don't want it updated. Quote Link to comment https://forums.phpfreaks.com/topic/262655-how-should-filter-my-post-array/#findComment-1346233 Share on other sites More sharing options...
Drummin Posted May 17, 2012 Share Posted May 17, 2012 Just wrapping the insert in a simple IF statement should do the job. <?php function insert_tel_order(){ $lastname = $_POST['lastname']; $orderid = strtoupper(substr($lastname,0,3)).(substr(time(),-4)); //Creates a unique order number $customerid = $_POST['customerid']; $pickupdate = $_POST['pickupdate']; $deliverylocationid = $_POST['deliverylocationid']; $orderdate = $_POST['orderdate']; foreach ($_POST['prodid'] as $arrkey => $prodid){ $quantity = $_POST['quantity'][$arrkey]; $prodid = $_POST['prodid'][$arrkey]; $price = $_POST['price'][$arrkey]; $ordervalue = $quantity * $price; if ($quantity>=1){ $sql =" INSERT INTO `order` (id, orderid,orderdate,customerid, deliverylocationid,pickupdate,prodid,price, quantity, ordervalue, status) VALUES ('', '$orderid',$orderdate,$customerid,$deliverylocationid,$pickupdate,$prodid,$price,$quantity,$ordervalue,'')"; $this->db->query($sql); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262655-how-should-filter-my-post-array/#findComment-1346245 Share on other sites More sharing options...
The Letter E Posted May 17, 2012 Share Posted May 17, 2012 Just use a continue in the foreach loop: if( $quantity <= 0 || $quantity == '' || $quantity == null ){ continue; } //skips anything after this point and goes to the next iteration of the foreach Quote Link to comment https://forums.phpfreaks.com/topic/262655-how-should-filter-my-post-array/#findComment-1346246 Share on other sites More sharing options...
vincej Posted May 17, 2012 Author Share Posted May 17, 2012 Thanks Guys !! How the obvious defeats me !! I'll be so glad when I have covered every eventuality and can just slam out code ! Quote Link to comment https://forums.phpfreaks.com/topic/262655-how-should-filter-my-post-array/#findComment-1346341 Share on other sites More sharing options...
mrMarcus Posted May 17, 2012 Share Posted May 17, 2012 Just use a continue in the foreach loop: if( $quantity <= 0 || $quantity == '' || $quantity == null ){ continue; } //skips anything after this point and goes to the next iteration of the foreach Checking a variable against null using the == comparison operator is not recommended: $quantity == null will return TRUE in this case, even though $quantity is not a true NULL value. .. || is_null($quantity) will return FALSE, as will: $quantity === null Instead, use isset or empty, depending on situation, to check whether a variable has been set or contains a value, respectively. Edit: if you are worried about a certain value not being set, or a combination of a value maybe not being set AND not having a value, use a combo of isset() and empty(): <?php if (!isset($quantity) || empty($quantity)) { continue; } Or, if you're certain that variable has been set at some point in your code already, you can simply use empty(): <?php if (!empty($quantity)) { continue; } empty covers a solid range of possibilities which replaces the need to do multiple checks that are basically the same: if( $quantity <= 0 || $quantity == '' || $quantity == null ){ continue; } Is like saying, "if $quantity is <= 0 OR if $quantity contains no value/is empty OR $quantity has an unknown value" When empty() will replace all that jazz in one swoop. Quote Link to comment https://forums.phpfreaks.com/topic/262655-how-should-filter-my-post-array/#findComment-1346353 Share on other sites More sharing options...
The Letter E Posted May 18, 2012 Share Posted May 18, 2012 Just use a continue in the foreach loop: if( $quantity <= 0 || $quantity == '' || $quantity == null ){ continue; } //skips anything after this point and goes to the next iteration of the foreach Checking a variable against null using the == comparison operator is not recommended: $quantity == null will return TRUE in this case, even though $quantity is not a true NULL value. .. || is_null($quantity) will return FALSE, as will: $quantity === null Instead, use isset or empty, depending on situation, to check whether a variable has been set or contains a value, respectively. Edit: if you are worried about a certain value not being set, or a combination of a value maybe not being set AND not having a value, use a combo of isset() and empty(): <?php if (!isset($quantity) || empty($quantity)) { continue; } Or, if you're certain that variable has been set at some point in your code already, you can simply use empty(): <?php if (!empty($quantity)) { continue; } empty covers a solid range of possibilities which replaces the need to do multiple checks that are basically the same: if( $quantity <= 0 || $quantity == '' || $quantity == null ){ continue; } Is like saying, "if $quantity is <= 0 OR if $quantity contains no value/is empty OR $quantity has an unknown value" When empty() will replace all that jazz in one swoop. You're right. Quote Link to comment https://forums.phpfreaks.com/topic/262655-how-should-filter-my-post-array/#findComment-1346493 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.