Find the answer to your Linux question:
Results 1 to 8 of 8
whats the easiest/most efficent way to have multiple pages in one file?...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! napolian's Avatar
    Join Date
    Aug 2005
    Posts
    20

    php


    whats the easiest/most efficent way to have multiple pages in one file?

  2. #2
    Linux Engineer
    Join Date
    Nov 2004
    Location
    home
    Posts
    796
    Perhaps use some if/elseif statements to figure out which page is requested (based on the parameters passed in the query string), then display the page accordingly.

  3. #3
    Just Joined! napolian's Avatar
    Join Date
    Aug 2005
    Posts
    20
    would you mind giving me an example?

  4. #4
    Linux Engineer
    Join Date
    Nov 2004
    Location
    home
    Posts
    796
    Code:
    if ($_GET['p'] == 'apples')
    {
        echo "This is the apples page";
        // and more of the apples page can go here
    }
    elseif ($_GET['p'] == 'cucumbers')
    {
        echo "This is the cucumbers page";
        // and more of the cucumbers page can go here
    }
    else
    {
        echo "This is the default page";
        // and more of the default page can go here
    }
    Then you can access the different pages by using monkeys.php?p=apples or monkeys.php?p=cucumbers . The p= is part of the query string, and is accessed from the $_GET array. Of cource things like the header and footer and all would be outside the if statements if it would be the same for all pages. Hopefully this helps.

  5. #5
    Just Joined!
    Join Date
    Oct 2005
    Location
    Buenos Aires, Argentina
    Posts
    24
    You can have the same effect with a switch statement.
    Code:
    switch ($_GET['p']){
        case 'apples':
            echo "This is the apples page";
            // and more of the apples page can go here
            break;
        case 'cucumbers':
            echo "This is the cucumbers page";
            // and more of the cucumbers page can go here
            break;
        default:
            echo "This is the default page";
            // and more of the default page can go here
            break;
    }
    Or you can also use eval and include if you don't want to write the whole code for each page on the same file and you still want to be able to access all the pages from the same file.

    Code:
    switch ($_GET['p']){
        case 'apples':
            eval("include 'apples.php';");
        case 'cucumbers':
            eval("include 'cucumbers.php';");
            break;
        default:
            eval("include 'default.php';");
            break;
    }
    And then you can write the code for each page on a separate file.

  6. #6
    Just Joined! napolian's Avatar
    Join Date
    Aug 2005
    Posts
    20
    Thank you all

  7. #7
    Just Joined! napolian's Avatar
    Join Date
    Aug 2005
    Posts
    20
    Thank you all

  8. #8
    Just Joined! napolian's Avatar
    Join Date
    Aug 2005
    Posts
    20
    Thank you all

Posting Permissions

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