ttmt Posted January 27, 2009 Share Posted January 27, 2009 I've posted before about this, but no luck and it's driving me mad. I have a while loop that is generating a series of checkboxes the results from them are captured in name = "section[]". This loop is called from within a for loop so the series of checkboxes are repeated a number of times down the page, the results from all of them are captures in section[]. Is it possible to create a separate array for each group of checkboxes - within the while loop a different array would be used to capture the information for that series of checkboxes Something like name="section['.$row[0].']" I've tried this but couldn't access it with php $_POST['section'][1]? <?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[]]" />'; } $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> If I was to hard code it with html it would look like this. <body> <div id="con"> <form name="upload_form" action="upload.php" method="post" enctype="multipart/form-data"> <p> <b>Image:</b> <textarea name="title[]" cols="30" rows="1"></textarea> </p> <p> <b>Sections:</b> Type: <input type="checkbox" value="1" name="section[]" /> Color: <input type="checkbox" value="2" name="section[]" /> People: <input type="checkbox" value="3" name="section[]" /> Places: <input type="checkbox" value="4" name="section[]" /> Buildings: <input type="checkbox" value="5" name="section[]" /> </p> <p> <b>Image:</b> <textarea name="title[]" cols="30" rows="1"></textarea> </p> <p> <b>Sections:</b> Type: <input type="checkbox" value="1" name="section1[]" /> Color: <input type="checkbox" value="2" name="section1[]" /> People: <input type="checkbox" value="3" name="section1[]" /> Places: <input type="checkbox" value="4" name="section1[]" /> Buildings: <input type="checkbox" value="5" name="section1[]" /> </p> <p> <b>Image:</b> <textarea name="title[]" cols="30" rows="1"></textarea> </p> <p> <b>Sections:</b> Type: <input type="checkbox" value="1" name="section2[]" /> Color: <input type="checkbox" value="2" name="section2[]" /> People: <input type="checkbox" value="3" name="section2[]" /> Places: <input type="checkbox" value="4" name="section2[]" /> Buildings: <input type="checkbox" value="5" name="section2[]" /> </p> <br/> <p> <input type="submit" name="submit" value="Add Photos" /> </p> </form> </div> </body> </html> Each group of checkboxes has a different name="section[]", so I could then access that array with <?php echo "<pre>"; print_r($_POST['section']); print_r($_POST['section1']); print_r($_POST['section2']); echo "</pre>"; ?> Hope this makes sense and someone can help. Link to comment https://forums.phpfreaks.com/topic/142622-is-this-possible/ Share on other sites More sharing options...
soak Posted January 27, 2009 Share Posted January 27, 2009 I think youi're nearly there. How about something like this: It's not the prettiest code but it should do what you need. <?php require_once("includes/connection.php"); require_once("includes/functions.php"); include("includes/header.php"); $query = "SELECT key_id, words FROM keywords"; $result = mysql_query($query); confirm_query($result); $options = array(); $number_of_uploads = 3; // Initialise your options strings for ($i = 1; $i <= $number_of_uploads; $i++) { $options[$i] = ''; } // build the checkboxes while($row = mysql_fetch_array($result)){ for ($i = 1; $i <= $number_of_uploads; $i++) { $options[$i] .= $row[1] . ':<input type="checkbox" value="'.$row[0].'" name="section'.$i.'[]]" />'; } } // now echo the form to the screen for ($i = 1; $i <= $number_of_uploads; $i++) { ?> <p> <b>Image:</b> <textarea name="title<?= $i ?>" cols="30" rows="1"></textarea> </p> <p> <b>Sections:</b> <?php echo $options[$i] ;?> </p> <?php } You can recover the data from the form using: <?php $sections = array(); $titles = array(); for ($i = 1; $i <= $number_of_uploads; $i++) { $sections[$i] = $_POST['section'.$i]; $titles[$i] = $_POST['title'.$i]; } I hope that all makes sense! -- A digital agency Link to comment https://forums.phpfreaks.com/topic/142622-is-this-possible/#findComment-747568 Share on other sites More sharing options...
ttmt Posted January 27, 2009 Author Share Posted January 27, 2009 I've been looking at this for 1 day now and just realized what I was doing wrong before I saw your post. Your code covers it perfect - thanks for your help. Link to comment https://forums.phpfreaks.com/topic/142622-is-this-possible/#findComment-747582 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.