0

I am trying to use PHP include on my website but I am running into trouble...

To start this is my code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Future | Origins</title>
</head>
<body>
    <?php
        if (isset($_GET['p'])) {
            $p = $_GET['p'];

            include('include/header.php');

            switch($p) {

                case "about":
                include('include/about.php');
                break;

                default:
                    $p = "include/main.php";
                break;

            }

            include('include/footer.php');
        }
    ?>
</body>
</html>

It only shows content for my index page if my URL is styled like

localhost/index.php?p=

I would like it to show content for my index page using the default

localhost

I don't have much knowledge of PHP and there never seems to be a straightforward solution. I am including multiple pages in my website using a switch case.

Many thanks

2
  • 1
    So add an else with what to include when no parameter is given; right now you don't do anything when p is not passed in the query string. Commented Jun 11, 2016 at 16:15
  • Thank you, that has solved it. Also moved header and footer include outside of IF statement. :) Commented Jun 11, 2016 at 16:23

1 Answer 1

1

Just move your includes outside the if statement

<body>
<?php
    include('include/header.php');
    if (isset($_GET['p'])) {
        $p = $_GET['p'];



        switch($p) {

            case "about":
            include('include/about.php');
            break;

            default:
                $p = "include/main.php";
            break;

        }
    }
        include('include/footer.php');

?>
</body>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it is working now. Used this as well as jeroen's answer :)

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.