0

I need someone to let me know a solution to this issue. I am trying to create an include on my index.php file so when a user clicks a link on my navbar the content on the index.php changes. The code below works great, except when I go to index.php, because it is not part of the array, it calls main.php twice, instead of once. I know this is because of the last portion that says:

    else {
    include('main.php');
    }

However, I need a solution to this because I am not good with php. Here is my full code for the include.

    <?php
    // Place the value from ?page=value in the URL to the variable $page.
    $page = $_GET['id'];
    // Create an array of the only pages allowed.
    $pageArray = array('index','css-pub1','page2','page3','page4','page5','page6');
    // If there is no page set, include the default main page.
    if (!$page) {
    include('main.php');
    }
    // Is $page in the array?
    $inArray = in_array($page, $pageArray);
    // If so, include it, if not, emit error.
    if ($inArray == true) {
    include(''. $page .'.php');
    } 
    else {
    include('main.php');
    }
    ?>
1
  • include_once() may be a solution. Commented Sep 27, 2013 at 16:27

4 Answers 4

1

Try to use include_once instead of include:

include_once($page . '.php');
//...
include_once('main.php');
Sign up to request clarification or add additional context in comments.

Comments

1

Just remove

if (!$page) {
    include('main.php');
}

and let the else handle main.php

Comments

0

It's because you're trying to grab the wrong $_GET parameter. Should be:

$page = $_GET['page'];

If your comments are accurate.

2 Comments

That doesn't change the issue, it only changes the url of the links, so instead of using index.php?id=page1, its now index.php?page=page1.
That's the point. Those are your $_GET parameters, so if your url currently is "index.php?page=page1" then you need to use $_GET['page'].
0

I have commented the code on the problems and fixes.

<?php
// initilize $page
$page='';
// Place the value from ?page=value in the URL to the variable $page.
if (isset($_GET['id'])){ // check if the page is set
    $page = $_GET['id'];
}
// Create an array of the only pages allowed.
$pageArray = array('index','css-pub1','page2','page3','page4','page5','page6');

/* This section is not needed
// If there is no page set, include the default main page.
if (!$page) {
include('main.php');
}
*/

// Is $page in the array?
$inArray = in_array($page, $pageArray);
// If so, include it, if not, emit error.
if ($inArray == true) {
include(''. $page .'.php');
} 
else {
// If there is no page set, include the default main page.
// this also does the same thing as the commented if loop above
include('main.php');
}
?>

3 Comments

I think he wants the main.php stuff even when he includes other pages, he just doesn't want it twice.
@Barmar not clear from OP's question. From his code comments it looked like he wants either one of the pages only
He says it works as desired when he goes to one of the other pages, it's only a problem when he goes to index.php.

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.