skip to Main Content

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


  1. Perhaps this might be a simpler way:

    // default
    $current1 = 'active';
    $current2 = '';
    
    if (!empty($_GET['mode']) && ($_GET['mode'] == 'contact')) {
        $current1 = '';
        $current2 = 'active';
    }
    
    Login or Signup to reply.
  2. 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:

    $current1 = "";
    $current2 = "";
    
    if (isset($_GET['mode'])) {
      $mode = $_GET['mode'];
      echo $mode . "<br>";
      
      if ($mode === "index") {
        $current1 = "active";
        $current2 = "";
      } elseif ($mode === "contact") {
        $current1 = "";
        $current2 = "active";
      }
    } else {
      $current1 = "active";
      $current2 = "";
    }
    

    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.

    Login or Signup to reply.
  3. Isset determine if a variable is declared and is different than null

    let’s check the official php example

    $var = '';
    
    // This will evaluate to TRUE so the text will be printed.
    if (isset($var)) {
        echo "This var is set so I will print.";
    }
    
    

    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:

    $modes = [
         'index' => '',
         'contact' => '',
    ];
    
    $mode = (isset($_GET['mode']) and !empty($_GET['mode'])) ? $_GET['mode'] : 'index';
    $modes[$mode] = 'active';
    
    // in case you need your variable:
    $current1 = $modes['index'];
    $current2 = $modes['contact'];
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search