开发者

If-statement: how to pull 2nd GET variable

开发者 https://www.devze.com 2022-12-20 05:36 出处:网络
How do I get this to pull my 2nd variable? (I already have a switch setup) <body id=\"<?php if (! isset($_GET[\'page\'])) { echo \"home\"; } else { $_GET[\'page\']; echo $page; } ?>\">

How do I get this to pull my 2nd variable? (I already have a switch setup)


<body id="<?php if (! isset($_GET['page'])) { echo "home"; } else { $_GET['page']; echo $page; } ?>">

I have a switch statement that pulls the pages from

index.php?page=#####

and I have j开发者_Go百科ust added this part to my switch:

index.php?page=####&section=#####

Right now, if I am on page=photos, my code ends up being:

<body id="photos">

I need to make it so that if any link has the "sections" variable on it like this page=photos&section=cars it uses the same ID:

<body id="photos">


First of all, a HTML element can only have one id. So if you want to create a hybrid (e.g. page-section) you can do something like this:

<body id="<?php echo isset($_GET['page']) ? $_GET['page'] : "home"; echo isset($_GET['section']) ? ("-".$_GET['section']) : ''; ?>">

For more information on Ternary Operators in PHP (the ? and : I used in the echo statement) see http://php.net/manual/en/language.operators.comparison.php


I am not entirely sure I understand your question, but where you're doing:

$_GET['page']; echo $page;

What do you think is happening? You're echoing a variable that has no definition. If you want to echo the value passed in the url, just do:

echo $_GET['page'];

GET doesnt mean your getting the varible, its the method by which the variable was passed to he page. The possible methods are get (in the url) or post (not).


Wouldn't that be an if to find out it if the section was defined? i.e.

if(isset($_GET['section'])){
     //create div
} elseif(isset($_GET['page']){
    //create fallback div
}


Move the PHP code outside the body's id attribute for readability, and use else if. Make sure your code isn't vulnerable to injection by sanitizing or validating input from $_GET. For example:

<?php
    function isValidID($x) {
        return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
    }

    if (isset($_GET['section']) && isValidID($_GET['section'])) {
        $bodyID = $_GET['section'];
    } else if (isset($_GET['page']) && isValidID($_GET['page'])) {
        $bodyID = $_GET['page'];
    } else {
        $bodyID = 'home';
    }
?>
...
<body id="<?php echo $bodyID; ?>">

Alternatively,

<?php
    function isValidID($x) {
        return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
    }
    $bodyID='home';
    foreach (array('section', 'home') as $key) {
        if (isset($_GET[$key]) && isValidID($_GET[$key])) {
            $bodyID = $_GET[$key];
            break;
        }
    }
?>
...
<body id="<?php echo $bodyID; ?>">

In this case, I'd use the first, unrolled version. If you had to check more input keys, use the loop-based approach.

If you decide you want both page & section in the ID, you can try something like:

<?php
    function isValidID($x) {
        return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
    }

    if (isset($_GET['page']) && isValidID($_GET['page'])) {
        $bodyID = $_GET['page'];
    } else {
        $bodyID = 'home';
    }
    if (isset($_GET['section']) && isValidID($_GET['section'])) {
        $bodyID .= '_' . $_GET['section'];
    }
?>
...
<body id="<?php echo $bodyID; ?>">
0

精彩评论

暂无评论...
验证码 换一张
取 消