Results 1 to 2 of 2

Thread: URL variables in PHP

  1. #1
    Join Date
    Mar 2009
    Posts
    3

    URL variables in PHP

    PHP Code:
    <?php
    if (isset($_REQUEST['tab'])) {
    $tab $_REQUEST['tab'];} 
    else {
    $tab 1;}
    ?>
    PHP Code:
    <?php
      
    switch ($tab) {
      case 
    1:
     
    ?>
      
        <b>Tab 1</b>
        <a href="index.php?tab=2">Tab 2</a>

       <?php include("page1.php"); ?>
       
     <?php
      
    break;
      case 
    2:
     
    ?>   
     
        <a href="index.php?tab=1">Tab 1</a>
        <b>Tab 2</b>

       <?php include("page2.php"); ?>
       
     <?php
      
    break;}
     
    ?>
    In the above code, how do I change the variable "tab" to a custom name? For example, instead of having "index.php?tab=1", I would like "index.php?mypage".

  2. #2
    Join Date
    Feb 2007
    Location
    Ireland
    Posts
    1,007
    Your switch statement won't work. In order for it to work, all the "html" code must be contained within the php tags. Otherwise the html code won't be parsed by the PHP. You need to change to this:

    PHP Code:
    <?php
      
    switch ($tab)
      {
         case 
    1:
            echo 
    '<b>Tab 1</b>';
            echo 
    '<a href="index.php?tab=2">Tab 2</a>';
            include(
    "page1.php");
            break;
         case 
    2:
            echo 
    '<a href="index.php?tab=1">Tab 1</a>';
            echo 
    '<b>Tab 2</b>';
            include(
    "page2.php");
            break;
         default:
            break;
      }
    ?>
    Now as for the url. I don't believe you can do that.

    index.php?mypage

    You are defining a get variable here called mypage and inserting nothing in it. The ? will see the mypage as a variable and not a value to be used. You will have to put in it like so:

    index.php?page=Home

    I was looking into "the urls used in wikipedia", I can't for the life of me remember the name of the technique and I couldn't find it anyway. (Probably because I don't know what to look for.) But maybe that's a direction you need to look into.
    “The best thing about a boolean is even if you are wrong, you are only off by a bit.”

Similar Threads

  1. Passing Variables (php to js to php)
    By aazad in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 02 Oct 2008, 07:56 PM
  2. How can i get a url from a php include file?
    By swiftmed in forum Web Design, HTML Reference and CSS
    Replies: 0
    Last Post: 22 Dec 2006, 10:45 AM
  3. php undelcared variable in url
    By ngubie in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 16 Aug 2006, 12:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •