glen-rogers Posted April 9, 2013 Share Posted April 9, 2013 Hi I have a 'categories' mysql table and a 'subcategories' table. I need to make a form so the site owner can remove subcategories as needed. I was think of having a form with 2 dropdowns on it, the first to choose the category, the second to choose the subcategory. Then based on that selection another form is filled with the chosen subcategories? I'm unsure how to go about this though.. At the minut I have a form that is populated with all the subcategories, but there is no reference to which category they belong to. This is the file <form action='subcategoryremoveform.php' method='post' enctype='multipart/form-data' name='remove_subcategory_form' id='remove_subcategory_form'> <input type='submit' name='submit' value='Remove Subcategories' /> <?php include '../inc/connect.php'; $data = mysql_query("SELECT * FROM subcategories") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { echo "<p>"; echo "<input type='checkbox' name='check[{$info['subcat_id']}]' />"; echo $info['subcategory']; echo "</span>"; echo "</p>"; } ?> </form> <?php include '../inc/connect.php'; if(isset($_POST['check'])){ $chk = (array) $_POST['check']; $p = implode(',',array_keys($chk)); if ($sql = mysql_query("DELETE FROM subcategories WHERE subcat_id IN ($p)")){ header( 'Location: subcategoryremoveform.php' ); } else{ echo 'No subcategories have been removed'; } } ?> Can anyone offer advice on this? Quote Link to comment Share on other sites More sharing options...
Barand Posted April 9, 2013 Share Posted April 9, 2013 but there is no reference to which category they belong to. You could put a join to the category table in the query Quote Link to comment Share on other sites More sharing options...
glen-rogers Posted April 10, 2013 Author Share Posted April 10, 2013 I'm unsure how to do joins! I'll give it a try! Quote Link to comment Share on other sites More sharing options...
glen-rogers Posted April 10, 2013 Author Share Posted April 10, 2013 I was thinking maybe I could have the dropdown of categories and just have subcategories with checkboxes below, that match the category chosen in the dropdown? Quote Link to comment Share on other sites More sharing options...
Barand Posted April 10, 2013 Share Posted April 10, 2013 I'm unsure how to do joins! I'll give it a try! see http://www.phpfreaks.com/tutorial/data-joins-unions I was thinking maybe I could have the dropdown of categories and just have subcategories with checkboxes below, that match the category chosen in the dropdown? Yes, you could Quote Link to comment Share on other sites More sharing options...
glen-rogers Posted April 10, 2013 Author Share Posted April 10, 2013 Thanks for the link, I read it and bookedmarked it! I came up with this code: <form action='subcategoryremoveform2.php' method='post' enctype='multipart/form-data' name='remove_subcategory_form' id='remove_subcategory_form'> <?php include 'admininc/categorydropdown.php'; ?> <b>Category</b> <select name="cat"> <?php echo $op;?> </select> <input type='submit' name='submit' value='Get Subcategories' /> <?php include '../inc/connect.php'; if(isset($_POST['submit'])){ $cat = intval($_POST['cat']); $data = mysql_query("SELECT * FROM subcategories WHERE cat_id IN ($cat) ") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { echo "<p>"; echo "<input type='checkbox' name='check[{$info['subcat_id']}]' />"; echo $info['subcategory']; echo "</span>"; echo "</p>"; } } ?> <p><input type='submit' name='remove' value='Remove Subcategories' /></p> </form> It does the job, but is a bit rough I think. Like the second submit button is there even if there are no subcategories! Is there a way to have the form so that button is not there without any subcategories? Quote Link to comment Share on other sites More sharing options...
Barand Posted April 10, 2013 Share Posted April 10, 2013 Move it inside the "if()" section Quote Link to comment Share on other sites More sharing options...
glen-rogers Posted April 10, 2013 Author Share Posted April 10, 2013 Thank you Quote Link to comment 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.