I did my homework, and the code works and does what I want.
The purpose is the following :
I have a menu with 6 options. When I click one option this option stays illuminated, then the rest is deactivated and that must happen successively with one and each of the options when clicked. I've come to the following code to control my application top menu and as I said it works and does what I want flawlessly. But my concern is the following. Since I'm not that skilled with php programming and programming at all, still learning, I want to know if this is the correct way to do this or is this the worst? also Can this code be optimized? I've tried to use switch but failed miserably T_T .
thanks in advance ;)
cheers JotaDG
---------------code---------------------
<ul class="floating left">
<?php
if ($Section=='1'){?>
<li><a class="on" href="<?php echo $StartUpUrl;?>">Start</a></li>
<li><a href="<?php echo $urlListFAQ;?>">FAQ</a></li>
<li><a href="<?php echo $urlHelp;?>">Help</a></li>
<li><a href="<?php echo $urlDocs;?>">Docs</a></li>
<li><a href="<?php echo $urlNews;?>">News</a></li>
<li><a href="<?php echo $urlGuide;?>">Guide</a></li>
<?php }
elseif ($Section== '2'){?>
<li><a href="<?php echo $StartUpUrl;?>">Start</a></li>
<li><a class="on" href="<?php echo $urlListFAQ;?>">FAQ</a></li>
<li><a href="<?php echo $urlHelp;?>">Help</a></li>
<li><a href="<?php echo $urlDocs;?>">Docs</a></li>
<li><a href="<?php echo $urlNews;?>">News</a></li>
<li><a href="<?php echo $urlGuide;?>">Guide</a></li>
<?php }
elseif ($Section== '3'){?>
<li><a href="<?php echo $StartUpUrl;?>">Start</a></li>
<li><a href="<?php echo $urlListFAQ;?>">FAQ</a></li>
<li><a class="on" href="<?php echo $urlHelp;?>">Help</a></li>
<li><a href="<?php echo $urlDocs;?>">Docs</a></li>
<li><a href="<?php echo $urlNews;?>">News</a></li>
<li><a href="<?php echo $urlGuide;?>">Guide</a></li>
<?php }
elseif ($Section== '4'){?>
<li><a href="<?php echo $StartUpUrl;?>">Start</a></li>
<li><a href="<?php echo $urlListFAQ;?>">FAQ</a></li>
<li><a href="<?php echo $urlHelp;?>">Help</a></li>
<li><a class="on" href="<?php echo $urlDocs;?>">Docs</a></li>
<li><a href="<?php echo $urlNews;?>">News</a></li>
<li><a href="<?php echo $urlGuide;?>">Guide</a></li>
<?php }
elseif ($Section== '5'){?>
<li><a href="<?php echo $StartUpUrl;?>">Start</a></li>
<li><a href="<?php echo $urlListFAQ;?>">FAQ</a></li>
<li><a href="<?php echo $urlHelp;?>">Help</a></li>
<li><a href="<?php echo $urlDocs;?>">Docs</a></li>
<li><a class="on" href="<?php echo $urlNews;?>">News</a></li>
<li><a href="<?php echo $urlGuide;?>">Guide</a></li>
<?php }
elseif ($Section== '6'){?>
<li><a href="<?php echo $StartUpUrl;?>">Start</a></li>
<开发者_JAVA百科;li><a href="<?php echo $urlListFAQ;?>">FAQ</a></li>
<li><a href="<?php echo $urlHelp;?>">Help</a></li>
<li><a href="<?php echo $urlDocs;?>">Docs</a></li>
<li><a href="<?php echo $urlNews;?>">News</a></li>
<li><a class="on" href="<?php echo $urlGuide;?>">Guide</a></li>
<?php }?>
</ul>
I would like to do in this way
<?php
//define an array of itmes I just taken three define all items
$menu=array(
1=> array('Start'=>'http://someurl'),
2=> array('FAQ'=>'http://faqurl'),
3=>array('Help'=>'http://helpurl')
);
foreach($menu as $key => $menuArray)
{
foreach ($menuArray as $title => $url)
{
?>
<li><a <?php if ($Section==$key ) { echo 'class="on"';}?> href="<?php echo $url;?>"><?php echo $title; ?></a></li>
<?php
}
}
?>
If later any url or any menu added or update you do not have to look up for whole logic again just go and add those updates into $menu
array.
Probably a more succicient way to do this but here's a start:
<ul class="floating left">
<li><a <?php echo ($section == 1) ? 'class="on"' : '';?> href="<?php echo $StartUpUrl;?>">Start</a></li>
<li><a <?php echo ($section == 2) ? 'class="on"' : '';? href="<?php echo $urlListFAQ;?>">FAQ</a></li>
<li><a <?php echo ($section == 3) ? 'class="on"' : '';?> href="<?php echo $urlHelp;?>">Help</a></li>
<li><a <?php echo ($section == 4) ? 'class="on"' : '';?> href="<?php echo $urlDocs;?>">Docs</a></li>
<li><a <?php echo ($section == 5) ? 'class="on"' : '';?> href="<?php echo $urlNews;?>">News</a></li>
<li><a <?php echo ($section == 6) ? 'class="on"' : '';?> href="<?php echo $urlGuide;?>">Guide</a></li>
</ul>
精彩评论