Jump to content

How Should Filter My Post Array


vincej
 Share

Recommended Posts

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);


	}


}




?>

Link to comment
Share on other sites

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);
		}		
	}
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.