WordPress.org

Forums

[resolved] PHP Form: Select Categories (3 posts)

  1. Greg Rickaby
    Member
    Posted 5 years ago #

    Refining my options panel and I want to offer the user the option to select certain categories from a drop down. This works - and even saves. However after the save the drop down list doesn't "remember" the selection and if you save again changes are lost.

    <select class="aselect" id="<?php echo $value['id']; ?>"  name="<?php echo $value['id']; ?>">
    	<option value="0"><?php _e('Select a Category', 'thetimes'); ?></option>
    		<?php	global $options;
    					foreach ($options as $value) {
    						if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] );
    						}
    					}
    
    					$categories = &get_categories('type=post&orderby=name&hide_empty=1');
    
    					if ($categories) {
    						$find_select = $tt_catbox_1;
    
    						foreach ($categories as $category) {
    							$selected = (in_array($category->cat_ID, $find_select)) ? ' selected="selected"' : '';
    							echo '<option value="' . $category->cat_ID . '"' . $selected . '>' . $category->cat_name . '</option>' . "\n";
    						}
    					}
    		?>
    </select>

    $tt_catbox_1 contains the category ID

    I've Googled and found the above code, however I can't get the "SELECTED" to work quite right. I think I need to pull from the database the settings. How do I do that?

  2. Greg Rickaby
    Member
    Posted 5 years ago #

    Solved it...

    <select class="aselect" id="<?php echo $value['id']; ?>"  name="<?php echo $value['id']; ?>">
    			<?php
    				global $options;
    					foreach ($options as $value) {
    						if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] );
    						}
    					}
    				$cats = get_categories('hide_empty=0'); foreach($cats as $cat) { ?>
    				<option value="<?php echo $cat->cat_ID; ?>"<?php selected($tt_catbox_1, $cat->cat_ID); ?>><?php echo $cat->cat_name; ?></option>
    				<?php } ?>
    </select>
  3. teenspirit
    Member
    Posted 5 years ago #

    You have done a great job. Have you solved it yourself? I'm usually using php form tutorials in such cases.

Topic Closed

This topic has been closed to new replies.

About this Topic