project3 Posted November 30, 2007 Share Posted November 30, 2007 What I have is a select box with multiple selections turned on. If a user doesn't select anything how can I find out if the data is empty so I can stop the error caused by the foreach statement. like if(array is empty){ do this} else { foreach statement} here is my code now $valxx = $_POST['Groups']; foreach ($valxx as $xx){ $klg .= "$xx,"; } Quote Link to comment https://forums.phpfreaks.com/topic/79505-solved-how-to-make-sure-array-has-data/ Share on other sites More sharing options...
pocobueno1388 Posted November 30, 2007 Share Posted November 30, 2007 <?php if (empty($valxx)){ //it's empty } else { //not empty } ?> Quote Link to comment https://forums.phpfreaks.com/topic/79505-solved-how-to-make-sure-array-has-data/#findComment-402632 Share on other sites More sharing options...
wsantos Posted November 30, 2007 Share Posted November 30, 2007 http://www.php.net/isset http://www.php.net/empty Quote Link to comment https://forums.phpfreaks.com/topic/79505-solved-how-to-make-sure-array-has-data/#findComment-402633 Share on other sites More sharing options...
project3 Posted November 30, 2007 Author Share Posted November 30, 2007 <?php if (empty($valxx)){ //it's empty } else { //not empty } ?> I still get this error when using that Invalid argument supplied for foreach() if (empty($valxx)){ } else { foreach ($valxx as $xx){ $klg .= "$xx,"; } } Quote Link to comment https://forums.phpfreaks.com/topic/79505-solved-how-to-make-sure-array-has-data/#findComment-402636 Share on other sites More sharing options...
The Little Guy Posted November 30, 2007 Share Posted November 30, 2007 OR <?php if(count($array)< 1){ // Nothing in array }else{ // Array has 1+ values } ?> Quote Link to comment https://forums.phpfreaks.com/topic/79505-solved-how-to-make-sure-array-has-data/#findComment-402637 Share on other sites More sharing options...
pocobueno1388 Posted November 30, 2007 Share Posted November 30, 2007 Try this... <?php if (isset($_POST['Groups'])){ foreach ($_POST['Groups'] as $xx){ $klg .= "$xx,"; } } ?> If that doesn't work, post your form HTML...maybe your not setting it up to be an array. Quote Link to comment https://forums.phpfreaks.com/topic/79505-solved-how-to-make-sure-array-has-data/#findComment-402638 Share on other sites More sharing options...
darkfreaks Posted November 30, 2007 Share Posted November 30, 2007 <?php if (!isset($valxx)) {echo "error:please insert a value";} else { //do stuff } ?> Quote Link to comment https://forums.phpfreaks.com/topic/79505-solved-how-to-make-sure-array-has-data/#findComment-402639 Share on other sites More sharing options...
The Little Guy Posted November 30, 2007 Share Posted November 30, 2007 I think isset might be a poor choice, what if $valxx is a string, or int?? then isset will be true, and your foreach still will fail. You might want to initialize $klg like I did below: <?php if(count($_POST['Groups']) > 1 && is_array($_POST['Groups'])){ $klg = ""; foreach ($_POST['Groups'] as $xx){ $klg .= "$xx,"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/79505-solved-how-to-make-sure-array-has-data/#findComment-402642 Share on other sites More sharing options...
project3 Posted November 30, 2007 Author Share Posted November 30, 2007 Try this... <?php if (isset($_POST['Groups'])){ foreach ($_POST['Groups'] as $xx){ $klg .= "$xx,"; } } ?> If that doesn't work, post your form HTML...maybe your not setting it up to be an array. same error. if (isset($_POST['Groups'])){ foreach ($_POST['Groups'] as $xx){ $klg .= "$xx,"; } } <select name='Groups[]' id="Groups" multiple size=3 > <option value="City1" >City1</option> <option value="City2" >City2</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/79505-solved-how-to-make-sure-array-has-data/#findComment-402643 Share on other sites More sharing options...
teng84 Posted November 30, 2007 Share Posted November 30, 2007 <?php if (empty($valxx)){ //it's empty } else { //not empty } ?> I still get this error when using that Invalid argument supplied for foreach() if (empty($valxx)){ } else { foreach ($valxx as $xx){ $klg .= "$xx,"; } } to avoid that error the solution is simple define your variable as array eg.. $array = array(); now that is an array and one more you have to check your var if it fits your needs like if all you need is array then check if that is an array before processing Quote Link to comment https://forums.phpfreaks.com/topic/79505-solved-how-to-make-sure-array-has-data/#findComment-402644 Share on other sites More sharing options...
project3 Posted November 30, 2007 Author Share Posted November 30, 2007 I think isset might be a poor choice, what if $valxx is a string, or int?? then isset will be true, and your foreach still will fail. You might want to initialize $klg like I did below: <?php if(count($_POST['Groups']) > 1 && is_array($_POST['Groups'])){ $klg = ""; foreach ($_POST['Groups'] as $xx){ $klg .= "$xx,"; } } ?> ok sort of working it does ok if you select two or more but i need to still get the data if only one is selected. the error is gone now though so that is awesome. should i change that >1 to >=1 Quote Link to comment https://forums.phpfreaks.com/topic/79505-solved-how-to-make-sure-array-has-data/#findComment-402646 Share on other sites More sharing options...
teng84 Posted November 30, 2007 Share Posted November 30, 2007 yes (>=) and since you have that count i dont think you need is_array count is use in array and object so i guess no need for is array http://www.php.net/manual/en/function.count.php Quote Link to comment https://forums.phpfreaks.com/topic/79505-solved-how-to-make-sure-array-has-data/#findComment-402649 Share on other sites More sharing options...
project3 Posted November 30, 2007 Author Share Posted November 30, 2007 Thanks The Little Guy and teng84 its working perfect now. That was sweet. I had been trying to get array_key_exists() to work all day with no luck. Quote Link to comment https://forums.phpfreaks.com/topic/79505-solved-how-to-make-sure-array-has-data/#findComment-402652 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.