1

I'd like to replace content within my page based on the URL parameter. Ideally I'd like to use PHP to get:

if {{parameter is X}} display {{content X}}

if {{parameter is Y}} display {{content Y}}

..for a few pages.

Current set up:

<?php if ($CURRENT_PAGE == "Index") { ?>
<div id="firstDiv">this is the standard page</div>
<?php } ?>

<?php if ($CURRENT_PAGE == "p1") { ?>
<div id-"secondDiv">this is a variation of the page</div>
<?php } ?>

And using

include("includes/content.php");
to call the html blocks to the page

The firstDiv displays in index.php as expected, but adding the URL parameter changes nothing - the same div still shows (I'd like it to be replaced with the secondDiv)

It seems $CURRENT_PAGE doesn't like URL parameters - what is the alternative?

Hopefully this makes sense, I'm pretty new to PHP. Happy to provide more details if required.

Thanks in advance for any help.

-- UPDATE --

Thank you for the answers so far!

It seems I missed part of my own code (Thanks to vivek_23 for making me realise this - I'm using a template, excuse me!!)

I have a config file that defines which page is which, as so:

<?php
  switch ($_SERVER["SCRIPT_NAME"]) {
    case "index.php/?p=1":
      $CURRENT_PAGE = "p1"; 
  break;
    default:
      $CURRENT_PAGE = "Index";
  }
?>

Before I learn $_GET, is there a way I can use my current set up?

Thanks again.

-- UPDATE 2 --

I have switched to using the $_GET method, which seems to be working well so far. My issue now is when the parameter is not set it is giving an undefined error. I'll try to remember to update with the fix.

$p = ($_GET['i']);


if($p == "1"){
    echo '<div id="firstDiv"><p>this is the first div</p></div>';
} 

Thanks to the two answerers below who suggested using $_GET

5
  • Try to get page with this $_SERVER['REQUEST_URI'] Commented Nov 17, 2018 at 5:26
  • Where is $CURRENT_PAGE defined and how did it get it's value(which is to be compared) ? Commented Nov 17, 2018 at 5:42
  • if-else-if struct better matches your case than if-if Commented Nov 17, 2018 at 5:43
  • You are setting $CURRENT_PAGE = "p1", whereas in the condition you check if ($CURRENT_PAGE == "Index/?p=1") Commented Nov 17, 2018 at 6:00
  • Thanks @TeeKea although I had actually updated that at my side - post now reflects this. Still not working Commented Nov 17, 2018 at 7:45

2 Answers 2

1

You can used $_GET like

if($_GET['p']==1){
 echo '<div id="firstDiv">this is the standard page</div>';
}else if($_GET['p']==2){
 echo '<div id="secondDiv">this is a variation of the page</div>';
}

The other way! you can used basename() with $_SERVER['PHP_SELF']

//echo basename($_SERVER['PHP_SELF']); first execute this and check the result
if(basename($_SERVER['PHP_SELF']) == 'index'){
    echo '<div id="firstDiv">this is the standard page</div>';
}else{
    echo '<div id="secondDiv">this is a variation of the page</div>';
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to send the parameters on the URL query string, like:

yourdomain.com?p=1

So, with this URL, the query string is "?p=1", where you have a GET parameter named 'p' with a value of '1'.

In PHP to read a GET parameter you can use the associative array $_GET, like this:

$current_page = $_GET['p'];
echo $current_page; // returns '1'

The rest of your logic is OK, you can display one div or the other based on the value of the p parameter. You can read more about how to read query string parameters here: http://php.net/manual/en/reserved.variables.get.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.