Jump to content

validate an array has any > 0


turpentyne

Recommended Posts

This is probably a simple one, but I'm not experienced with arrays.

 

I have a form with looped dropdowns for items from a database. On submit it goes to a second page. Before I run any script I want to make sure the array created from the dropdowns contains anything greater than 0...

 

I thought this would do it:

 

if (isset($_POST['participantqty']) && ($_POST['participantqty']) > 0){

But it does nothing.

 

I also tried:

if (isset($_POST['participantqty[]']) && ($_POST['participantqty[]']) > 0){

 

 

 

 

 

Link to comment
Share on other sites

max()

 

This will return the highest value in the array. But, you are referring to the field incorrectly

 

if (isset($_POST['participantqty']) && max($_POST['participantqty']) > 0){

 

You would likely need to do some additional validations based upon what you expect the values to be. If you don't want the '0' values int eh array you might consider running it through array_filter() first.

Link to comment
Share on other sites

This is probably a simple one, but I'm not experienced with arrays.

 

I have a form with looped dropdowns for items from a database. On submit it goes to a second page. Before I run any script I want to make sure the array created from the dropdowns contains anything greater than 0...

 

I thought this would do it:

 

if (isset($_POST['participantqty']) && ($_POST['participantqty']) > 0){

But it does nothing.

 

I also tried:

if (isset($_POST['participantqty[]']) && ($_POST['participantqty[]']) > 0){

 

 

 

 

 

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.