Jump to content

arrays and radio buttons


pdunn

Recommended Posts

Hello I have a minor problem with radio buttons and arrays. 

I need to get a "Yes" or a "No" from the user. I will use radio buttons just like in the rest of the site. Since these button values depends on the amount of people from the previous form, I have to use an array as the variable.

If no information is typed in the second form and I try to verify, I get notices that say that "Notice: Uninitialized string offset: 2 in /home/web/test.php on line 768". That is because no information was written in the array. I get this message when using !isset(array($loop)) and I tried empty() (but that's nearly {less than} the same). array($loop) of uninitialized value will always give me that Notice.

I did made a new discovery. Regardless how many items the array is SUPPOSED to have, if at least one is filled in, there is no error. What I meant by SUPPOSED is that if the amount of people is 20, the array should be of size 20. However, if the user only fills in the information for at least 1 person, then there will not be a notice about
uninitialization string and my error checking works with isset(). However, if none is set, then the problem occurs with isset(), empty(), etc. Any call to array(index) will result in an error since nothing is inside of the array. But if one thing is inside of the error, then the index is simply not set. So, how do I see if anything is in an array? Without printing it out, but just checking.

count(), this did not work, it gives the value of 1 and if one item is in the array count() give out 1.

Thank you,

pdunn
Link to comment
Share on other sites

I can't tell you exactly what is wrong without seeing your code, but in general if you don't know if something will be set or is or is not array you need to test each instance.

A simple way to test for an array and if it's not empty is to do!

[code]if ( isset ( $array[0] ) ) { ... }[/code]

It much easier than...

[code]if ( isset ( $array ) && is_array ( $array ) && ! empty ( $array ) ) { ... }[/code]

What I'm trying to say is that you do testing based on what is expected, not based on guessing!


me!
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.