php - get curent class in sidebar ($_GET['do'] == "")

203

In my sidebar is class added for active link with this code:

<?php echo ($_GET['do'] == "") ? "class=\"current\"": "";?>

// This is root link

All working but I have error in my error_log file....

This is in next menu item:

<?php echo ($_GET['do'] == "add_account") ? "class=\"current\"": "";?>

Also working but without error!! So I need something add into "" ?Or how can I better make root link mark in menu with php ?

Thanks for opinions!! Regards Makromat

383

Answer

Solution:

<?php echo (isset($_GET['do']) && $_GET['do'] == "add_account") ? "class='current'" : ""; ?>

Check if it exists and then check which value it holds.

358

Answer

Solution:

Accessing variables (or array indexes, or object properties) which do not exist (yet) in PHP triggers aNotice (not an error). This is the case when you access$_GET['do'], which does not exist.

In order to fix that, replace:

<?php echo ($_GET['do'] == "") ? "class=\"current\"": "";?>

With:

<?php echo !isset($_GET['do']) ? "class=\"current\"": "";?>

The same applies everywhere a variable (or array index, or object property) which might not exist. So when you're checking$_GET['do'] for equality with either"" or"add_account" you need to first check ifisset($_GET['do']).

Which means you also need to edit the second occurrence of$_GET['do'] in your code:

<?php echo ($_GET['do'] == "add_account") ? "class=\"current\"": "";?>

Becomes:

<?php echo (isset($_GET['do']) AND $_GET['do'] == "add_account") ? "class='current'" : ""; ?>

Like @Houssni said.

People are also looking for solutions to the problem: php - How to set the sql results index value by desired column name in mysql?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.