Jump to content

better way to write this code


allex01

Recommended Posts

Is there a way to minimize this code.

 

elseif($page=="index1.php"){$current3="current";}

elseif($page=="index2.php"){$current3="current";}

elseif($page=="index3.php"){$current3="current";}

elseif($page=="index4.php"){$current3="current";}

elseif($page=="index5.php"){$current3="current";}

elseif($page=="index6.php"){$current3="current";}

 

Basicly i want $current3= current if any of the pages occur in $page.

 

I tried using or operator as you see below, but that didn't work

elseif($page==("index1.php"||"index2.php"||"index3.php"||"index4.php")){$current3="current";}

 

Thanks for your help.

Link to comment
https://forums.phpfreaks.com/topic/112397-better-way-to-write-this-code/
Share on other sites

well I don't know what the bigger picture here is, but based off just that code, suppose you could do:

if((substr($page,5,1) >= 1) && (substr($page,5,1) <= 6)) {
   $current3="current";
}

 

edited to fix 2nd substr argument (forgot it started at 0)

Thanks for the responses.

 

What i'm trying to do is set current selection for my css menu

so if page is page1.php or page2.php or page3.php for exmaple i want the current menu selection to at home which has a css class definition of $current1 see below

<li class="<?php echo $current1;?>"><a. href.......>Home</a></li>

<li class="<?php echo $current2?>"><a. href.......>Audio</a></li>

..

 

The solution you provided below assuming the name formats are index1 index2 .... Not all pages have the same character count or same name format. We might have a page with the name signup_here.php. So it will not work.

f((substr($page,5,1) >= 1) && (substr($page,5,1) <= 6)) {

   $current3="current";

}

 

I want to learn of a nice way to optimize the code. I appreciate all your help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.