samoht Posted June 23, 2009 Share Posted June 23, 2009 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?? Quote Link to comment https://forums.phpfreaks.com/topic/163308-working-with-an-array-and-_post-values/ Share on other sites More sharing options...
EchoFool Posted June 23, 2009 Share Posted June 23, 2009 You can use an in array function to check if the what your looking for is in the array. Quote Link to comment https://forums.phpfreaks.com/topic/163308-working-with-an-array-and-_post-values/#findComment-861629 Share on other sites More sharing options...
Ken2k7 Posted June 23, 2009 Share Posted June 23, 2009 Or use a database. Quote Link to comment https://forums.phpfreaks.com/topic/163308-working-with-an-array-and-_post-values/#findComment-861632 Share on other sites More sharing options...
samoht Posted June 23, 2009 Author Share Posted June 23, 2009 thanks EchoFool I'm not exactly sure what to do with the $_POST array?? foreach ($materials as $material) { if (is_array($_POST) && in_array($material,$_POST)) Quote Link to comment https://forums.phpfreaks.com/topic/163308-working-with-an-array-and-_post-values/#findComment-861634 Share on other sites More sharing options...
samoht Posted June 23, 2009 Author Share Posted June 23, 2009 Ken, I cant use the database because the form is not actually submitted yet but is just in confirmation stage. Can any one help me with the in_array function because the above does not work. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/163308-working-with-an-array-and-_post-values/#findComment-861820 Share on other sites More sharing options...
AviNahum Posted June 23, 2009 Share Posted June 23, 2009 if ( in_array($_POST['abc'] ,$glass) ) { // Something } you even dont have to use a loop... Quote Link to comment https://forums.phpfreaks.com/topic/163308-working-with-an-array-and-_post-values/#findComment-861837 Share on other sites More sharing options...
samoht Posted June 23, 2009 Author Share Posted June 23, 2009 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, Quote Link to comment https://forums.phpfreaks.com/topic/163308-working-with-an-array-and-_post-values/#findComment-861845 Share on other sites More sharing options...
AviNahum Posted June 23, 2009 Share Posted June 23, 2009 correct me if i mistaken.... $_POST get some array... and you want to check if some values of this array are exist in $glass array? Quote Link to comment https://forums.phpfreaks.com/topic/163308-working-with-an-array-and-_post-values/#findComment-861860 Share on other sites More sharing options...
samoht Posted June 23, 2009 Author Share Posted June 23, 2009 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 /> ... Quote Link to comment https://forums.phpfreaks.com/topic/163308-working-with-an-array-and-_post-values/#findComment-861892 Share on other sites More sharing options...
samoht Posted June 23, 2009 Author Share Posted June 23, 2009 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/163308-working-with-an-array-and-_post-values/#findComment-861911 Share on other sites More sharing options...
samoht Posted June 23, 2009 Author Share Posted June 23, 2009 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?? Quote Link to comment https://forums.phpfreaks.com/topic/163308-working-with-an-array-and-_post-values/#findComment-861985 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.