2

I would like to create a site in php that works the same way that https://www.bitcoins.lc/ does, in terms of it having the same layout on each page but the content would change as you change links/pages, how do I set this up in php with layout.php and index.php and header.php ect?

I was told to read about MVC frameworks but I don't really understand it all.

Any help or advice would be appreciated.

Jason

2
  • clone of http://stackoverflow.com/questions/6480947/layout-of-a-simple-php-mysql-website?
    – Florian
    Commented Jun 26, 2011 at 9:53
  • try read this topic link
    – redexp
    Commented Jun 26, 2011 at 10:14

4 Answers 4

8

Here's the approach I generally take to this:

FILES:

  • layout.php
  • index.php
  • views
    • _index.php

layout.php:

<html>
<head>
    <title><?php echo $title; ?></title>
</head>
<body>
    <?php include($childView); ?>
</body>
</html>

_index.php:

<section>
    Some page content
</section>

index.php:

<?php
    $title = 'Home';
    $childView = 'views/_index.php';
    include('layout.php');
?>

Basically, the page itself tells the layout what view to inject into the content area. It's similar to how you would use ASP.NET ContentPlaceholder elements.

0

You could include a header and a footer in each page:

<?php
include('header.php');
?>
Welcome to my page
<?php
include('footer.php');
?>

Put the header and navigation in header.php, and the footer in footer.php.

2
  • This way you have unmatched html tags on header.php and footer.php Commented Oct 7, 2011 at 17:19
  • This is the normal way and is not very neat... I wouldn't suggest this. Commented May 29, 2022 at 21:26
0

Hope you are worried about MVC as you are reading complex documentations about that. I would suggest you to go through a documentation which helps you to easily understand and create an MVC Framework. It will not be good if you move forward with your project without understanding its basics. Please have a look at the following article and let me know if it helps. Feel free to contact if you need any support.

http://www.henriquebarroso.com/how-to-create-a-simple-mvc-framework-in-php/

3
  • Ive started to go through it, but Ive got a problem, I get an error warning in Netbeans with this code: function loadView($view,$vars="") { if(is_array($vars) &amp;&amp; count($vars) > 0) extract($vars, EXTR_PREFIX_SAME, "wddx"); require_once('view/'.$view.'.php'); } Commented Jun 26, 2011 at 13:02
  • I did and that works, but I think its a little bit beyond me. I dont quite understand how it all works at the moment Commented Jun 26, 2011 at 22:10
  • Ive used the system that hogni89 posted, I understand it and ive set it up quickly and it works for me, but thanks anyway! im going to keep reading about MVC and maybe change in the future. Commented Jun 27, 2011 at 22:46
-2

The simplest approach is the one described by Sjoerd. If your page only contains some few elements, there is noting wrong with a switch or if statement.

index.php:

<html>
    <body>

        <!-- Wrapper div -->
        <div id="wrapper>


            <!-- Header div -->
            <div id="header">
                <?php
                    include('header.php'); // File containing header code
                ?>
            </div>

            <!-- Content div -->
            <div id="content">


                <!-- Left Colon div -->
                <div id="leftCol">
                    <?php
                        include('leftMenu.php'); // File containing the menu
                    ?>
                </div>


                <!-- Center colon -->
                <div id="centerCol">
                    <?php
                        $page = $_GET['page']; // To get the page

                        if($page == null) {
                            $page = 'index'; // Set page to index, if not set
                        }
                        switch ($page) {

                            case 'index':
                                include('frontPage.php');
                                break;

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

                            case 'contact':
                                include('contact.php');
                                break;
                        }

                    ?>
                </div>

            </div>

            <!-- Footer div -->
            <div id="footer">
                <?php
                    include('footer.php'); // File containing the footer
                ?>
            </div>
        </div>
    </body> 
</html>

header.php:

<?php
    echo "This is header";   
?>

leftMenu.php:

<?php
    echo "<a href='index.php/?page=index'>Front Page</a>"; // set page to index
    echo "<a href='index.php/?page=about'>About</a>";      // page = about
    echo "<a href='index.php/?page=contact'>Contact</a>";  // page = contact
?>

and so on

1
  • You have to do url rewriting and make a front controller. Else your urls are pretty ugly Commented Oct 7, 2011 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.