6 Replies Latest reply on Aug 14, 2007 9:52 AM by bregent

    Applying PHP Variables to URL

    AngryCloud Level 1
      How can I set links to add, change, or remove a specific variable on a URL without having to rewrite the whole thing?
        • 1. Re: Applying PHP Variables to URL
          Level 7
          AngryCloud wrote:
          > How can I set links to add, change, or remove a specific variable on a URL without having to rewrite the whole thing?

          not sure what you mean, but here's typical situation:

          if you have a link like this

          <a href="page.php?var=red">link</a>

          the in page.php, you can have:

          <?php if(isset($_GET['var'])){
          $var = urldecode($_GET['var']);
          if($var = 'red'){
          do someting...
          }elseif($var = 'blue'){
          do something else...
          }
          }
          --
          seb ( ---@webtrans1.com)
          http://webtrans1.com | high-end web design

          An Ingenious WebSite Builder: http://sitelander.com
          • 2. Re: Applying PHP Variables to URL
            Level 7
            (_seb_) wrote:
            > AngryCloud wrote:
            >> How can I set links to add, change, or remove a specific variable on a
            >> URL without having to rewrite the whole thing?
            >
            > not sure what you mean, but here's typical situation:
            >
            > if you have a link like this
            >
            > <a href="page.php?var=red">link</a>
            >
            > the in page.php, you can have:
            >
            > <?php if(isset($_GET['var'])){
            > $var = urldecode($_GET['var']);
            > if($var = 'red'){
            > do someting...
            > }elseif($var = 'blue'){
            > do something else...
            > }
            > }

            sorry it should be

            if($var == 'red')

            and

            elseif($var == 'blue') (double equal signs within conditionals)

            --
            seb ( ---@webtrans1.com)
            http://webtrans1.com | high-end web design

            An Ingenious WebSite Builder: http://sitelander.com
            • 3. Re: Applying PHP Variables to URL
              AngryCloud Level 1
              Here's an example of what I meant:

              This is the current URL:
              http://mysite.com/index.php?page=browse&type=images&category=2

              There is a link on the page that adds/changes the URL variable, 'order' , to 1

              Instead of setting the hyperlink to something like:
              index.php?page=browse&type=<?php echo $_GET['type']; ?>&category=<?php echo $_GET['category']; ?>&order=1

              ...I would just like to set it to something more simplistic that only adds or changes 'order' and keeps all the other current variables in tact without having to write all of that.

              That way I don't have to include in every hyperlink every possible variable a page could have just to make sure no variables are lost.
              • 4. Re: Applying PHP Variables to URL
                Level 7
                AngryCloud wrote:
                > ...I would just like to set it to something more simplistic that only adds or
                > changes 'order' and keeps all the other current variables in tact without
                > having to write all of that.

                If it's the same page, you can use $_SERVER['PHP_SELF'], which contains
                a reference to the current URL. However, you then encounter the problem
                that the user might click the link again, adding &order=1 ad infinitum.
                Although that probably wouldn't affect functionality, it's not very
                elegant. So you still need to perform a check before adding the new
                variable. You could do it like this:

                a href="<?php echo strpos($_SERVER['PHP_SELF'], 'order=') ?
                $_SERVER['PHP_SELF'] : $_SERVER['PHP_SELF'].'&order=1'; ?>"

                --
                David Powers, Adobe Community Expert
                Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
                Author, "PHP Solutions" (friends of ED)
                http://foundationphp.com/
                • 5. Re: Applying PHP Variables to URL
                  Level 7
                  .oO((_seb_))

                  ><?php if(isset($_GET['var'])){
                  > $var = urldecode($_GET['var']);

                  PHP automatically decodes the query string.

                  Micha
                  • 6. Re: Applying PHP Variables to URL
                    bregent Most Valuable Participant
                    >This is the current URL:
                    > http://mysite.com/index.php?page=browse&type=images&category=2
                    >
                    >There is a link on the page that adds/changes the URL variable, 'order' , to 1

                    I don't see that link on the page. Where is it?


                    >...I would just like to set it to something more simplistic that only adds or changes 'order' and keeps
                    >all the other current variables in tact without having to write all of that.

                    Is order a sort order option? If so, I think you are much better off implementing client side sorting rather than server side.