BobGCA2
BobGCA2

Reputation: 125

Display content only if URL contains specific parameter

I would like to display content only if the url contains certain parameter. In the case is checked whether the URL exists, for example, the parameter ?offset=10. If the parameter exists (site.com/post-name?offset=10) is shown the content X, otherwise the content is shown Y.

Eg:

function parameterUrl() {
$str = "?offset=10";
$uri = $_SERVER['REQUEST_URI']; 
if ($uri == "http://site.com/post-name$str") {
echo "Show this";
}
 else {
}
} 

But the above function is not working. Can anyone help. Any idea is welcome. Thank you.

Upvotes: 1

Views: 1730

Answers (2)

Liam Bailey
Liam Bailey

Reputation: 5905

Your parameters are stored in the $_GET globals array.

You need:

if (isset($_GET['offset']) && $_GET['offset'] == 10)
{
echo "show this";
}
else {
echo "show that";
}

Update from Comment

If you are going to have multiple quantities then a switch statement would be better:

if (isset($_GET['offset']))
{
    switch($_GET['offset'])
    {
        case 10:
            echo "show for 10";
        break;

        case 20:
            echo "show for 20";
        break;

        case 30:
            echo "show for 30;
        break;
//and so on
    }
}
else {
echo "show for no offset";
}

Upvotes: 1

ronalchn
ronalchn

Reputation: 12335

Check if the appropriate key in the $_GET associative array is set.

isset($_GET['offset'])

Thus, in your code, it will be:

<?php 
function parameterUrl() {
  if (isset($_GET['offset'])) {
    echo "Show this";
  }
  else {
  }
} 
?>

Optionally if it needs to be equal to 10, use (isset($_GET['offset']) && $_GET['offset'] == 10).

Upvotes: 0

Related Questions