SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Selecting Parents

    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:
    Code:
    <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:
    Code:
    <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>
    Can anyone help steer me in the right direction or help with this?

    Thanks and happy turkey day!

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What the heck are you trying to do?

    What do you mean edit a category?

  3. #3
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You need to run a query, get the results, and loop through them. Echo out an <option> tag for each result, and if the current result ID is equal to the current selected ID, put the selected="selected" in there. Without knowing exact details its difficult to give you some code, but if your category info is in an array indexed on ID, try this:

    PHP Code:
    $arrCategories = <an array of categoriesout the database or something>
    $intCurrentSubCategoryID = <currently selected sub category ID>

    echo 
    "<select name=\"subcat\" id=\"sabcat\">\r\n";
    echo 
    " <option value=\"0\">not a sub category</option>\r\n";
     
    foreach (
    $arrCategories as $intCategoryID => $arrCategoryInfo) {
     
    $strSelectedText "";
     if (
    $intCategoryID == $intCurrentSubCategoryID) {
      
    $strSelectedText " selected=\"selected\"";
     }
    //if

     
    echo " <option value=\"" $intCategoryID "\"" $strSelectedText ">" $arrCategoryInfo['name'] . "</option>\r\n";
    }
    //foreach

    echo "</select>\r\n"
    If is better if you use objects though instead of an array.

  4. #4
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    wonshikee - sorry I tried to be as clear as possible. What is not understood?

    Stormrider - thanks, I need to look over the code you have given. Not sure what other details are needed but 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)

    A new category screen is shown as an example of the setup:
    Last edited by centered effect; Nov 23, 2007 at 03:40.

  5. #5
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yeh, the code above should do the trick, but you will need to modify it depending on how you are storing your category data, or if you are using objects etc. It gives you the general idea though. You just loop through the categories available, and echo out an option tag for each one. If the option tag you are echoing out is the current parent category, then stick the ' selected="selected"' in there too.

  6. #6
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    $intCurrentSubCategoryID = <currently selected sub category ID>

    What if the category is not a subcategory? How is the parent defined? Perhaps I am not seeing how it is relating to what I am trying to achieve

    The code being used is below:
    Code:
    // 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>';
    	}
    }
    Last edited by centered effect; Nov 23, 2007 at 22:36.

  7. #7
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    $intCurrentSubCategoryID is the parent ID of the current category from database.

    If the category is not subcategory, the id will be NULL (or 0, whatever you have).

    Your function is a bit odd though. The $var parameter is basically unused and you pick up the id from $_GET. So why not pass the id from outside? What if you did this:

    Code php:
    // LISTS CATEGORIES...specifically SUB CATEGORIES
    function category_list($var=null) {
    	$var=(int)$var;
    	echo '<select name="subcat" id="subcat">';
       	$query = 'SELECT * FROM '.db('prefix').'categories WHERE subcat = 0';
       	$result = mysql_query($query);
     
       	echo '<option value="0"';
       	empty($var) ? print ' selected="selected">' : print '>';
       	echo 'not a sub category</option>';
     
       	while ($r = mysql_fetch_array($result)) {
         		echo '<option value="'.$r['id'].'"';
          		$var==$r['id'] ? print ' selected="selected">' : print '>';
          		echo $r['name'].'</option>';
       	}
    	echo '</select>';
    }

    And use it as follows, for editting:

    PHP Code:
    category_list($parent
    and adding new:

    PHP Code:
    category_list() 
    Having the $parent above picked up from the database beforehand.

  8. #8
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks php_daemon. Sorry I walked away from this and now need to come back to it.

    Utilizing your solution I now have this:
    PHP Code:
    function category_list($id) {
        if (isset(
    $_GET['id']) && is_numeric($_GET['id']) && !is_null($_GET['id'])) { $var $id; }
        echo 
    '<select name="subcat" id="subcat">';    
        
    $selected =' selected="selected"'
        
    $result mysql_query("SELECT * FROM ".db('prefix')."categories WHERE subcat = 0");
            if (!empty(
    $var)) { $parent_selection $selected; }        
               echo 
    '<option value="0"'.$parent_selection.'>'.l('not_sub').'</option>';
           while (
    $r mysql_fetch_array($result)) {
            
    $subresult mysql_query("SELECT * FROM ".db('prefix')."categories WHERE id = ".$var." AND subcat = ".$r['id']);    
            while (
    $sub mysql_fetch_array($subresult)) {    
                if (
    $var == $sub['id']) { $selection $selected; } 
                   echo 
    '<option value="'.$r['id'].'"'.$selection.'>'.$r['name'].'</option>';}
            echo 
    '<option value="'.$r['id'].'">'.$r['name'].'</option>';}        
        echo 
    '</select>';} 
    My issue is that If i am editing a child category, the parent is shown twice in the selection box, one as selected. How can i exclude the non-selected parent?
    Last edited by centered effect; Dec 27, 2007 at 05:56.

  9. #9
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Bump?
    Passing the selection variable outside the inner loop makes all parent categories as selected...

    so back to my question... and a screen to show the problem

  10. #10
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    But you're not doing it by my example at all. What are you trying to achieve with the nested loop?
    Saul

  11. #11
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I fixed the problem
    $id is passed from the outside as you suggested
    Code:
    function category_list($id) {
    	if (isset($_GET['id']) && is_numeric($_GET['id']) && !is_null($_GET['id'])) { $var = $id; }	
    	echo '<select name="subcat" id="subcat">';	
    	$selected =' selected="selected"'; 
    	$result = mysql_query("SELECT * FROM ".db('prefix')."categories WHERE subcat = 0");
    		if (!empty($var)) { $parent_selection = $selected; }		
       		echo '<option value="0"'.$parent_selection.'>'.l('not_sub').'</option>';
       	while ($r = mysql_fetch_array($result)) {
    		$child = retrieve('subcat','categories','id',$var); 
    		if ($r['id'] == $child) { echo '<option value="'.$r['id'].'"'.$selected.'>'.$r['name'].'</option>'; } else {
        	echo '<option value="'.$r['id'].'">'.$r['name'].'</option>';}}		
    	echo '</select>';}
    When editing the child category, I wanted to show the selected parent once. This solution now works.

    $child get the value from SELECT subcat FROM ".db('prefix')."categories WHERE id = $var


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •