Here's the solution I came up with for checking the columns to varify only
one in a column is selected (without using JavaScript) in a matrix of radio
buttons consisting of 4 columns and 4 rows.  Doing this was complicated by
the fact that some rows did not have to be answered. The radio buttons
themselves make sure that only one item is selected in the row so all I had
to deal with was the columns.

Here's what the matrix looks like:

Rank these colors based upon your preference -- 1 is your favorite, 4 is
your least favorite.

Green 1:<input type="Radio" name="color1" value="1" /> 2:<input
type="Radio" > name="color1" value="2" /> 3: <input type="Radio"
name="color1" value="3" /> 4: 3: <input type="Radio" name="color1" value="4"
/>

and then do this for three more colors (red, blue, and yellow)  for a 4 by 4
matrix.


First I created an array of answers:  $colorarray = array($color1, $color2,
$color3, $color4);

Next I sorted that array:  arsort($colorarray);
   *Note: This is done to get the empty answers at the end.  After all the
user might only answer for green and yellow in rows 1 and 4.

Next I convert to a string using a blank space as separator:  $colorstring =
implode ($colorarray, " ");
    *Note:  This is done so that I can remove any rows of questions that the
user did not answer.  Other code makes sure that they have at least answered
one of the four colors.

After that I trim the string of blank spaces: $colorstring =
trim($colorstring);
    *Note:  This leaves me with only the questions they have answered,
regardless of whether it is one or four.

Now time to go back into an array: $colorarray = explode (" ",
$colorstring);

Next I count how many times particular values show up in an array:
$colorvalues = array_count_values ($colorarray);
   *Note:  This gives me an array with how many times each value shows up.
So if the user selects three items in column 1 of the matrix, the array will
show the key as 1 and the value as 3.

Next I count how many questions have been answered:  $colorcount = count
($colorarray);

With $colorcount and $colorvalue I can then do the following to give the
appropriate error message:

   if ($colorvalues17[1] == '')
     { echo "This is not a problem.";}
     else{
         if ($colorvalues[1] >1)
                 {echo "You have too many colors ranked first (column 1).
Please change your answers so you only have one item in the first
column.";}
     }
   if ($colorvalues17[2] == '')       { echo "This is not a problem.";}
     else{
         if ($colorvalues[2] >1)
                 {echo "You have too many colors ranked second (column 2).
Please change your answers so you only have one item in the second
column.";}
    }

and so on for 3 and 4.  Why the two "if - else" statements for each column?
The problem was -- remember -- that they don't have to answer all the
questions so I need to make sure there is an answer before I evaluate the
array_count_values to see if it is greater than one.

I suppose that there is a simpler and easier way to do this but I was
pleased that as a newbie I was able to come up with this solution without
expert help.  In addition, with this same set of variables, I was also able
to validate that the user ranked them appropriately. What I mean by this is
that if the person, for example,  answered three of the four, they had to
rank them 1, 2, and 3 and get an error message if they rank them 2, 3, and
4.   The same was done if they rank only 1 or rank only 2 as well.

I am rather amused by the fact that  the "experts" were too busy telling me
what to do (even when I said JavaScript was not to be used)instead of
actually helping me learn and come up with a solution.  But, then, I suppose
this problem was too simple for them.   I was tempted to not post my
solution but then I'd be guilty of the same.   I hope this is of some use to
other newbies out there.

HiFiTubes

Reply via email to