using a variable inside a variable ??

A

Anonymous

Guest
I have a page that has 138 checkboxes on it. I have the page set so it can accept the input and also reshow the saved input.
Code:
<?php if (($r1) == "Y") { 
echo '<input type="checkbox" name="r1" value="ON" checked>';
} else {
echo '<input type="checkbox" name="r1" value="ON">'; } ?>
The names of the checkboxes are r1 r2 r3 r4 ... r137 r138. I also have 138 columns in a mysql database named the same r1 r2 .. etc.

When the form is submitted I get the checkbox value like this. (Now I know this is not the best way to get a checkbox value but it works so I have been using it. I convert the checkbox value to either Y or N.)
Code:
if ($r1 == "ON") {
$r1 = "Y";
} else {
$r1 = "N";
}

Now rather than write this 138 times to set the variables to Y or N for each checkbox, and then input 138 values into the database at once it would be a lot easier to have a loop that sets the variable and inputs the value one at a time. My attempts so far at writing this loop have failed. Any hints or help would be greatly appreciated.
 
First of all, you should be using $_GET['r1'] or $_POST['r1']. And turn register_globals off.

Secondly, what you're after is an array. Check the FAQ section entitled "How do I create arrays in a HTML <form>?"
 
swirlee said:
First of all, you should be using $_GET['r1'] or $_POST['r1']. And turn register_globals off.
oh, yeh I was using $_POST['r1'] ... I changed it in my attempts to get it working.

swirlee said:
Secondly, what you're after is an array. Check the FAQ section entitled "How do I create arrays in a HTML <form>?"
Looks like an array is exactly what I am after .. thanks. I suppose questions like mine make you want to :shock: but I had no idea how to word my search. Considering my very short time in php/mysql coding I have done some really good stuff on my website. I do appreciate your help. :-D
 
Back
Top