Jump to content

Working with an array and $_POST values


samoht

Recommended Posts

hello all,

 

I want to have a few simple arrays to group certain products

 

so let say I have these arrays:

$materials = array('product1','product2','product3');
$glass = array('product4','product5','product6');

 

And I wanted to check my $_POST values to see if there is any products relating to this grouping and if so, Print something like:

Materials: 'proudct2'.

glass: 'product5', 'product6'.

 

 

how do I set this up??

Link to comment
Share on other sites

Thanks aviavi,

 

but that is checking for just one value 'abc'

what I need to do is loop through the post array to see if there are any of the values in the $glass array.

 

so if my post array looked like:

 

Array ( product2=>1, product5=>1, product6=>1)

 

then my ckeck for the products within the $glass array would trip because the $glass array has product5 and product6 in it.

 

If it helps these post values are coming from checkboxes and there is a list of over 30 of those. So my post array could have a lot of these product values in it and the client wants to group the output

 

Hope that helps,

 

Link to comment
Share on other sites

well $_POST is an array, but the checkboxes are separate inputs not an array of checkboxes - this has to do with database the client is working with.

 

So the main form has these...

<input  id="product1" name="product1" type="checkbox" value="1" /><label>Asphalt Paving:</label>
		<br />
		<input  id="product2" name="product2" type="checkbox" value="1" /><label>Aggregate/Fill:</label>
		<br />
...

Link to comment
Share on other sites

OK I am off to a better start, the arrays need to match so since $_POST array looks like:

 

[product1]=>1

[product2]=>1

[product3]=>1

[product4]=>1

...

 

my $materials and $glass array needed to include the =>1

 

so now my problem is that I am trying to print the group name - but I only want to do that once if a value shows up??

 

Here is what I have:

$materials = array('product1'=>1,'product2'=>1,'product3'=>1);
	  foreach ($materials as $material) {
		if (in_array($material,$_POST))
			echo '<div class="product_divisions">Oldcastle Materials:</div>';
	  }

Link to comment
Share on other sites

OK,

 

I created a function:

$Materials = array('product1'=>1,'product2'=>1,'product3'=>1);

ocMat($Materials, $_POST);
function ocMat($Materials, $_POST){
$found = false;
foreach ($Materials as $material) {
	if (in_array($material,$_POST)){
		$found = true;
	}
}
  return $found;
}

if(ocMat($Materials, $_POST)){
	echo '<div class="product_divisions">Oldcastle Material:</div>';
}
?>

 

which would work ok except it looks for any value in $Materials array as similar to the value in $_POST array. This is a problem because the value =>1  so of course it is going to be triggered if any checkboxes are checked.

 

Is there a way to check for the "name" in the array instead of the value??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.