Advertisement
ngekoding

PHP Set Menu Active and Dropdown Open by Ngekoding

Jun 1st, 2020
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.15 KB | None | 0 0
  1. // I use this helper in CodeIgniter
  2. // AdminLTE 3 template
  3. // You can use for another reason
  4.  
  5. if ( ! function_exists('set_active'))
  6. {
  7.     function set_active($urls)
  8.     {
  9.         $_ci =& get_instance();
  10.        
  11.         $urls = explode(',', $urls);
  12.         foreach ($urls as $url) {
  13.             $ex_url = explode('/', $url);
  14.             $seg = 1;
  15.             $status = TRUE;
  16.             if (count($ex_url) > 1) {
  17.                 foreach ($ex_url as $part) {
  18.                     $segment = $_ci->uri->segment($seg++);
  19.                     if ($segment != $part) $status = FALSE;
  20.                 }
  21.             } else {
  22.                 $segs = '';
  23.                 while (!empty($_ci->uri->segment($seg))) {
  24.                     $segs .= $_ci->uri->segment($seg).'/';
  25.                     $seg++;
  26.                 }
  27.                 $segs = rtrim($segs, '/');
  28.                 if ($ex_url[0] != $segs) $status = FALSE;
  29.                 // var_dump('SEG: '.$segs);
  30.             }
  31.             if ($status) break;
  32.         }
  33.         if ($status) return 'active';
  34.         return '';
  35.     }
  36. }
  37.  
  38. if ( ! function_exists('set_menu_open'))
  39. {
  40.     function set_menu_open($urls, $exact = FALSE)
  41.     {
  42.         $cur_url = uri_string();
  43.         $urls = explode(',', $urls);
  44.         foreach ($urls as $url) {
  45.             if (($exact && $cur_url == $url) || (strpos($cur_url, $url) !== FALSE) && !$exact) {
  46.                 return 'menu-open';
  47.             }
  48.         }
  49.         return '';
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement