Jump to content

Default option in a drop-down list


transparencia

Recommended Posts

I want to change the default option on a drop menu list. The following code, draws the menus from the database and outputs them to the user. I would like to know if there is a possibility to change the default option that the menu selects.

 

I tried changing $selected = 1, 2 or 3 but it doesn't do anything.

 

function construct_category_list( $selected = 0 )
{
	global $kernel;

	$options = $kernel->db->item( "SELECT `datastore_value` FROM `" . TABLE_PREFIX . "datastore` WHERE `datastore_key` = 'category_dropmenu'" );

	if( is_array( $selected ) )
	{
		$kernel->vars['html']['category_list_options'] = $options;

		foreach( $selected AS $id )
		{
			$kernel->vars['html']['category_list_options'] = str_replace( "value=\"" . $id . "\"", "value=\"" . $id . "\" selected=\"selected\"", $kernel->vars['html']['category_list_options'] );
		}
	}
	else
	{
		$new_options = array();

		$options_list = explode( LINE_BREAK_CHR, $options );

		$blocked_categories = ( !empty( $kernel->session->vars['session_categories'] ) ) ? array_flip( unserialize( $kernel->session->vars['session_categories'] ) ) : array();

		foreach( $options_list AS $item )
		{
			preg_match( "/[0-9]+/", $item, $matches );

			if( !isset( $blocked_categories[ $matches[0] ] ) OR empty( $matches[0] ) ) $new_options[] = $item;
		}

		$options = implode( LINE_BREAK_CHR, $new_options );

		$kernel->vars['html']['category_list_options'] = str_replace( "value=\"" . $selected . "\"", "value=\"" . $selected . "\" selected=\"selected\"", $options );
	}
}

Link to comment
https://forums.phpfreaks.com/topic/139771-default-option-in-a-drop-down-list/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.