Last active
November 17, 2016 17:44
-
-
Save k1sul1/8f1e906cf74d4df25c1899c67fe8dc25 to your computer and use it in GitHub Desktop.
Recursive PHP function that eats through links and creates a box with them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function box_linklist($links = NULL) { | |
if (is_null($links)) { | |
// Just a set of defaults for development. | |
$links = array( | |
array( | |
"text" => "Sample", | |
"children" => array( | |
array( | |
"text" => "1. Subitem", | |
"children" => array( | |
array( | |
"text" => "2. Subitem", | |
"attributes" => array( | |
"target" => "_blank", | |
"href" => "#" | |
) | |
) | |
), | |
"attributes" => array( | |
"target" => "_blank", | |
"href" => "#" | |
) | |
) | |
), | |
"attributes" => array( | |
"target" => "_blank", | |
"href" => "#" | |
) | |
) | |
); | |
} | |
function recursiveChildren($links) { | |
foreach ($links as $link): ?> | |
<li class="<?php echo !empty($link["children"]) ? 'parent' : 'child' ?>"> | |
<i class="fa fa-li <?php echo !empty($link["children"]) ? 'fa-plus' : 'fa-chevron-right'; ?>"></i> | |
<a <?php foreach ($link['attributes'] as $key => $value) { echo "$key='$value'" ; } ?>> | |
<?php echo $link['text']; ?> | |
</a> | |
<?php | |
if (!empty($link['children'])) { | |
echo "<ul class='submenu'>"; | |
recursiveChildren($link['children']); | |
echo "</ul>"; | |
} | |
?> | |
</li> | |
<?php endforeach; | |
} | |
?> | |
<div class="box box-linklist collapsible"> | |
<div class="box-content bg--white"> | |
<ul class="fa-ul"> | |
<?php recursiveChildren($links); ?> | |
</ul> | |
</div> | |
</div> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment