I have a problem to make a url form navigation active so it light up.
I have this code, but it is not working. It has been a while that I used php and it does not accept anymore if the variable is empty.
Can someone help me?
This is the code:
<?php
$current1 = ""; $current2 = "";
if (isset($_GET['mode']))
//mode not empty
{
$mode = $_GET[ 'mode'];
echo $mode."<br>";
if ($mode = "index") { $current1 = "active";$current2="";}
elseif ($mode = "contact") { $current2 = "active";$current1="";}
}
else
//mode empty
{
$current1 = "active";$current2="";
}
?>
3
Answers
Perhaps this might be a simpler way:
There are a couple of issues with your code that are preventing it from working correctly. Let’s go through them and provide the corrected code:
Comparison Operator: In your if statements, you are using the assignment operator = instead of the comparison operator == or ===. To compare values, you need to use == for loose comparison or === for strict comparison.
Multiple Conditions: When comparing multiple conditions within an if statement, you should use the logical operators && (AND) or || (OR) to combine the conditions.
Variable Assignment: The assignment of the active class is reversed in your code. You have assigned it to $current1 when the mode is "index" and to $current2 when the mode is "contact". It should be the other way around.
Here’s the corrected code:
In this updated code, we use the === operator for strict comparison. We assign the active class to $current1 when the mode is "index" and to $current2 when the mode is "contact". The conditions are now properly separated using the logical operators.
Isset determine if a variable is declared and is different than null
let’s check the official php example
So you may use empty to check if the $_GET[‘mode’] is a empty value or no, And for your answer you may code this way: