idontknowphp Posted April 14, 2012 Share Posted April 14, 2012 I have figured out far enough to loop through my table and return a list of check boxes that will be matched to a product. My issue now, is i cannot figure out the proper way to loop through them after they are selected and return them as checked/unchecked for the given product. Thought there is some MySQL involved, that part isn't the problem, I know how to insert into a database or update...when the product is selected from a drop down, I need to be able to populate the checkboxes. Here is the code i am using to initially display all of the categories to choose from... <ul class="categories"> <?php $query = "SELECT * FROM products"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $category = $row['category']; $catId = $row['id']; echo "<li><input class='catCheck' type='checkbox' name='p_cat[]' value='$catId' /> $category</li>"; } ?> </ul> Thanks guys.... Quote Link to comment https://forums.phpfreaks.com/topic/260909-checkboxes-saving-and-retrieving-values-phpmysql/ Share on other sites More sharing options...
MMDE Posted April 14, 2012 Share Posted April 14, 2012 There needs to be more HTML... A form tag specifying where and how the data should be be sent, and a submit button. <?php foreach($_POST['p_cat'] AS $p_cat){ echo $p_cat; } ?> <ul class="categories"> <form action="" method="post"> <?php $query = "SELECT * FROM products"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $category = $row['category']; $catId = $row['id']; echo "<li><input class='catCheck' type='checkbox' name='p_cat[]' value='$catId' /> $category</li>"; } ?> <input type="submit" name="submit" value="submit" /> </form> </ul> ^ I Quote Link to comment https://forums.phpfreaks.com/topic/260909-checkboxes-saving-and-retrieving-values-phpmysql/#findComment-1337258 Share on other sites More sharing options...
idontknowphp Posted April 14, 2012 Author Share Posted April 14, 2012 I am aware of that, I was posting only the pertinent code....here is the whole "form" portion: <form id="theform" action="manage-products.php?action=addProduct" method="post"> <input type="text" name="p_name" id="p_name"/> <textarea name="p_desc"></textarea> <input "name="submit" type="submit" value="Create Product"/> <ul class="categories"> <?php $query = "SELECT * FROM products"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $category = $row['category']; $catId = $row['id']; echo "<li><input class='catCheck' type='checkbox' name='p_cat[]' value='$catId' /> $category</li>"; } ?> </ul> </form> Quote Link to comment https://forums.phpfreaks.com/topic/260909-checkboxes-saving-and-retrieving-values-phpmysql/#findComment-1337260 Share on other sites More sharing options...
MMDE Posted April 14, 2012 Share Posted April 14, 2012 If you had run the code I gave you earlier, you would have seen I only did one thing wrong and that it answered your problem, though it seems I forgot to mention it (you can see I started saying something under "^ I". It would give you an error about $_POST['p_cat'] not being set if it wasn't set. You also have not connected to MySQL or chosen database. Run this code, and you will see it does what you want. So try to understand it. The solution lies with the foreach loop. <?php if(isset($_POST['p_cat'])){ foreach($_POST['p_cat'] AS $p_catID){ echo 'You selected category: '.$p_catID.'.<br/>'; // mysql_query('UPDATE table SET col = value WHERE id = '.$p_catID); } } ?> <form id="theform" action="" method="post"> <input type="text" name="p_name" id="p_name"/> <textarea name="p_desc"></textarea> <input "name="submit" type="submit" value="Create Product"/> <ul class="categories"> <?php //$query = "SELECT * FROM products"; //$result = mysql_query($query) or die(mysql_error()); //while($row = mysql_fetch_assoc($result)){ // $category = $row['category']; // $catId = $row['id']; for($i=0; $i<10; $i++){ $rows[$i]['id'] = $i; $rows[$i]['category'] = 'category '.$i; } foreach($rows AS $row){ $catId = $row['id']; $category = $row['category']; echo "<li><input class='catCheck' type='checkbox' name='p_cat[]' value='$catId' /> $category</li>"; } ?> </ul> I added some extra lines of code just while I were testing. Quote Link to comment https://forums.phpfreaks.com/topic/260909-checkboxes-saving-and-retrieving-values-phpmysql/#findComment-1337271 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.