Skip to content

Instantly share code, notes, and snippets.

@Van-peterson
Created June 12, 2013 12:24
Show Gist options
  • Save Van-peterson/5764799 to your computer and use it in GitHub Desktop.
Save Van-peterson/5764799 to your computer and use it in GitHub Desktop.
Menu given class
<ul class="menu">
<?php
// get a string with all li of the next html
$menu = "<li><a href="."'".$_path.$_SESSION['lang']."'".">".$home."</a></li>";
$sql = mysqli_query($connexion, "SELECT * FROM menu WHERE etat=1 ORDER BY position ASC");
$rel = 0;
if(mysqli_num_rows($sql)>0):
while($row = mysqli_fetch_object($sql)){
$id = $row->id;
$post = $row->post;
$plugins = $row->plugins;
$plug = "SELECT alias, alias_en FROM plugins where id = '".$plugins."'";
if($plugins!=0):
$plug = mysqli_query($connexion, $plug);
$row_plug = mysqli_fetch_object($plug);
$alias_plug = $row_plug->$alias_lang;
mysqli_free_result($plug);
else:
$alias_plug = "";
endif;
if($post!=0):
$sql_p ="SELECT * FROM page where id = '".$post."'";
$sql_p = mysqli_query($connexion,$sql_p);
$row_p = mysqli_fetch_object($sql_p);
$alias_post = $row_p->$alias_lang;
$id_post = $row_p->id;
$alias_post=preg_replace("# #","-",$alias_post);
mysqli_free_result($sql_p);
else:
$alias_post = "";
endif;
if(isset($alias_post) && ($alias_post!="")):
$href = $_path.$row->id."-".$alias_post;
elseif(isset($alias_plug) && ($alias_plug!="")):
$href = $_path.$alias_plug;
else:
$href = "page-undefined";
endif;
$menu .= "<li><a href="."'".$href."'".">".$row->$titre_lang."</a></li>";
$rel++;
}
mysqli_free_result($sql);
endif;
$menu .= "<li class='last'><a href='".$_path."contact'>Contact</a></li>";
$lines = preg_split("#<li#", $menu);
$lines = array_filter($lines);
foreach ($lines as &$value)
$value = '<li'.$value;
// get the url of the browser
$url=$_SERVER["REQUEST_URI"];
// remove the domain
$path = parse_url($url, PHP_URL_PATH);
// fragment the url with the /
$pathFragments = explode('/', $path);
// get last fragment of url
$end = end($pathFragments);
// loop inside the array of each menu line
foreach ($lines as $line) {
$current = false;
// get the value of the url from the line, put the value in variable match
$url = preg_match('/<a href=\'(.+)\'>/', $line, $match);
// parse the value url, put the value in array
$info = parse_url($match[1]);
$link = explode('/', $info['path']);
$link = end($link);
if (in_array($link, $pathFragments)) {
// if ($end == $info['path']) {
// if end url equal href menu, add the class current to the line
$line = str_replace('<a h', '<a class="current" h', $line);
}
// write the line
echo $line."\n";
}
?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment