I have written a sipmle function for navigation:
function user_list()
{
$us = array("setting", "message", "listUsers");
foreach($us as $value){
$class = $_GET['user'] == $value ? $class = "userCurrent": '';
}
$user_ul = '<ul class="userlist">
<li><a href="user.php?user=setting" class='.$class.'>Profile</a></li>
<li><a href="user.php?user=message" class='.$class.'>Messages</a></li>
<li><a href="user.php?user=listUsers" class='.$class.'>User List</a></li>
</ul>';
return $user_ul;
}
With this all links have $class = “userCurrent”. How can I prevent repeating the class and assign to only one link per one url query? Or is my way is the right way?
Thanks in advance
Sorry about the short reply, I’m a little short on time.
You can compare $_SERVER[“REQUEST_URI”] to the link uri and apply the css class if they match.
Sorry about the short reply, I’m a little short on time.
You can compare $_SERVER[“REQUEST_URI”] to the link uri and apply the css class if they match.
what is the differenece?
My problem is not in comparing. Its in duplicated classes.
BTW, Thanks for reply
Sorry I didn’t really read it properly earlier.
Because you’re using $class outside of the loop it is always going to contain the last value set by the loop.
This will work
function user_list()
{
$us = array("setting", "message", "listUsers");
$class = array();
foreach($us as $value){
$class[$value] = ($_GET['user'] == $value ? 'userCurrent' : '');
}
$user_ul = '<ul class="userlist">
<li><a href="user.php?user=setting" class='.$class['settings'].'>Profile</a></li>
<li><a href="user.php?user=message" class='.$class['message'].'>Messages</a></li>
<li><a href="user.php?user=listUsers" class='.$class['listUsers'].'>User List</a></li>
</ul>';
return $user_ul;
}