Jump to content

[SOLVED] How to make sure array has data


project3

Recommended Posts

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,";

      }

Link to comment
Share on other sites

<?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,";

      }

}

 

 

Link to comment
Share on other sites

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,";
     }
}
?>

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

<?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

 

 

 

Link to comment
Share on other sites

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

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.