i'm trying to use uri segment to add class active and non-active for my navigation menu. this is my code.
<li <?php echo $uri1 == "admin" ? "class='active'" : "class=''"; ?>>
<i class="fa fa-fw fa-dashboard"></i> Dashboard
</li>
<li <?php echo $uri1 == "admin" || $uri2 == "admin/categories" ? "class='active'" : "class=''"; ?>>
<i class="fa fa-fw fa-cubes"></i> Categories
</li>
but when i tried this one, they both got active class on their list even it's not their current page. And the case is: how to set one non-active class if it's not their current page. Will appreciate any help, thanks before!
My guess is you used the || insted of &&. Try this:
<li <?php echo $uri1 == "admin" ? "class='active'" : "class=''"; ?>>
<i class="fa fa-fw fa-dashboard"></i> Dashboard
</li>
<li <?php echo $uri1 == "admin" && $uri2 == "admin/categories" ? "class='active'" : "class=''"; ?>>
<i class="fa fa-fw fa-cubes"></i> Categories
</li>
Related
What I do is to put all these nav-item (there are about five of these links) inside the navbar.php and just add <?php include(navbar.html) in all those five page so that everytime I click page 1,2,3,.4 or 5th webpage, the nav bar still can be seen. But when the "User" (non-admin user) logged in, I don't want to include the page 1 on his navbar because that page has features for admin only, which is what I'm having a problem with....
Index.php
<?php
include('includes/navbar.php');
?>
<!-- Main Content -->
<?php
include('includes/footer.php');
?>
page2.php
<?php
include('includes/navbar.php');
?>
<!-- Main Content -->
<?php
include('includes/footer.php');
?>
page3.php
<?php
include('includes/navbar.php');
?>
<!-- Main Content -->
<?php
include('includes/footer.php');
?>
page4.php
<?php
include('includes/navbar.php');
?>
<!-- Main Content -->
<?php
include('includes/footer.php');
?>
Page5.php
<?php
include('includes/navbar.php');
?>
<!-- Main Content -->
<?php
include('includes/footer.php');
?>
code.php Here is the code where for validating if the user is an admin or non-admin user
The !password_verify is for validation for password and hash password.
if(isset($_POST['login_btn']))
{
$email_login = $_POST['emaill'];
$password_login = $_POST['passwordd'];
$query = "SELECT * FROM register WHERE email='$email_login' LIMIT 1";
$query_run = mysqli_query($connection, $query);
$password = "SELECT password FROM register WHERE email='$email_login'";
$usertypes = mysqli_fetch_array($query_run);
if($usertypes['usertype'] == "admin")
{
if(!password_verify($password_login, $password))
{
$_SESSION['username'] = $usertypes['username'];
header('Location: index.php');
}
else
{
$_SESSION['status'] = "Email / Password is Invalid";
header('Location: login.php');
}
}
else if ($usertypes['usertype'] == "user")
{
if(!password_verify($password_login, $password)){
$_SESSION['username'] = $usertypes['username'];
header('Location: index2.php');
}
else
{
$_SESSION['status'] = "Email / Password is Invalid";
header('Location: login.php');
}
}
}
UPDATE: I've already found a solution to the problem... Just put all the list items tag<li></li> inside this php elseif if you want to display a specific navbar list when an Admin user is logged in but want to hide them in a non-admin one.
<?php if($_SESSION['TYPE'] == 'admin'): ?>
<li class="nav-item">
<a class="nav-link" href="PageForAdmin.php">
<i class="fas fa-user" style="font-size: 20px;"></i>
<span>Page For Admin</span></a>
</li>
<?php endif; ?>
Use if else loop
For Laravel
#if(Auth::user()->user_role == "role")
// Show page 1
#else
// show page 2
#endif
For you
<?php
if ($user->role == 'admin'){
<li class="nav-item">
<a class="nav-link" href="page1.php">
<i class="fas fa-fw fa-tachometer-alt"></i>
<span>Page1</span></a>
</li>
}else {
<li class="nav-item">
<a class="nav-link" href="page.php">
<i class="fas fa-fw fa-tachometer-alt"></i>
<span>Page2</span></a>
</li>
}
?>
May be code not compiled
<?php
if ($user->role == "admin"){
If You use number then
<?php
if ($user->role == 1){
Note: This is a example you can use it. I not tested it. Use how you want to use role or usertype at the if condition
<?php
if ($usertype->username == "admin){
Example For 5 Page
<?php
if ($user->role == 'admin'){
<li class="nav-item">
<a class="nav-link" href="page1.php">
<i class="fas fa-fw fa-tachometer-alt"></i>
<span>Page1</span></a>
</li>
}else if ($user->role == 'author'){
<li class="nav-item">
<a class="nav-link" href="page.php">
<i class="fas fa-fw fa-tachometer-alt"></i>
<span>Page2</span></a>
</li>
}else if ($user->role == 'editor'){
<li class="nav-item">
<a class="nav-link" href="page.php">
<i class="fas fa-fw fa-tachometer-alt"></i>
<span>Page3</span></a>
</li>
}else if ($user->role == 'superadmin'){
<li class="nav-item">
<a class="nav-link" href="page.php">
<i class="fas fa-fw fa-tachometer-alt"></i>
<span>Page4</span></a>
</li>
}else if ($user->role == 'superadmin2'){
<li class="nav-item">
<a class="nav-link" href="page.php">
<i class="fas fa-fw fa-tachometer-alt"></i>
<span>Page5</span></a>
</li>
}
?>
You can use nested if else or elseif too Like you can use next if in firstone's else like see below code
<?php
if ($user->role == 'admin'){
// your query
}else
if ($user->role == 'author'){
// your query
}else
if ($user->role == 'admin2'){
// your query
}else
if($user->role == 'admin1'){
// your query
}
}
?>
I have a header with a menu with dropdowns also a mega dropdown. I merged so far to make the parent highlighted but when a child is clicked than it doesn't work. I have searched and tried everything I could find, but no luck sofar. I have this: in the top of the header and on every dropdown link: ", that works perfectly for the parent but again the child of the parent won't make the parent to be active. here is a example of the dropdown I have:
<li class="dropdown">
<a class="dropdown-item dropdown-toggle <?= ($activePage == 'Link1') ? 'active':''; ?>" href="#">Link1</a>
<ul class="dropdown-menu">
<li class="dropdown-submenu">
<a class="dropdown-item" <?= ($activePage == 'Link2') ? 'active':''; ?>" href="#">Link2</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item <?= ($activePage == 'Link3') ? 'active':''; ?>" href="#">Link3</a></li>
<li class="dropdown-submenu">
<a class="dropdown-item <?= ($activePage == 'Link4') ? 'active':''; ?>" href="#">Link4</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item <?= ($activePage == 'Link5') ? 'active':''; ?>" href="#">Link5</a></li>
</ul>
</li>
</ul>
</li>
<li class="dropdown-submenu">
<a class="dropdown-item" <?= ($activePage == 'Link6') ? 'active':''; ?>" href="#">Link6</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item <?= ($activePage == 'Link7') ? 'active':''; ?>" href="#">Link7</a></li>
<li class="dropdown-submenu">
<a class="dropdown-item <?= ($activePage == 'Link8') ? 'active':''; ?>" href="#">Link8</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item <?= ($activePage == 'Link9') ? 'active':''; ?>" href="#">Link9</a></li>
</ul>
</li>
</ul>
</li>
<li class="dropdown-submenu">
<a class="dropdown-item <?= ($activePage == 'Link10') ? 'active':''; ?>" href="#">Link10</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item <?= ($activePage == 'Link11') ? 'active':''; ?>" href="#">Link11</a></li>
<li class="dropdown-submenu">
<a class="dropdown-item <?= ($activePage == 'Link12') ? 'active':''; ?>" href="#">Link12</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item <?= ($activePage == 'Link13') ? 'active':''; ?>" href="#">Link13</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
How can I make it work with php or can't that not be done at all????
looks like bootstrap. Here an example of javascript
//APP/assets/js/app.js
import '../css/nav.scss';
// Need jQuery? Install it with "yarn add jquery", then uncomment to require it.
const $ = require('jquery');
import 'bootstrap'; //add functions to jQuery
//uncomment to support legacy code
global.$ = $;
//navigation active class settings
$(document).ready(function () {
$(function () {
var current_page_URL = location.href;
$("a").each(function () {
if ($(this).attr("href") !== "#") {
var target_URL = $(this).prop("href");
if (target_URL == current_page_URL) {
$('nav a').parents('li, ul').removeClass('active');
$(this).parent('li').addClass('active');
return false;
}
}
});
});
$(function () {
$('[data-toggle="tooltip"]').tooltip()
});
});
//APP/templates/base.html.twig
...
{% block javascripts %}
{{ encore_entry_script_tags('app') }}
{% endblock %}
My HTML code is
<div id="sidebar"><i class="icon icon-home"></i>Dashboard
<ul>
<li class="active"><i class="icon icon-home"></i> <span>Scam Type</span> </li>
<li> <i class="icon icon-signal"></i> <span>Scam Database</span> </li>
<li> <i class="icon icon-inbox"></i> <span>Scam Story</span> </li>
<li><i class="icon icon-th"></i> <span>Keyword</span></li>
<li><i class="icon icon-th"></i> <span>Category</span></li>
<li><i class="icon icon-th"></i> <span>Sub Category</span></li>
</ul>
</div>
Here i gave li class active as like in bootstrap and its not working, but i don't know how to give in laravel and i am very beginner of laravel, so please avoid minus votes and give me the right solution for it.. How Should i change my code to get li class active dynamically?
You can use ternary operator. For example, you can check URI for the current route:
<li{{ request()->is('scam-types') ? ' class="active"' : '' }}>
You can also use * as wildcard:
<li{{ request()->is('scam-type-number-*') ? ' class="active"' : '' }}>
Or you can check route name:
<li{{ request()->route()->getName() === 'ScamType.index' ? ' class="active"' : '' }}>
Try Below Code to write each li tag for check some text in URL
if (strpos($_SERVER['REQUEST_URI'], "ScamType") !== false){
echo "active";
}
Write above code in class.
Ex.
<li class="{{ if (strpos($_SERVER['REQUEST_URI'], "ScamType") !== false){ echo "active"; } }}"><i class="icon icon-home"></i> <span>Scam Type</span> </li>
From above answer I have try and doesnt work because class="active" must be class=active
<li><a class="{{ request()->routeIs('about.index*') ? 'active-menu' : '' }}" href="{{route('about.index')}}"><i class="fa fa-desktop"></i> About</a></li>
routeIs('Route_Name*') ? 'active-menu' : '' }}">
In Route Name just write Route name defined in Routes
i'm having some problems with my html, php code. With this code, my nav will show the active li but the other pages which are not active won't have style or links.
<!--/. PHP "active" -->
<?php
$current_url = basename($_SERVER['PHP_SELF']);
$active = "class=\"active-menu\"";
?>
<!--/. PHP "active" -->
<nav class="navbar-default navbar-side" role="navigation">
<div class="sidebar-collapse">
<ul class="nav" id="main-menu">
<li>
<?php if ($current_url == "home.php") { ?>
<a <?php echo $active;?> href="home.php"><?php } ?><i class="fa fa-dashboard"></i> Dashboard</a>
</li>
<li>
<?php if ($current_url == "new.php") { ?>
<a <?php echo $active;?> href="new.php"><?php } ?><i class="fa fa-qrcode"></i> Nieuw item toevoegen</a>
</li>
<li>
<?php if ($current_url == "show.php") { ?>
<a <?php echo $active;?> href="show.php"><?php } ?><i class="fa fa-qrcode"></i> Bekijk inventaris</a>
</li>
<li>
<?php if ($current_url == "profile.php") { ?>
<a <?php echo $active;?> href="profile.php"><?php } ?><i class="fa fa-user"></i> Gebruikersprofiel</a>
</li>
<li>
<i class="fa fa-sitemap"></i> Gebruikers<span class="fa arrow"></span>
<ul class="nav nav-second-level">
<li>
Beheerders
</li>
<li>
ICT - Verantwoordelijken
</li>
<li>
Raadplegers
</li>
</ul>
</li>
<li>
<?php if ($current_url == "empty.php") { ?>
<a <?php echo $active;?> href="empty.php"><?php } ?><i class="fa fa-fw fa-file"></i> Lege pagina</a>
</li>
</ul>
</div>
</nav>
the pages with no styling will also not have a link. The active-menu class has the styling.
Your if condition it is wrong.
Try something like this in every <li>.
<li>
<a <?php echo $current_url == "home.php" ? $active : ''; ?> href="home.php"><i class="fa fa-dashboard"></i> Dashboard</a>
</li>
<li>
<a href="new.php" <?php if ($current_url == "new.php") {echo $active;}?>>
<i class="fa fa-qrcode"></i> Nieuw item toevoegen
</a>
</li>
Put your list like this
You should make another class, like $not_active = "class=\"not-active-menu\"";
And add that to your code
<li>
<a href="something.php" <?php if ($current_url == "something.php") {echo $active;} else { echo $not_active; }?>>
<i class="fa fa-qrcode"></i> something
</a>
</li>
I have the following side nav in side-nav.php
<?php
if (isset($_SESSION['account'])) {
$current_user = get_user_on_uin($_SESSION['account']['ein']);
$current_user = $current_user->fetch_assoc();
}
//show sidebar on everypage except login & graph
$page_name = basename(__FILE__);
//var_dump($page_name);
//die();
if ($page_name != 'login.php' || $page_name != 'graph.php'){
echo '<div class="navbar-default navbar-static-side" role="navigation">
<div class="sidebar-collapse">
<ul class="nav" id="side-menu">
<li>
<li>
<i class="fa fa-file-text"></i> Request a Coach
</li>
<li>
<i class="fa fa-database"></i> View All Requests
</li>';
if (isset($current_user) && $current_user['user_role']!='reactive_coach' || $current_user['user_role']!='proactive_coach'){
echo '<li>
<i class="fa fa-user fa-fw"></i> View Accepted Requests
</li>
<li><i class="fa fa-exclamation-triangle"></i> View Rejected Requests
</li>
<li><i class="fa fa-binoculars"></i> View Coach Assigned Requests
</li>';
}
echo '<li><i class="fa fa-user fa-fw"></i> View Completed Requests
</li>
<li class="divider"></li>';
if (isset($current_user) && $current_user['user_role']=='reactive_coaching_manager' || $current_user['user_role']=='proactive_coaching_manager' || $current_user['user_role']=='OEM' || $current_user['user_role']=='GM'){
echo '<li><i class="fa fa-area-chart"></i> Analytics
</li>';
}
echo '<li><i class="fa fa-sign-out fa-fw"></i> Logout
</li>';
echo '</ul>
</div>
</div>';
}
?>
I do not wish to show this side-nav.php to appear on login.php or graph.php pages but it still does...
I tried by successfully getting the current page by:
$page_name = basename(__FILE__);
But the if ($page_name != 'login.php' || $page_name != 'graph.php') seems to be true all the time...
is there a different way of doing this?
Thanks
You're using the wrong operator, it is contradicting itself.
You need to use the && operator instead of ||
if ($page_name != 'login.php' && $page_name != 'graph.php')