问题
On my website, I use a switch case to give a color to the navigation bar's active link.
I first declared an array $case with all the possible links in the navigation bar. Then, I check to see if it's a function clicked on one of the pages (if the query string isn't empty) and if it was put into the $case array (to keep from changing color when other links inside other pages are clicked.)
If true, $current will get a value like docs_zamo or akuut_wgakuut --> FILENAME_QUERY. I only want to change the color when I have declared it before (in the $case array) Otherwise, it will just be the filename (without '.php')
I find this piece of code rather unwieldy. Is there another way to do the job? I'm pretty new to PHP. I thought; if there's a function checking whether a case exists inside a switch, then I wouldn't need the $case array, because I would've declared the existence of the link inside the switch case already.
The code to check if it's a FILENAME_QUERY link:
$case = array("index","medica","praesidium","akuut","sponsors","docs","docs_zamo","kalender","evenementen","fotos","werking","jaarraden","akuut_wgakuut","onderwijs","sociaal","sport","werkgroepen","eerstejaars","international",);
if (($_SERVER['QUERY_STRING'] != "") && (in_array(basename($_SERVER['PHP_SELF'],'.php') ."_". str_replace('actie=','',$_SERVER['QUERY_STRING']),$case) == true)) {
$current = basename($_SERVER['PHP_SELF'],'.php') ."_". str_replace('actie=','',$_SERVER['QUERY_STRING']);
}
else {$current = basename($_SERVER['PHP_SELF'],'.php');}
The switch:
$id = array("","","","","","","","","","","","","","","","","","","");
switch($current)
{
case $case[0]:$id[0]="class = 'here'";break;
case $case[1]:$id[1]="class = 'here'";break;
case $case[2]:$id[2]="class = 'here'";break;
case $case[3]:$id[3]="class = 'here'";break;
case $case[4]:$id[4]="class = 'here'";break;
case $case[5]:$id[5]="class = 'here'";break;
case $case[6]:$id[18]="class = 'here'";break;
case $case[7]:$id[6]="class = 'here'";break;
case $case[8]:$id[7]="class = 'here'";break;
case $case[9]:$id[8]="class = 'here'";break;
case $case[10]:$id[9]="class = 'here'";break;
case $case[11]:$id[10]="class = 'here'";break;
case $case[12]:$id[11]="class = 'here'";break;
case $case[13]:$id[12]="class = 'here'";break;
case $case[14]:$id[13]="class = 'here'";break;
case $case[15]:$id[14]="class = 'here'";break;
case $case[16]:$id[15]="class = 'here'";break;
case $case[17]:$id[16]="class = 'here'";break;
case $case[18]:$id[17]="class = 'here'";break;
}
?>
The HTML:
<div id='menu'>
<ul>
<li><a href='index.php' <? echo $id[0]; ?>>home</a></li>
<li><a href='medica.php' <? echo $id[1]; ?>>medica</a>
<ul>
<li><a href='praesidium.php' <? echo $id[2]; ?>>» praesidium</a></li>
<li><a href='akuut.php' <? echo $id[3]; ?>>» akuut</a></li>
<li><a href='sponsors.php' <? echo $id[4]; ?>>» sponsors</a></li>
</ul>
</li>
<li><a href='docs.php' <? echo $id[5]; ?>>doc's bar</a>
<ul>
<li><a href='docs.php' <? echo $id[5]; ?>>» doc's</a></li>
<li><a href='docs.php?actie=zamo' <? echo $id[18]; ?>>» zamo</a></li>
</ul>
</li>
<li><a href='kalender.php' <? echo $id[6]; ?>>kalender</a>
<ul>
<li><a href='evenementen.php' <? echo $id[7]; ?>>» evenementen</a></li>
</ul>
</li>
<li><a href='fotos.php' <? echo $id[8]; ?>>foto's</a></li>
<li><a href='werking.php' <? echo $id[9]; ?>>Werking</a>
<ul>
<li><a href='jaarraden.php' <? echo $id[10]; ?>>» jaarraden</a></li>
<li><a href='akuut.php?actie=wgakuut' <? echo $id[11]; ?>>» akuut</a></li>
<li><a href='onderwijs.php' <? echo $id[12]; ?>>» onderwijs</a></li>
<li><a href='sociaal.php' <? echo $id[13]; ?>>» sociaal</a></li>
<li><a href='sport.php' <? echo $id[14]; ?>>» sport</a></li>
<li><a href='werkgroepen.php' <? echo $id[15]; ?>>» werkgroepen</a></li>
</ul>
</li>
<li><a href='eerstejaars.php' <? echo $id[16]; ?>>eerstejaars</a></li>
<li><a href='https://www.medica-csd.be' target='_blank'>cursusdienst</a></li>
<li style='border:none;'><a href='http://www.medica.be/w' target='_blank'>wikimedica</a></li>
<li><a href='international.php' <? echo $id[17]; ?>>international <img src='site/images/gb.png'></a></li>
</ul>
</div>
回答1:
This entire block...
switch($current)
{
case $case[0]:$id[0]="class = 'here'";break;
case $case[1]:$id[1]="class = 'here'";break;
case $case[2]:$id[2]="class = 'here'";break;
case $case[3]:$id[3]="class = 'here'";break;
case $case[4]:$id[4]="class = 'here'";break;
case $case[5]:$id[5]="class = 'here'";break;
case $case[6]:$id[18]="class = 'here'";break;
case $case[7]:$id[6]="class = 'here'";break;
case $case[8]:$id[7]="class = 'here'";break;
case $case[9]:$id[8]="class = 'here'";break;
case $case[10]:$id[9]="class = 'here'";break;
case $case[11]:$id[10]="class = 'here'";break;
case $case[12]:$id[11]="class = 'here'";break;
case $case[13]:$id[12]="class = 'here'";break;
case $case[14]:$id[13]="class = 'here'";break;
case $case[15]:$id[14]="class = 'here'";break;
case $case[16]:$id[15]="class = 'here'";break;
case $case[17]:$id[16]="class = 'here'";break;
case $case[18]:$id[17]="class = 'here'";break;
}
can be replaced with a loop:
for($i=0;$i<19;$i++) {
if($case[$i] == $current) {
$id[$i] = "class = 'here'";
break;
}
}
In general, if you find yourself writing nearly the same thing for multiple lines, you can probably replace it with a loop somehow (and almost certainly should).
来源:https://stackoverflow.com/questions/17861158/easier-way-than-using-a-switch-case-to-change-color