Skip to content

Instantly share code, notes, and snippets.

@iredun
Created August 12, 2015 08:58
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save iredun/44fd97301fc054446cd4 to your computer and use it in GitHub Desktop.
Save iredun/44fd97301fc054446cd4 to your computer and use it in GitHub Desktop.
Получить список разделов и их подразделов в 1С - Битрикс
<?
$res = CIBlockSection::GetList(
Array('name' => 'asc'),
Array('IBLOCK_ID' =>'5' , 'ACTIVE' => 'Y')
);
while ($row = $res->GetNext())
{
echo $row['NAME'].'<br>';
$rsParentSection = CIBlockSection::GetByID($row['ID']);
if ($arParentSection = $rsParentSection->GetNext())
{
$arFilter = array('IBLOCK_ID' => $arParentSection['IBLOCK_ID'],'>LEFT_MARGIN' => $arParentSection['LEFT_MARGIN'],'<RIGHT_MARGIN' => $arParentSection['RIGHT_MARGIN'],'>DEPTH_LEVEL' => $arParentSection['DEPTH_LEVEL']); // выберет потомков без учета активности
$rsSect = CIBlockSection::GetList(array('left_margin' => 'asc'),$arFilter);
while ($arSect = $rsSect->GetNext())
{
echo '---'.$arSect['NAME'].'<br>';
}
}
}
?>
@Roman-Moschenskiy
Copy link

Roman-Moschenskiy commented Dec 11, 2019

А зачем повторно получать раздел по ID, если он уже есть в первой выборке? Оптимизировал код вот так:

<?
$rsParentSection = CIBlockSection::GetList(
		Array('name' => 'asc'),
		Array('IBLOCK_ID' => 36, 'ACTIVE' => 'Y')
	);
while ($arParentSection = $rsParentSection->GetNext())
{
	echo $arParentSection['NAME'].'<br>';
	$arFilter = array('IBLOCK_ID' => $arParentSection['IBLOCK_ID'],'>LEFT_MARGIN' => $arParentSection['LEFT_MARGIN'],'<RIGHT_MARGIN' => $arParentSection['RIGHT_MARGIN'],'>DEPTH_LEVEL' => $arParentSection['DEPTH_LEVEL']); // выберет потомков без учета активности
	$rsSect = CIBlockSection::GetList(array('left_margin' => 'asc'),$arFilter);
	while ($arSect = $rsSect->GetNext())
	{
	    echo '&#8195;'.$arSect['NAME'].'<br>';
	}
}
?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment