centered effect Posted November 24, 2007 Share Posted November 24, 2007 In my CMS, I have parent and children categories in one level and in one table (id, name, subcat). What i would like to do is, in PHP of course: Upon editing a category, in a dropdown box, show selected, the parent of the category. if there is no parent, show selected, a message saying "Not a subcategory". In the selection though, still shows the parent options as possible selections. Example: Upon creating a new category, by default not a subcategory: <select name="subcat" id="subcat"> <option value="0" selected="selected">not a sub category</option> <option value="1">catOne</option> <option value="2">catTwo</option> <option value="4">catThree</option> <option value="8">catFour</option> </select> Upon editing a category, say a subcategory whose parent is "catTwo", should reflect the parent: <select name="subcat" id="subcat"> <option value="0">not a sub category</option> <option value="1">catOne</option> <option value="2" selected="selected">catTwo</option> <option value="4">catThree</option> <option value="8">catFour</option> </select> When a user clicks on the category they wish to edit becomes the $categoryid = the category's primary id. An example in relation to the select options above would be index.php?action=edit_category&id=3 (3 being a subcat and 1 being the parent, referencing the above) The categories table looks like this: id name description subcat 1 catOne Parent One 0 2 catTwo Parent Two 0 3 subOne Child One 1 4 catThree Parent Three 0 5 subTwo Child Two 1 6 subThree Child Three 4 7 subFour Child Four 2 8 catFour Parent Four 0 A new category screen is shown as an example of the setup: The code I have so far is below. It shows the parent category list in a dropdown box. The add category dropdown is fine, but the edit dropdown is the problem. I did not write all of this code below, just an fyi // LISTS CATEGORIES function category_list($var) { if (isset($_GET['id']) && is_numeric($_GET['id']) && !is_null($_GET['id'])) { // Edit the category $categoryid = $_GET['id']; echo '<select name="subcat" id="subcat">'; // Editing code is here.... // Show "not a subcategory" and parents // Selected option is defined based on $categoryid // $categoryid = parent (0) then is is not a subcategory // $categoryid = subcat (>0) then show the parent selection echo '</select>'; } else { // Add new category echo '<select name="subcat" id="subcat">'; $query = 'SELECT * FROM '.db('prefix').'categories WHERE subcat = 0'; isset($var) ? $query .= ' AND id <> $var' : $query .= ''; $result = mysql_query($query); echo '<option value="0"'; !isset($var) ? print ' selected="selected">' : print '>'; echo 'not a sub category</option>'; while ($r = mysql_fetch_array($result)) { echo '<option value="'.$r['id'].'"'; isset($var) ? print ' selected="selected">' : print '>'; echo $r['name'].'</option>'; } echo '</select>'; } } Can anyone help show me the right direction I need to go with this? Do I need a join in my sql or do I need multiple sql queries? Moreso, how do I accomplish this after the query is completed? I am not sure how to go about this... Thanks for any and all help!! Link to comment https://forums.phpfreaks.com/topic/78642-showing-selected-parent/ Share on other sites More sharing options...
centered effect Posted November 26, 2007 Author Share Posted November 26, 2007 Should I have not said thank you so quickly? Link to comment https://forums.phpfreaks.com/topic/78642-showing-selected-parent/#findComment-399140 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.