-2

I'm trying to build a website that having different pages. I'm a newbie to PHP, I have an index page with certain tables for holding header, navigation menu, main body, a sidebar and for a footer. The index page is attached to all the above-mentioned elements in it using include("filename.extension");. The problem is I tried to load the main body i.e content of the site, dynamically when the menu is changed.

Below is my code, any suggestion on this is very appreciatable. Thanks in advance.

<body>
<div> <?php Include('header.php'); </div>

<div id="menu" align="center">
    <table width="790" height="35">
        <tr>
            <td>Home</td>
            <td><a href="pages/students/reg.php">Register</a></td> // this page need to display on the same window with other elements, after click.
        </tr>
    </tabel>
</div>

<div id="sidebar" align="right"> <?php include ("sidebar.php");?> </div>

<div id="Content ">
    <?php include("FILE"); // here i need to display the hyper linked page.?>
</div>

<div id="footer"> <?php include("footer.php");?> </div>
</body> `
Prasanthvel
  • 27
  • 2
  • 15
  • Help us help you, include your current code. – chris85 Aug 02 '15 at 16:32
  • I think this is a server problem(Apache, Nginx or whatever you're using). Try this posts: http://stackoverflow.com/questions/7264014/why-is-my-php-source-code-showing, http://stackoverflow.com/questions/12142172/apache-shows-php-code-instead-of-executing, http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-i-can-see-it-on-source-code-of-page – Rulisp Aug 02 '15 at 16:38

2 Answers2

3

guess Diogo's answer will get you to what you want. create your index.php file like this.

//menu area 
<div id="menuWrap">
  <a href="?page=page1">link to page1</a>
  <a href="?page=page2">link to page2</a>
</div>

//side bar
<div id="sidebarWrap">...</div>

//content area
<div id="contentWrap">
<?php
switch($_GET['page']) 
{
    case 'page1':
        include '/pages/page1.php';
        break;
    case 'page2':
        include '/pages/page2.php';
        break;
    default:
        include '/pages/notfound.php';

}
?>
</div>

//footer
<div id="footerWrap">...</div>

This way you can display other pages in your site in the same window, only the content area will change

update:

I have got corrected your typos, here is the answer according to your code segment.

<body>
<div>
    <?php Include('header.php'); ?>
</div>
<div id="menu" align="center">
    <table width="790" height="35">
        <tr>
            <td><a href="?page=page1">Home</a></td>
            <td><a href="?page=page2">Register</a></td>
        </tr>
    </table>
</div>
<div id="sidebar" align="right">
    <?php include ("sidebar.php"); ?>
</div>
<div id="Content ">
    <?php
        switch($_GET['page']) 
    {
        case 'page1':
            include '/pages/home.php';//file path of your home page
            break;
        case 'page2':
            include '/pages/students/reg.php';
            break;
        default:
            include '/pages/notfound.php';

    }
    ?>
</div>
<div id="footer">
    <?php include("footer.php");?>
</div>
</body>
user27
  • 169
  • 1
  • 9
  • i'm in the process of checking your code and there were some typo which u need to correct. in 'Include('header.php');' part closing php tag is missing (?>). And in the table, the closing table tag has typed as . First get these errors correct and check again – user27 Aug 02 '15 at 18:30
  • It shows one error on the switch statement line. The error what i'm getting is -> Undefined index:page in C:/xampp/htdocs/it/index.php – Prasanthvel Aug 02 '15 at 19:13
  • Please give some solution – Prasanthvel Aug 06 '15 at 12:14
0

First, create a file with exactly the name below: (including the dot)

.htaccess

and put it in the same folder of your index.

Inside the .htaccess, write the following code:

# .htaccess mod_rewrite

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]

The .htaccess on this case will rewrite the URLs to change the http://127.0.0.1/index.php?page=register to accept the "blogging pretty url" http://127.0.0.1/register

inside of your index.php folder, create a new folder "pages".

Inside of the folder pages, create 4 simple PHP files (home.php, register.php, anyother.php and notfound.php), with any content like

<?php
   echo "I'm the Register Page.";
?>

This will be an example of index based on your code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8"><title>¨title</title></head>
<body>
    <div>
        <?php include 'header.php'; ?>
    </div>
    <div id="menu" align="center">
        <table width="790" height="35">
            <tr>
                <td><a href="/home">Home</a></td>
                <td><a href="/register">Register</a></td>
                <td><a href="/anyother">AnyOther</a></td>
                <td><a href="/misstypo" title="This will lead to the notfound.php">Broken Sample</a></td>
            </tr>
        </table>
    </div>
    <div id="sidebar" align="right">
        <?php include 'sidebar.php'; ?>
    </div>
    <div id="Content ">
        <?php
            if(!isset($_GET['page']) || $_GET['page'] == ''){
                $page = 'home'; //If no page specified
            } else {
                $page = $_GET['page'];
            }

            switch($page)
            {
                case 'home':
                    include '/pages/home.php';//file path of your home page
                    break;
                case 'register':
                    include '/pages/register.php';
                    break;
                case 'anyother':
                    include '/pages/anyother.php';
                    break;
                default:
                    include '/pages/notfound.php'; //If any page that doesn't exists, then get back to home.

            }
        ?>
    </div>
    <div id="footer">
        <?php include("footer.php");?>
    </div>
</body>
</html>
KodornaRocks
  • 425
  • 3
  • 14
  • I need the concept of bloggers site. I need to display other page of my site in the same window with the sidebars and footer, which i have already included in the index page – Prasanthvel Aug 02 '15 at 16:45
  • Explain better what do you need or put some code in or question. I'm in doubt about what you really need. Maybe you need a ModRewrite to have the "website.com/articlename/" and keep the layout – KodornaRocks Aug 02 '15 at 16:48
  • I tried the same code you mentioned above. But i still get an error on the switch statement line – Prasanthvel Aug 03 '15 at 03:48
  • Its works nice. But i get error line while accessing the home page (error on switch statement line). Other than that its work fine – Prasanthvel Aug 03 '15 at 04:24
  • Hi Diogo Paim, Could you please close this question as answered?? I don't have the privilege to close this. – Prasanthvel Jun 15 '17 at 06:55
  • @Prasanthvel There's no such thing as closing a question here on StackOverflow, you can either delete the question or accept the answer only, the purpose of the website is to create a big knowledge base that can be updated with better answers over the years. – KodornaRocks Jun 16 '17 at 14:30