Title: PHP Form: Select Categories
Last modified: August 19, 2016

---

# PHP Form: Select Categories

 *  Resolved [Greg Rickaby](https://wordpress.org/support/users/gregrickaby/)
 * (@gregrickaby)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/php-form-select-categories/)
 * 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?

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [Greg Rickaby](https://wordpress.org/support/users/gregrickaby/)
 * (@gregrickaby)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/php-form-select-categories/#post-1299903)
 * 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>
       ```
   
 *  [teenspirit](https://wordpress.org/support/users/teenspirit/)
 * (@teenspirit)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/php-form-select-categories/#post-1300178)
 * You have done a great job. Have you solved it yourself? I’m usually using [php form tutorials](http://phpforms.net/tutorial/tutorial.html)
   in such cases.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘PHP Form: Select Categories’ is closed to new replies.

## Tags

 * [form](https://wordpress.org/support/topic-tag/form/)
 * [options panel](https://wordpress.org/support/topic-tag/options-panel/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [select](https://wordpress.org/support/topic-tag/select/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [teenspirit](https://wordpress.org/support/users/teenspirit/)
 * Last activity: [16 years, 1 month ago](https://wordpress.org/support/topic/php-form-select-categories/#post-1300178)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
