views:

31

answers:

3

I'm trying to use PHP to do this:

<if !empty($list) {echo
  .
  .
  .
?>

And get the result:

Additional options:
    <p><input type="checkbox" name="1">1</input></label></p>
    <p><input type="checkbox" name="2">2</input></label></p>
    .
    .
    .
    <p><input type="checkbox" name="n">n</input></label></p>
  </legend>
</fieldset>
+1  A: 

Assuming $list is an array

for($i=1; $i<count($list); $i++)
{
 echo '<p><input type="checkbox" name="'.$i.'">'.$i.'</input></label></p>'."\n";
}

If not, please provide the contents of $list.

Gazler
One minor issue. `\n` would be taken literally, given the use of single quotes.
Brad F Jacobs
$list is a tags array. Thank you.
Fohsap
Ahh, you're quite right, hacked around it now.
Gazler
Just common series terminology. When you expand a series, you usually write in: first pass, second pass, ..., nth pass. :) In a series of n length, "n" is the variable which indicates the last pass to be taken in the series.
Fohsap
premiso meant the \n (newline) at the end of my line, not n as in "consider a number n."
Gazler
I see. Well, that is to be desired. Thank you, Gazier. Your comments were very helpful and informative.
Fohsap
+2  A: 

Given the context of the question, I am guessing here. But it seems that the you do not understand the checkbox, given that you do not even assign it a value that and this would be a pain to loop through on the form processing end.

Assuming that $list is an array (borrowing some code from Gazler)

$cnt = count($list);
$checkBoxes = "";

for ($i=1; $i<$cnt; $i++) {
    $checkBoxes .= '<p><input type="checkbox" name="checkBoxes" value="' . $i . '">' . $i . '</input></label></p>' . PHP_EOL;
}

echo $checkBoxes . '</legend>' . PHP_EOL . '</fieldset>';

Then on your form processing side, it will be easy to loop through the checked boxes like so:

if (isset($_POST['checkBoxes'])) {
    foreach ($_POST['checkBoxes'] as $val) {
        // $val will contain the value of the selected boxes
    }
}

Using this system, it should get you to where you want to be.

Brad F Jacobs
+1 for decrypting the question.
Gazler
Thank you. Please, excuse me for this follow-up question: if value isn't necessary, why include $val in the response? Is this so that we know whether or not the checkboxes are checked?
Fohsap
$val will return 'on' if the checkbox is selected.
Gazler
Thank you. This answer was very comprehensive. It rocked. I wish I had enough credit to give you another up. I can now create a series of n length, with however many plotable bars {i.e., [a, (1, 2, ..., n)]; [b, (1, 2, ..., n)]; and [c, (1, 2, ..., n)]}. I have a better sense of $_POST. I can use additional 'echo' commands to amend tokens. This is great. What an awesome bit of code to throw into a .php file. Thank you. You're a true pro.
Fohsap
Not a problem, glad it helped you out.
Brad F Jacobs
I actually don't understand why you use a value. Wouldn't it be best to use an id, since each is different, if anything? And why not just search by label and then inspect to see if checked="checked" or checked="unchecked"?
Fohsap
Not sure if you can search by label, but doing that would require a lot of static code persay. Setting a value allows you to set that to be the id, if you want, I just set it up how you described, IE decrypted what I thought you were asking. But setting the value to an ID, would be preferred and why not search by label, there is no need to when you can loop through it all and just pull the id, as that is what you need in the end right?
Brad F Jacobs
A: 

Part 1:

// echo the 3-bar, expanded series (alternating sequence), of the tags array
<legend>Additional Options:</p>
$cnt = count($list);
$checkBoxes = "";

for ($i=1; $i<$cnt; $i++) {
    $checkBoxes .= '<p><input type="checkbox" name="checkBoxes" value="' . $i . '">' . $i . '</input></label></p>' . PHP_EOL;
}

echo $checkBoxes . '</legend>' . PHP_EOL . '</fieldset>' . "\n" . </legend>;

Part 2:

// loop through the checkboxes
if (isset($_POST['checkBoxes'])) {
    foreach ($_POST['checkBoxes'] as $val) {
        // $val will contain the value of the selected boxes
    }
}
Fohsap