-1

Trying to use a $_GET within a $_GET It's not throwing any errors but isn't working the way I want it to. It's been a while since I've done this so I don't know if I'm overlooking something or not. I'm trying to be able to do something like index.php?chatroom&id=1

$pgtitle = '';
$cractive = '';
$dactive = '';
$acactive = '';
$pgChat = '';
if(isset($_GET['chatroom'])){
    $cractive = 'active';
    if (isset($_GET['cid']) == "1") {
        $pgChat == 'Global Chatroom';
    }else if(isset($_GET['cid']) == "2"){
        $pgChat == 'AK Chatroom';
    }else if(isset($_GET['cid']) == "3"){
        $pgChat == 'AZ Chatroom';
    } else {
        echo '<meta http-equiv="refresh" content="0; URL=index.php?chatroom&cid=1">';
    }
}else{
    header('Location: index.php?dashboard');
}
3
  • isset($_GET['cid']) just returns if the key is set, not what the value is. isset($_GET['cid']) == "1" will always return true if it exists, which it does not. You're passing id in the Url, but checking for cid Commented Nov 9, 2021 at 19:47
  • Does this answer your question? PHP - Nested If Statements Issue Commented Nov 9, 2021 at 19:49
  • Test if the variable is set first and report an error if it isn't. Then test the value separately. Commented Nov 9, 2021 at 20:31

1 Answer 1

2

isset() returns a boolean not the content of the variable, and you was using == instead of = in $pgChat == 'Global Chatroom';, here is an example using your code for something like index.php?chatroom&cid=1:

if (isset($_GET['chatroom'])) {
    $cractive = 'active';

    if (isset($_GET['cid'])) {
        if ($_GET['cid'] == "1") {
            $pgChat = 'Global Chatroom';
        } elseif ($_GET['cid'] == "2") {
            $pgChat = 'AK Chatroom';
        } elseif ($_GET['cid'] == "3") {
            $pgChat = 'AZ Chatroom';
        } else {
            echo '<meta http-equiv="refresh" content="0; URL=index.php?chatroom&cid=1">';
        }
    } else {
        echo '<meta http-equiv="refresh" content="0; URL=index.php?chatroom&cid=1">';
    }
}

EXTRA: You can do the same thing in that way if you wanna.

$chats = [
    '1' => 'Global Chatroom',
    '2' => 'AK Chatroom',
    '3' => 'AZ Chatroom',
];

if (isset($_GET['chatroom'])) {
    $cractive = 'active';

    if (isset($_GET['cid']) && isset($chats[$_GET['cid']])) {
        $pgChat = $chats[$_GET['cid']];
    } else {
        echo '<meta http-equiv="refresh" content="0; URL=index.php?chatroom&cid=1">';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.