As pointed out in the comments, you are including bottomBar.php
before defining the variable $page
. So, when bottomBar.php
is being rendered, $page
is still undefined!
Therefore, simply define $page
before including bottomBar.php
:
$page = 'index';
include("includes/bottomBar.php");
Solution: defining a public variable before including the bottomBar
the $page
variable is defined after it’s used
session_start();
$page = 'index'; // definition
include("includes/header.php");
include("includes/bottomBar.php"); // Using
include("includes/classes/User.php");
include("includes/classes/Post.php");
I want my navbar icons change their colors when the user is on a certain page (like on instagram etc.) This is what I tried – bottomBar.php:
if ($page == 'friends') {
$theme = 'link_friendsWall_active';
$theme2 = 'link_index';
$theme3 = 'link_trophypage';
$theme4 = 'link_selfpage';
} else if ($page == 'index') {
$theme = 'link_friendsWall';
$theme2 = 'link_index_active';
$theme3 = 'link_trophypage';
$theme4 = 'link_selfpage';
} else if ($page == 'trophy') {
$theme = 'link_friendsWall';
$theme2 = 'link_index';
$theme3 = 'link_trophypage_active';
$theme4 = 'link_selfpage';
} else if ($page == 'profile') {
$theme = 'link_friendsWall';
$theme2 = 'link_index';
$theme3 = 'link_trophypage';
$theme4 = 'link_selfpage_active';
}
$page refers to the variables defined in files index.php (as index), friendsWall.php (as friends) etc. and the bottom_menu is included in them. The variables defined by the themes are classes that are styled in css. Then in my bottom_menu I’m calling those variables:
<div class="bottom_menu">
<li><a class="<?=$theme?>" href="includes/handlers/friends_link.php">
<i class="fas fa-user-friends"></i>
</a></li>
<li><a class="<?=$theme2?>" href="index.php">
<i class="fas fa-users "></i>
</a></li>
<li><a href="#">
<i class="fas fa-plus-circle"></i>
</a></li>
<li><a class="<?=$theme3?>" href="trophyPage.php">
<i class="fas fa-trophy"></i>
</a></li>
<li><a class="<?=$theme4?>" href="myProfile.php">
<img class='img_bottom_bar' src="<?php echo $user['profile_pic']; ?>" width="30px" height="30px" border-radius="20px">
</a></li>
</div>
My problem is that the website doesn’t read the variable $page from other pages.
index.php:
session_start();
include("includes/header.php");
include("includes/bottomBar.php");
include("includes/classes/User.php");
include("includes/classes/Post.php");
$page = 'index';
Oh I haven’t thought about that. Unfortunately it shows me an error undefined variable $page