In my CMS, I have parent and children categories in one level and in one table (id, subcat).

What i would like to do is, in PHP:

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>

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, of course a parent has no parent so the default message would show like the below screenshot)
Code being:

	
.....
if (isset($_GET['id']) && is_numeric($_GET['id']) && !is_null($_GET['id'])) {
		$categoryid = $_GET['id'];
		$query = mysql_query("SELECT * FROM ".db('prefix')."categories WHERE id='$categoryid'");
		$r = mysql_fetch_array($query);
		$frm_action = db('website').'index.php?action=process&amp;task=admin_category&amp;id='.$categoryid;
.....

Can anyone help steer me in the right direction or help with this?

Thanks alot!

Here is a picture of the new category setup:
http://img261.imageshack.us/img261/2858/addcatbz1.png
Code for new category drop down:

function category_list($var) {
	if (isset($_GET['id']) && is_numeric($_GET['id']) && !is_null($_GET['id'])) { // edit an existing category
		$categoryid = $_GET['id'];
		echo '<select name="subcat" id="subcat">';
		// code to go into here....			
		echo '</select>';					
	} else { // Add a 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>';
	}
}

    Too much information perhaps?

    I simplified it here, see the commented lines:

    // LISTS CATEGORIES...specifically SUB 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>';
    	}
    }
    
      Write a Reply...