How to set an ACTIVE CSS Class using PHP based on the page you are viewing? -


Below is a very basic example of a very unaffected way of setting "active" CSS class on a menu item on which page you are viewing with PHP ...

  if ($ this-> currentPageName) === 'lead_records') {$ leadRecordsActiveMenu =' nav -tab active '; $ FollowUpLeadsActiveMenu = ''; $ MassAddLeadRecordsActiveMenu = ''; $ LeadRecordSettingsActiveMenu = ''; } If ($ this-> current page name) === 'follow_up_lead_records' {'Navi-tab-active'; $ Leadershaks ActiveMenu = ''; $ Follow-up updated activate menu = 'nav-tab-active'; $ MassAddLeadRecordsActiveMenu = ''; $ LeadRecordSettingsActiveMenu = ''; } If ($ this- & gt; current page name) === 'mass_add_lead_records') {$ leadRecordsActiveMenu = ''; $ FollowUpLeadsActiveMenu = ''; $ MassAddLeadRecordsActiveMenu = 'Navi-tab-active'; $ LeadRecordSettingsActiveMenu = ''; } If ($ this-> current page name) === 'lead_record_settings') {$ leadRecordsActiveMenu = ''; $ FollowUpLeadsActiveMenu = ''; $ MassAddLeadRecordsActiveMenu = ''; $ LeadRecordSettingsActiveMenu = 'Navi-tab-active'; } Echo & lt; H2 class = "nav-tab-wrapper" & gt; & Lt; A class = "nav-tab". $ LeadRecordsActiveMenu '' Href = "/ admin.php? Page = lead_records" & gt; Leeds & lt; / A & gt; & Lt; A class = "avi-tab". $ Follow Ups Expeditions Activation '' Href = "/ admin.php? Page = Follow-us-Live_ Records" & gt; Leads up & lt; / A & gt; & Lt; A class = "avi-tab". $ MassAddLeadRecordsActiveMenu '' Href = "/ admin.php? Page = mass_add_lead_records" & gt; Month Lead & lt; / A & gt; & Lt; A class = "new-tab". $ LeadRecordSettingsActiveMenu '"Href =" / admin.php? Page = Lead_Record_Settings "> Lead Settings & lt; / a & gt; & lt; / h2 & gt; ';  

So this is a dummy project that just for me Is also not present, but it is one of those things that comes out in many projects. Now that I am working with something like WordPress or Laverwell, then there are generally more elegant solutions available for all types. There is more to those smaller projects that are free of any kind There is only PHP to use the work or library.

The idea is that there will be a menu in many projects where you have to set a simple CSS class to show this item in the menu Active < / Strong>,

then based on this simple 4 menu item menu, you can see the CSS class nav-tab-active HTML that you generate based on that page Output in. This is the source of the page in PHP variable $ this-> currentPageName

Will you like your help and ideas on a more elegant solution for future projects on Mango? Imagine 50 menu items as you can see that this is a lot of sloping codes for the 4 item menu ...

I simplify this code like this:

  $ pages = array ('lead_records', 'follow_up_lead_records', 'mass_add_lead_records', 'lead_record_settings'); Echo & lt; H2 class = "nav-tab-wrapper" & gt; '; Forex Currency ($ Page $ Page) {Echo '& lt; A class = "avi-tab" ($ -This-> current page name == $ page? 'Nav-tab-active': ''). '' Href = "/admin.php?page= ' $ Page. '' & Gt; Leads & lt; / a & gt; ';} Echo' & lt; / h2 & gt; ';  

Sure nav-tab-active Can be changed to active or both sections are activated by the NAV tab active. Anyway, if you want to make it even more dynamic, And whenever you need it ...

  class PageHtmlHelper {... public static function DrawS ImpleMenu ($ simple menu, $ active page = '') {$ str = '& lt; h2 square = "newly-tabbed-wrapper" & gt;' foreign currency ($ ordinary menu $ as page page) {$ Str. = '& Lt; a class = "new-tab". ($ Active page == $ page? 'Nav-tab-active': ''). '' Href = /admin.php?page='.$page.'">Leads</a> ''} $ Str. = '& Lt; / h2 & gt;'; return $ str;}} < / Code> 

and call it:

  echo pageHuffellHiller :: DrawSimal menu ($ pages, $ this-> currentPageName);  

Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -