Re: Validating Radio Buttons in two directions
| From: | Barry | Date: | Thu, 02 Feb 2006 08:57:41 +0000 |
| Subject: | Re: Validating Radio Buttons in two directions | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-229699@lists.php.net to get a copy of this message | ||
HiFi Tubes wrote:
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, " ");$newArray = $colorarray; foreach ($colorarray as $key => $value) {*Note: This is done so that I can remove any rows of questions that theuser 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. also can be done like:
if ($value == NULL) unset($newarray[$key]);} Removes every empty arrayfield from your array. And you don't have to implode and then explode it again. How you do here:
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] == '')1. There is also called a function elseif() so you dont have to write if () {{ 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.";}}
else
{
if () ..code code..;
}
}
just write:
if ()
{
elseif()
{
.. code code ..
}
}
that's the same.
2. Btw, why can't they rank each color first place O_o ?
3. where does $colorvalues17 come from x_X ?
4. also here can be foreach used
foreach ($colorvalues as $key => $value)
{
if ($value == 0) echo "you rule! no problem!";
elseif ($value >= 1) echo "Ouch! Just one rank for each color!";
}
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. Yes there is always an easier way.I would even say, my way can much more be simplyfied, but thats not the point. As long as it works, and the code is ok (who cares?).
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.You would wonder how much "newbies" are on this list. Show me just one "PHP expert" in here and you get 20 bucks. Regards Barry -- Smileys rule (cX.x)C --o(^_^o)