Replace Content on Page Based on URL Parameter with PHP












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










share|improve this question

























  • Try to get page with this $_SERVER['REQUEST_URI']

    – Gurpreet Singh
    Nov 17 '18 at 5:26











  • Where is $CURRENT_PAGE defined and how did it get it's value(which is to be compared) ?

    – vivek_23
    Nov 17 '18 at 5:42











  • if-else-if struct better matches your case than if-if

    – vivek_23
    Nov 17 '18 at 5:43











  • You are setting $CURRENT_PAGE = "p1", whereas in the condition you check if ($CURRENT_PAGE == "Index/?p=1")

    – TeeKea
    Nov 17 '18 at 6:00













  • Thanks @TeeKea although I had actually updated that at my side - post now reflects this. Still not working

    – MISQ
    Nov 17 '18 at 7:45


















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










share|improve this question

























  • Try to get page with this $_SERVER['REQUEST_URI']

    – Gurpreet Singh
    Nov 17 '18 at 5:26











  • Where is $CURRENT_PAGE defined and how did it get it's value(which is to be compared) ?

    – vivek_23
    Nov 17 '18 at 5:42











  • if-else-if struct better matches your case than if-if

    – vivek_23
    Nov 17 '18 at 5:43











  • You are setting $CURRENT_PAGE = "p1", whereas in the condition you check if ($CURRENT_PAGE == "Index/?p=1")

    – TeeKea
    Nov 17 '18 at 6:00













  • Thanks @TeeKea although I had actually updated that at my side - post now reflects this. Still not working

    – MISQ
    Nov 17 '18 at 7:45
















1












1








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










share|improve this question
















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







php html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 1:41







MISQ

















asked Nov 17 '18 at 5:21









MISQMISQ

63




63













  • Try to get page with this $_SERVER['REQUEST_URI']

    – Gurpreet Singh
    Nov 17 '18 at 5:26











  • Where is $CURRENT_PAGE defined and how did it get it's value(which is to be compared) ?

    – vivek_23
    Nov 17 '18 at 5:42











  • if-else-if struct better matches your case than if-if

    – vivek_23
    Nov 17 '18 at 5:43











  • You are setting $CURRENT_PAGE = "p1", whereas in the condition you check if ($CURRENT_PAGE == "Index/?p=1")

    – TeeKea
    Nov 17 '18 at 6:00













  • Thanks @TeeKea although I had actually updated that at my side - post now reflects this. Still not working

    – MISQ
    Nov 17 '18 at 7:45





















  • Try to get page with this $_SERVER['REQUEST_URI']

    – Gurpreet Singh
    Nov 17 '18 at 5:26











  • Where is $CURRENT_PAGE defined and how did it get it's value(which is to be compared) ?

    – vivek_23
    Nov 17 '18 at 5:42











  • if-else-if struct better matches your case than if-if

    – vivek_23
    Nov 17 '18 at 5:43











  • You are setting $CURRENT_PAGE = "p1", whereas in the condition you check if ($CURRENT_PAGE == "Index/?p=1")

    – TeeKea
    Nov 17 '18 at 6:00













  • Thanks @TeeKea although I had actually updated that at my side - post now reflects this. Still not working

    – MISQ
    Nov 17 '18 at 7:45



















Try to get page with this $_SERVER['REQUEST_URI']

– Gurpreet Singh
Nov 17 '18 at 5:26





Try to get page with this $_SERVER['REQUEST_URI']

– Gurpreet Singh
Nov 17 '18 at 5:26













Where is $CURRENT_PAGE defined and how did it get it's value(which is to be compared) ?

– vivek_23
Nov 17 '18 at 5:42





Where is $CURRENT_PAGE defined and how did it get it's value(which is to be compared) ?

– vivek_23
Nov 17 '18 at 5:42













if-else-if struct better matches your case than if-if

– vivek_23
Nov 17 '18 at 5:43





if-else-if struct better matches your case than if-if

– vivek_23
Nov 17 '18 at 5:43













You are setting $CURRENT_PAGE = "p1", whereas in the condition you check if ($CURRENT_PAGE == "Index/?p=1")

– TeeKea
Nov 17 '18 at 6:00







You are setting $CURRENT_PAGE = "p1", whereas in the condition you check if ($CURRENT_PAGE == "Index/?p=1")

– TeeKea
Nov 17 '18 at 6:00















Thanks @TeeKea although I had actually updated that at my side - post now reflects this. Still not working

– MISQ
Nov 17 '18 at 7:45







Thanks @TeeKea although I had actually updated that at my side - post now reflects this. Still not working

– MISQ
Nov 17 '18 at 7:45














2 Answers
2






active

oldest

votes


















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






share|improve this answer































    0














    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>';
    }





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348492%2freplace-content-on-page-based-on-url-parameter-with-php%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      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






      share|improve this answer




























        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






        share|improve this answer


























          0












          0








          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






          share|improve this answer













          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 17 '18 at 5:40









          Jose FloridoJose Florido

          10719




          10719

























              0














              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>';
              }





              share|improve this answer






























                0














                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>';
                }





                share|improve this answer




























                  0












                  0








                  0







                  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>';
                  }





                  share|improve this answer















                  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>';
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 17 '18 at 5:42

























                  answered Nov 17 '18 at 5:37









                  Bilal AhmedBilal Ahmed

                  3,25631435




                  3,25631435






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348492%2freplace-content-on-page-based-on-url-parameter-with-php%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)