News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Need some basic PHP logc checked

Started by Antechinus, September 04, 2021, 01:00:18 AM

Previous topic - Next topic

Antechinus

This is for a basic 6 button top menu mod, with added drop menus that can take up to 6 links each. Basically I need to know if I can use $i and $a like this.

I want to check for $modSettings in the ranges 10 to 60 for top level buttons, and 11 to 16, 21 to 26, 31 to 36, etc, for sub menu links. Then use those checks to generate (or not) the relevant li's and anchors.

// Custom menu markup begins.
echo '
<div id="site_menu">
<ul id="site_nav" class="dropmenu">';

for ($i=1; $i<7; $i++)
{
if (!empty($modSettings['nav_url_' . $i . '0']) && !empty($modSettings['nav_title_' . $i . '0']))
{
echo '
<li id="nav_button_', $i, '" class="subsections">
<a href="', $modSettings['nav_url_' . $i . '0'],'"', (!empty($modSettings['nav_target_' . $i . '0'])) ? ' target="_blank" rel="noopener noreferrer"' : '', '>
<span class="textmenu">', $modSettings['nav_title_' . $i . '0'], '</span>
</a>
</li>';

if (!empty($modSettings['sub_url_' . $i . '0']) && !empty($modSettings['sub_title_' . $i . '0']))
{
echo '
<ul>';

// This is where it gets dodgey. :P
for ($a=1; $a<7; $a++)
{

if (!empty($modSettings['sub_url_' . $i . $a]) && !empty($modSettings['sub_title_' . $i . $a]))

{

// Is this shiznit going to work?
echo '
<li>
<a href="', $modSettings['sub_url_' . $i . $a], '"', (!empty($modSettings['sub_target_' . $i . $a])) ? ' target="_blank" rel="noopener noreferrer"' : '', '>
', $modSettings['sub_title_' . $i . $a], '
</a>
</li>';
}
}

echo '
</ul>';
}
}
}

echo'
</ul>
</div>';

// Custom menu markup ends.

Arantor

I wouldn't necessarily call them $i and $a but as long as nothing before or after is relying on variables called the same thing, it's basically fine.

Antechinus

I just copied the $i name out of an existing mod, then called the other one $a for the heck of it. I can easily change them to something more descriptive, which I agree is probably a good idea.

Good to know the basics should work anyway. PHP is not really my thing. ;)

Advertisement: