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
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.