Dynamically name array <input> checkbox ?

Is it possible to generate dynamic array names for input checkbox forms ?

I'm using this code to generate a form of text fields and checkboxs

<?php 
  require_once("includes/connection.php"); 
  require_once("includes/functions.php"); 
  include("includes/header.php"); 
?> 
<?php 
	$query = "SELECT key_id, words FROM keywords"; 
	$result = mysql_query($query); 
	confirm_query($result); 
	$options = ""; 
	while($row = mysql_fetch_array($result)){ 
		$options .= "{$row[1]}:<input type=\"checkbox\" value=".$row[0]." name=\"section[]\" />\n"; 
	} 
	$number_of_uploads = 3; 
?> 
<body> 
	<div id="con"> 
		<form name="upload_form" action="upload.php" method="post" enctype="multipart/form-data"> 
		<?php 
			for($counter = 1;$counter<=$number_of_uploads;$counter++){ 
		?> 
		<p> 
		<b>Image:</b> 
			<textarea name="title[]" cols="30" rows="1"></textarea> 
		</p> 
		<p> 
		<b>Sections:</b> 
		<?php echo $options ;?> 
		</p> 
		<?php 
			} 
		?> 
		<br/> 
		<p> 
			<input type="submit" name="submit" value="Add Photos" /> 
		</p> 
		</form> 
	</div> 
</body> 
</html>

The while loop generates the checkbox's and the values from all the checkbox's are stored in section[] which can them be used from $_POST.

while($row = mysql_fetch_array($result)){ 
	$options .= "{$row[1]}:<input type=\"checkbox\" value=".$row[0]." name=\"section[]\" />\n"; 
}

What I want to be able to do is create a seperate array for each loop of the while loop so each set of checkbox's created in one loop has it's own array.

Something like:

name="section[].$row[0]\"

How can I have a seperate array for each loop of the array?

How can I then access this array with php from the $_POST array ?

I thought I might be able to access the array with something like:
But this doesn't work.

<?php
	echo "<pre>";
		print_r($_POST['section'['.1.']]);
	echo "</pre>";
?>

I don't really understand what you're trying to do but here goes:

You'll need to put your $row[0] inside the the $section[]:

$section[$row[0]];

Access via $_POST['section']['whatever you decide in here'] . You can check loop out the array content with a

$i = 0;
while ($i < count($_POST['section'])){
  echo $_POST['section'][$i];
  $i = $i + 1;
}

This while loop, creates a list of checkboxes, the infomation from the boxes is captured in the array section[].

while($row = mysql_fetch_array($result)){ 
                $options .= "{$row[1]}:<input type=\"checkbox\" value=".$row[0]." name=\"section[]\" />\n"; 
        }

This code is called in another loop so there are a number of these groups of checkboxes. The information for all of them is captured in section[].

Here is a hmtl example of what it will look like.

http://www.ttmt.org.uk/test/test.html

I want to be able to capture each group of checkboxes in a seperate array, so the first group of checkboxes will be section1[], the second section2[] etc.

I have used your example to name the name array

name="section['.$row[0].']

but I can't access this array with php using

<?php
	echo "<pre>";
		$i = 0;
		print_r($_POST['section'][$i]);
	echo "</pre>";
?>

If you want "section{x}[]" as a name for your checkboxes for a single record:

Place your keyword data from the db into an easy to use array ($keyword) for looping. This avoids having to set the internal pointer of the recordset back to 0 every time. This array van hold your 'key_id' values or your 'words' or both!

//do single widgets if any
$i = 1;
while($i <= $num_uploads){
  //do other repeated widgets - eg description
     $j = 0;
     while($j < count($keyword)){     
       echo "\n<label>{$keyword[$j]}: <input type=\"checkbox" name = \"section{$i}[]\" value=\"{$keyword[$j]}\"  /></label> ";
       $j = $j + 1;
     }
  //do other repeated widgets if any
  $i = $i + 1;
}
//do single widgets (submit etc)

The only problem with this nomenclature (section{x}[]) is that it may be difficult to loop through your $_POST[] array. I would suggest having a hidden field in your form that stores the number of uploaded images so that this is also passed to the form handling code.

e.g. echo "<input type=\"hidden\" id=\"num_up\" name=\"num_up\" value=\"{$num_uploads}\" /> In this way you can loop through your $_POST array variables at the other end:

$num = $_POST['num_up'];
$x = 1;
while($x <= $num){
  if(isset($_POST['section' . $x])){
    (... do your processing ...print_r($_POST['section' . $x]) ...)
  }
  $x = $x + 1;
}

This is off the top of my head, so you may have to debug or maybe rethink your naming conventions for the section array - it seems a bit complex to me.

This article has been dead for over six months. Start a new discussion instead.