Pergunta

I'm trying to pull in the parameter of a URL and use that to determine what information to display on page, but for some reason the information is being read wrong. The first thing I do is check for the parameter below and assign it to $page

<?php
if(isset($_GET["page"])) {
    $page=$_GET["page"]; 
}
?>

I then check if the $page is equal to 2 or 3. For some reason, if I echo out $page, I get the proper value of the parameter but it displays incorrect info.

<?php
if(isset($page) == '2') { ?>


DISPLAY INFO A
ECHO $PAGE RETURNS 2

<?php } elseif(isset($page) == '3') { ?>

DISPLAY INFO B
ECHO $PAGE RETURNS 3

<?php } else { something here } ?>

For some reason, even though $page returns 3, I receive INFO A that's supposed to be displayed on page 2. Am I pulling the parameter wrong? The URL Looks like this:

feed.php?page=3
Foi útil?

Solução

php isset function return Boolean. You should change code to:

<?php
if(isset($page) && $page== '2') {
?>
DISPLAY INFO A
ECHO $PAGE RETURNS 2
<?php } elseif(isset($page) && $page== '3') { ?>
DISPLAY INFO B
ECHO $PAGE RETURNS 3
<?php } else { something here } ?>

Outras dicas

This is wrong:

if(isset($page) == '2') { 

It should be

if( $page == '2') {

This will only seem to work on page 1, because isset($page) returns true, which truthy gets converted to 1. The isset() function is only to check if the variable has been set or not

if($page == '3')

Why the isset()? You already do that when you assign it. Maybe this as well:

if(isset($_GET["page"])) {
    $page = $_GET["page"]; 
} else {
    $page = '0';  // or something
}

isset() return a TRUE/FALSE, yet you're comparing it against normal integers. Boolean TRUE in mysql is equivalent to integer 1, but will fail the rest of your tests. You need to have:

if (isset($_GET['page'])) {
   if ($_GET['page'] == 1) { ... 1 stuff } 
   else if ($_GET['page'] == 2) { ... 2 stuff } 
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top