php - Easier way than using a switch case to change color -
on website, use switch case give color navigation bar's active link.
i first declared array $case
possible links in navigation bar. then, check see if it's function clicked on 1 of pages (if query string isn't empty) and if put $case
array (to keep changing color when other links inside other pages clicked.)
if true, $current
value docs_zamo
or akuut_wgakuut --> filename_query.
want change color when have declared before (in $case array) otherwise, filename (without '.php')
i find piece of code rather unwieldy. there way job? i'm pretty new php. thought; if there's function checking whether case exists inside switch, wouldn't need $case array, because would've declared existence of link inside switch case already.
the code check if it's 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>
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 replaced loop:
for($i=0;$i<19;$i++) { if($case[$i] == $current) { $id[$i] = "class = 'here'"; break; } }
in general, if find writing same thing multiple lines, can replace loop somehow (and should).
Comments
Post a Comment