boblee Posted December 26, 2008 Share Posted December 26, 2008 Hello all, I want to create an array from information posted from checkboxes which are created by a foreach statement. For example information is taken out of the database table full of product numbers and several checkboxes are displayed for every product number, when the user checks multiple boxes and hit submit an array of the products in created. I got the checkbox part working, but I don't how to create the array. Would any one mind filling me in? -Thanks the code that works do far: $conn = "SELECT product_id FROM `products`"; $result = mysql_query($conn, $query); while ($row = mysql_fetch_row($result)) { $product[] = $row[0]; } foreach ($product as $y) { echo "<input class='checkbox' type='checkbox' name='$i++' value='$y'>\n" . $y . "</input>\n"; } Link to comment https://forums.phpfreaks.com/topic/138447-creating-an-array/ Share on other sites More sharing options...
php1 Posted December 26, 2008 Share Posted December 26, 2008 To create an array of checkbox you have to use name='checkbox[]' foreach ($product as $y) { echo "<input class='checkbox' type='checkbox' name='$i++[]' value='$y'>\n" . $y . "</input>\n"; } Link to comment https://forums.phpfreaks.com/topic/138447-creating-an-array/#findComment-723879 Share on other sites More sharing options...
kenrbnsn Posted December 26, 2008 Share Posted December 26, 2008 Why are you creating an extra array to hold the "products"? Also, where is the variable "$i" set? You can create the checkbox line in the while loop: <?php $conn = "SELECT product_id FROM `products`"; $result = mysql_query($conn, $query); while ($row = mysql_fetch_assoc($result)) { echo '<input class="checkbox" type="checkbox" name="product[]" value="' . $row['product_id'] . '">' . $row['product_id'] . "</input>\n"; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/138447-creating-an-array/#findComment-723903 Share on other sites More sharing options...
boblee Posted December 26, 2008 Author Share Posted December 26, 2008 Probably because I don't know what I'm doing As for the the variable "$i", just a typo I guess, I thought I had 'products_id' in there. alright so using that while statement I can create the series of check boxes, but how do I create an array based on the boxes the user has checked? -Thanks Link to comment https://forums.phpfreaks.com/topic/138447-creating-an-array/#findComment-724047 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.