Jump to content


Photo

arrays and radio buttons


  • Please log in to reply
2 replies to this topic

#1 pdunn

pdunn
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 September 2006 - 02:16 PM

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

#2 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 21 September 2006 - 02:30 PM

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!

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

It much easier than...

if ( isset ( $array ) && is_array ( $array ) && ! empty ( $array ) ) { ... }

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


me!

#3 pdunn

pdunn
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 September 2006 - 03:48 PM

Thank you,

you were helpful in solving the problem.  Using empty($array) is inteligent and I should have thought of that (checking the whole array instead of just indices, especially since I knew that there were no indices).


pdunn




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users