用户单击并在新页面中打开时,子菜单保持活动状态

I have problem when a visitor clicks on a submenu and the link opens in a new page, so I want to keep that submenu active on that page.

I have css class active and javascript for opening it, what I need is to make it with php to be active. This is UL with class:

This is my code. Can it be done with php or with javascript.

<ul>
<?php 
$qKategori = ("SELECT * FROM kategori WHERE kprind = 0");
$rKategori = mysqli_query($dbc, $qKategori);
if ($rKategori) {

while ($exKat = mysqli_fetch_array($rKategori, MYSQLI_ASSOC)){ 
$emrikategorise = $exKat['kemri'];
$idkategori = $exKat['kid'];
$idprind = $exKat['kprind'];
?>  

<li><a href="#"><?=$emrikategorise;?></a>

<ul>
<?php 
$qPrind = ("SELECT * FROM kategori WHERE kprind = '".$idkategori."'");              
$rPrind = mysqli_query($dbc,$qPrind);

while($prind = mysqli_fetch_array($rPrind)) {                 
?>
<li><a href="kategori.php?kid=<?=$prind['kid']?>"><?=$prind['kemri']?></a>   </li>
<?php 
}
mysqli_free_result($rPrind);    
?>
</ul>

</li>

<?php                   }       
mysqli_free_result($rKategori); 
}   

?>
</ul>

You can see menu on the left in The website is www.sitimobil.mk

You probably will need to build the array before outputting to be able to determine which menus should be active. You can also combine it with an optimization of the query to not have to do 1 query per category.

Something like:

$active = isset($_GET['kid'] ? $_GET['kid'] : -1;
$tree = array();
$list = array();
$qKategori = ("SELECT * FROM kategori ORDER BY kprind");
$rKategori = mysqli_query($dbc, $qKategori);
if ($rKategori) {
   while ($exKat = mysqli_fetch_array($rKategori, MYSQLI_ASSOC)){ 
      $id = $exKat['kid'];
      //To prevent numerical array with unused space
      $name = 'kategori'.$exKat['kid']; 
      $list[$name] = $exKat;

      //Calculate depth to see if the menu is a sub..sub..sub menu etc.
      $parent = $list[$name]['kprind'];
      if($parent == 0) {
         $list[$name]['depth'] = 0;
         $list[$name]['childCount'] = 0;
      }
      else {
         $list['kategori'.$parent]['childCount']++;
         $list[$name]['depth'] = $list['kategori'.$parent]['depth']+1; //Increment
      }

      if($id == $active) {
         $list[$name]['active'] = true;

         while($parent != 0) {
            $parentName = 'kategori'.$parent;
            $list[$parentName]['active'] = true;
            $parent = $list[$parentName]['kprind'];
         }
      }
      else 
         $list[$name]['active'] = false;
   }  
   mysqli_free_result($rPrind); 
   //Once we have that we can output the results...

   function output_menu($list, $parent = 0, $active = false)
       $activeClass = $active ? ' class="active"' : '';
       echo '<ul'.$activeClass.'>';
       foreach($list as $row){
          if($row['kprind'] != $parent) continue;
          $link = $row['kprind'] == 0 ? '#' : 'kategori.php?kid='.$row['kid'];
          echo '<li><a href="'.$link.'">'.$row['kemri'].'</a>';
          if($row['childCount'] > 0) 
           output_menu($list, $row['kprind'], $row['active']);
          echo '</li>';
       }
       echo '</ul>';
   }
   output_menu($list);
}

This is still a bit rough but should do the trick. It can probably be optimized so that we don't have to go through the list too many times but has the benefit of not having to request too many calls to the database. That should result in a lighter workload for the DB and faster output.