CMS Eleanor - Поиск
Полная версия этой страницы: Официальный форум Eleanor CMS » Добавить в меню SELECT (optgroup) (функция)
Официальный форум Eleanor CMS » Для вебмастеров и владельцев сайтов » Комната программистов
Здравствуйте, подскажите пожалуйста как сделать функцию так чтобы в меню добалялось optgroup меню
функция из DLE:

$cat_info = array(
    1 => array( 'id' => 1, 'parentid' => 0, 'name' => 'test one' ),
    2 => array( 'id' => 2, 'parentid' => 1, 'name' => 'test1' ),
    3 => array( 'id' => 3, 'parentid' => 1, 'name' => 'test2' ),
    4 => array( 'id' => 4, 'parentid' => 0, 'name' => 'test two' ),
    5 => array( 'id' => 5, 'parentid' => 4, 'name' => 'test3' ),
    6 => array( 'id' => 6, 'parentid' => 4, 'name' => 'test4' ),
);

function CategoryNewsSelection($categoryid = 0, $parentid = 0, $nocat = TRUE, $sublevelmarker = '', $returnstring = '') {
	global $cat_info, $user_group, $member_id, $dle_module;

	if ($dle_module == 'addnews') $allow_list = explode( ',', $user_group[$member_id['user_group']]['cat_allow_addnews'] );
	else $allow_list = explode( ',', $user_group[$member_id['user_group']]['allow_cats'] );

	$spec_list = explode( ',', $user_group[$member_id['user_group']]['cat_add'] );

	$root_category = array ();
	
	if( $parentid == 0 ) {
		if( $nocat ) $returnstring .= '<option value="0"></option>';
	} else {
		$sublevelmarker .= '&nbsp;&nbsp;&nbsp;';
	}
	
	if( count( $cat_info ) ) {
		
		foreach ( $cat_info as $cats ) {
			if( $cats['parentid'] == $parentid ) $root_category[] = $cats['id'];
		}
		
		if( count( $root_category ) ) {
			foreach ( $root_category as $id ) {
				
				if( $allow_list[0] == "all" OR in_array( $id, $allow_list ) ) {
					
					if( $spec_list[0] == "all" or in_array( $id, $spec_list ) ) $color = "black";
					else $color = "red";
					
					$returnstring .= "<option style=\"color: {$color}\" value=\"" . $id . '" ';
					
					if( is_array( $categoryid ) ) {
						foreach ( $categoryid as $element ) {
							if( $element == $id ) $returnstring .= 'SELECTED';
						}
					} elseif( $categoryid == $id ) $returnstring .= 'SELECTED';
					
					$returnstring .= '>' . $sublevelmarker . $cat_info[$id]['name'] . '</option>';
				}
				$returnstring = CategoryNewsSelection( $categoryid, $id, $nocat, $sublevelmarker, $returnstring );
			}
		}
	}
	return $returnstring;
}


Сам optgroup это то что жирным написано:


Не могу добиться именно такого результата:

<optgroup label="test one">
     <option value="2">&nbsp;&nbsp;&nbsp;test1</option>
     <option value="3">&nbsp;&nbsp;&nbsp;test2</option>
</optgroup>
<optgroup label="test two">
     <option value="5">&nbsp;&nbsp;&nbsp;test3</option>
     <option value="6">&nbsp;&nbsp;&nbsp;test4</option>
</optgroup>


optgroup label=test one и test two - Это parent_id = 0

Спасибо заранее за помощь!
Alexander
Как-то так. Если заменить:
	if( $parentid == 0 ) {
		if( $nocat ) $returnstring .= '<option value="0"></option>';
	} else {
		$sublevelmarker .= '&nbsp;&nbsp;&nbsp;';
	}
	
	if( count( $cat_info ) ) {
		
		foreach ( $cat_info as $cats ) {
			if( $cats['parentid'] == $parentid ) $root_category[] = $cats['id'];
		}
на
$group=!$sublevelmarker;
	if( $parentid == 0 ) {
		if( $nocat ) $returnstring .= '<option value="0"></option>';
	} else {
		$sublevelmarker .= '&nbsp;&nbsp;&nbsp;';
	}
	
	if( count( $cat_info ) ) {
		
		if($group)
		{
			$found=false;
			foreach( $cat_info as $cats )
				if( $cats['id'] == $parentid ){ $returnstring. = '<optgroup label="'.$cats['name'].'">'; $found=true; break; }
		}
		else
			foreach ( $cat_info as $cats ) {
				if( $cats['parentid'] == $parentid ) $root_category[] = $cats['id'];
			}


а после
				$returnstring = CategoryNewsSelection( $categoryid, $id, $nocat, $sublevelmarker, $returnstring );
			}

дописать:
			if($group and $found)	$returnstring.='</optgroup>';
Благодарю за отклик, все равно как-то не корректно работает.

выводит почему-то криво опции, и не закрывает </optgroup>

не получается добиться такого вывода:
Alexander
lexus999, я вам лишь направление показал. Постарайтесь самостоятельно глюки исправьте :)
Для просмотра полной версии этой страницы, пожалуйста, пройдите по ссылке.