2

I am trying to lad content from page1.php, page2.php and page3.php with the help of url varibale in my index page.

Here is my index page code

<html>
<head>
</head>
<body>
<h1>Hello there></h1>
<ul>
<li><a href="index.php?page=page1.php">page 1</a>page1</li>
<li><a href="index.php?page=page2.php">page 2</a>page2</li>
<li><a href="index.php?page=page3.php">page 3</a>page3</li>
</ul>
<?php
    $page = $_GET['page'];
    $pages = array('page1', 'page2', 'page3');
    if (!empty($page)) {
        if(in_array($page,$pages)) {

            include($page);
        }
        else {
        echo 'Page not found. Return to
        <a href="index.php">index</a>';
        }
    }
    else {
        include('page1.php');
    }
?>
</body>
</html>

the index page shows undefined variable $page

4
  • $page will get you pagex.php and you are checking it with pagex in inarray what is the value in $page. Commented Dec 3, 2015 at 4:18
  • If you are going to do this method you should just do page=1 then check if 'page'.$_GET['page'].'.php' is a file Commented Dec 3, 2015 at 4:23
  • when you visit index.php the key 'page' is not set in your $_GET array. if you want to avoid that notice you will want to check isset($_GET['page']) before assigning it to $page Commented Dec 3, 2015 at 4:24
  • check my ans nishant Commented Dec 3, 2015 at 4:58

3 Answers 3

1

you have to use array('page1.php', 'page2.php', 'page3.php'); or avoid .php extension from url aswell as array and use $page.".php" in include. Also make sure that $_GET['page']; is set

<html>
<head>
</head>
<body>
<h1>Hello there></h1>
<ul>
<li><a href="index.php?page=page1.php">page 1</a>page1</li>
<li><a href="index.php?page=page2.php">page 2</a>page2</li>
<li><a href="index.php?page=page3.php">page 3</a>page3</li>
</ul>
<?php
    $page = isset($_GET['page'])?$_GET['page']:'page1.php';
    $pages = array('page1.php', 'page2.php', 'page3.php');
    if (!empty($page)) {
        if(in_array($page,$pages)) {

            include($page);
        }
        else {
        echo 'Page not found. Return to
        <a href="index.php">index</a>';
        }
    }
    else {
        include('page1.php');
    }
?>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

I'd recommend using a routing system from a framework.

Aside from that your $_GET variable: index.php?page=page1.php is returning the string "page1.php" which doesn't exist in your $pages array. Add the .php into your $pages array and you should have it working.

Recommendations for routing systems: http://silex.sensiolabs.org/

Uses symfonys routing and request components. Will save you some headaches.

3 Comments

okkk. My array contains page1.php, page2.php and page3.php.It is working fine but on index.php page it still shows Notice: Undefined index: page in C:\xampp\htdocs\mynew\index.php on line 12
Php Notices can be disabled in the ini file, but the notice is being thrown because you're attempting to pull an member of an array that doesn't exist: like page4.php. I'd double check to make sure no funny business is happening in-relation to your arrays.
More information on php errors, warning, notices : pc-freak.net/blog/turn-php-notices
0
<html>
<head>
</head>
<body>
<h1>Hello there></h1>
<ul>

change page1.php to page1 beacuse in array there is no page1.php it's only page1.....

<li><a href="index.php?page=page1">page 1</a>page1</li>
<li><a href="index.php?page=page2">page 2</a>page2</li>
<li><a href="index.php?page=page3">page 3</a>page3</li>
</ul>
<?php

you got undefine index because first time when page load $_GET['page'] doesn't exit.you need to check wether $_GET['page'] is set or not

if(isset($_GET['page']))
{
    $page = $_GET['page'];
    $pages = array('page1', 'page2', 'page3');
    if (!empty($page)) {
        if(in_array($page,$pages)) {

here $page only contain page1,page2...else so you need to concate with ".php"

            include($page.".php");
        }
        else {
        echo 'Page not found. Return to
        <a href="index.php">index</a>';
        }
    }
    else {
        include('page1.php');
    }
}
?>
</body>
</html>

Comments

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.