treehouse : what would you like to learn today?
Web Design Web Development iOS Development

PHP website Layout

  • example:
    Header.php

    <!DOCTYPE html>
    <html>
    <head>
    Head stuff here such as meta, and stylesheets etc.
    </head>
    <body>
    <header>
    <h1>Website Title</h1>
    <span>Subheading</span>

    <nav>
    <ul>
    <li><a href="#">home</a></li>
    <li><a href="#">about</a></li>
    <li><a href="#">gallery</a></li>
    <li><a href="#">contact</a></li>
    </ul>
    </nav>
    </header>

    <div id="main-content">



    pages such as index.php and about.php


    <p>This is a paragraph with body text and lists and such<p>


    Footer.php

    </div> <!-- End of main-content -->

    <footer>
    <nav>
    <ul>
    <li><a href="#">home</a></li>
    <li><a href="#">about</a></li>
    <li><a href="#">gallery</a></li>
    <li><a href="#">contact</a></li>
    </ul>
    </nav>
    <span>&copy; Copyright 2011 - 2012. All Rights Reserved.</span>
    </footer>




    thats the layout of my website and im just wondering if there's a better way i could use php instead of including header and footer in every page?
  • Actually that is exactly the way Wordpress does it. The index.php page of Wordpress generally looks like this: first <?php get_header(); ?>, then it gets all the main content using the loop, after that it sometimes gets a sidebar and it ends with <?php get_footer(); ?>.

    So I'd say your code above is pretty much the same, of course if you don't want the footer or header in one of your pages you just don't put for example <?php get_header(); ?>in your code.

    Be sure to add the </body>and </html>closing tags to your footer file though.
  • how does it use loops for the pages though. i reallly dont want to have get header and footer on each page.

    and whats the difference between get header and include header?
  • I used to use this for my index.

    <header>
    All the header stuff and nav
    </header>

    <div id="main-content">
    <?php
    if(isset($_GET["page"]) && $_GET["page"] != "home"){
    if(file_exists($_GET["page"].".php")){
    include($_GET["page"].".php");
    }else{
    include("PageNotFound.php");
    }
    }else{
    include("home.php");
    }
    ?>

    </div>

    <footer>
    All of the footer stuff
    </footer>




    and every page had to be


    <?php
    echo "<p>this is the page content</p>"
    ?>


    and i HATED this way because i didn't like the fact that i had to echo the entire page including images and everything. aldo the code highlighting in my text editor was one color which made everything confussing. so i wasn't sure what to do but the way im doing it. so thats why now im looking at a different way. i can see that im not doing anything wrong. but would like to figure out how to loop in pages so i dont have to put include header and include footer on every page. because i would like to add a blog (write it myself) one day down the road. and probibally some sort of CMS myself for my site. but thats LAATERR down the road for the entire site.
  • Looks good to me other than a few minor things. Make sure to add meta charset in your header and close the body and html in the footer.


    I think it would be a good idea to just use Wordpress now if you plan to use a CMS in the future. Wordpress is really simple if using the right blank theme.
  • Just out of curiosity - why don't you just use Wordpress? The way I see it, it fits your needs exactly, because as you say, it's a hassle the way you get and include everything now. Using Wordpress you can just use Wordpress' functions and that seems a lot easier than you're doing it now.
  • The first post above was just an example. my site does have it.

    i used to use wordpress, but when i installed it and set everything up. honestly i didn't know what I was looking at, and the snippets in PHP for CSS-Tricks, i didn't know what was going on and how to implement them . now that im learning php more im getting around easier. but would like to be able to learn the functions id like in an example i can relate to. to be able to use it. like i dont need a blog now, and just want these static pages.
  • but how would i make a loop for header to output the proper page from links they click.

    wow that was really bad wording.


    when someone clicks about, how do i make the site go to about through a loop.
  • For what, why do you need it to go through the loop?
  • so i dont need to put include header and include footer into EVERY page. incase i add more pages.
  • So you're asking how you can do something like:

    if home show content of home || if about show content of about || if contact show content of contact

    else show 404?


  • or some thing like


    <div id="main-content">
    <?php
    if(isset($_GET["page"]) && $_GET["page"] != "home"){
    if(file_exists($_GET["page"].".php")){
    include($_GET["page"].".php");
    }else{
    include("PageNotFound.php");
    }
    }else{
    include("home.php");
    }
    ?>

    </div>


    but so i wouldn't have to ECHO the entire page. cause thats a pain to read in a text editor its just 1 color.
  • I'm not sure this would be beneficial but you could do something similar below.

    Get URL
    If home.php echo content
    else if page.php echo content
    ...
    else echo 404.php

    endif

    Sounds more simple this way
  • I guess i got a bit of learning to do.
  • I would steer clear of this method and just suck it up. Because you can not possibly have that many pages. If you do, this the way you are talking about it will both kill your SEO and the scalability of any site. These two reasons alone make the cost out-way the benefits. You will also learn a lot more by doing the project in a more scalable manner.
  • Well thank you. I appreciate your response, and will take act upon it and keep what I have.

    one question though, when I'm using the include header, and footer, (mainly header) how do i do Nav highlight, and change the title depending on the page the user is on? because right now i have no way of telling my TITLE tag where the user is
  • get the url
    remove php extension so you have: home, about, contact
    echo that to a body class

    In your css target the body class with a current class to your nav item

    I'm not sure I explained that well. I think you should stick to Wordpress, man. Atleast until you get the hang of how to loop your content.

    @seanroberts - I'm not aware of how a loop will "kill" your SEO, care to explain?
  • never trust user input. something like

    include($_GET["page"].".php");
    is evil itself.

    now, if you don't want to "echo the whole page", don't use include(), just use file_get_contents(), and *echo that. that also means you can use an .html extension, which might help with text editor syntax highlighting, too.

    (* what I personally do is build the whole output, then set the appropriate headers, then have a single echo $output; at the very end)

    if you want to use a whitelist:

    // do stuff

    if (in_array($_GET['page'], array('home', 'about', 'another_page', 'you_get_the_idea')))
    {
    $page = file_get_contents('path_to_pages/' . $_GET['page'] . '.html');
    }
    else
    {
    $page = '404 sadface.';
    }

    // do stuff
    that is the one super secure way -- nothing you haven't predetermined to be a valid argument gets accepted.

    I guess you could also remove all dots and slashes from $_GET['page'], and then do a

    if (file_exists('path_to_pages/' . $_GET['page']))

    instead of the in_array() check, that *should* be safe, too, but maybe there is more to watch out for than dots and slashes I'm not sure, I'm usually using the a database anyway, and when I don't I usually use a white list because hey, it's not *that* much hassle and it's bulletproof.


    as long as it's your own little site, go for it... you can always "just use wordpress" later. yes, if you want good results fast, use wordpress :P and if you do it your own way, you will eat your foot so many ways! it's amazing what one can screw up (there's more to HTTP than just echoing stuff, if you want things properly cached, support 304 not modified and whatnot), and you'll be fixing errors and wait months for google to forgive you haha... but I say, go for it. learn how this stuff actually works.

    everybody should have built at least one crappy little CMS before using those of others, that's my motto :P
  • one thing that's really important, set the right headers! and generally learn about the awesome extra stuff you can do with them, starting with the basics of setting a content-length that matches the length of the string you're going to output, working yourself up to caching, etags and 304 not modified :D

    but one thing is sure, if you don't set a 404 header when outputting a 404 page, search engines will hate you. that's the one thing about headers you *have* to learn. been there, done that ^^
  • when using


    if (in_array($_GET['page'], array('home', 'about', 'another_page', 'you_get_the_idea')))
    {
    $page = file_get_contents('path_to_pages/' . $_GET['page'] . '.html');
    }
    else
    {
    $page = '404 sadface.';
    }


    does that mean i should be making an index file. with a header and footer inside there and just using the above code on the index for main-content?

    and wont need to include my header and footer?
  • okay. so the first pahe shows the header and footer, but no content.

    when i go to other pages, it just shows the stripped down version of the page, no header, footer, it stripps the styling aswell. ? what am i doing wrong?

    (i removed header and footer of all pages.)
    made my index page the one to work off of. here's an example:
    instead of the example ill show you what im using exactly.


    <!DOCTYPE html>

    <html lang="en">
    <head>
    <meta name="description" content="Attila Hajzer is a website design and Developoper, he uses the latest HTML5, CSS3, jQuery and PHP practices in the industry to make your next website. Who will you choose to create the image of your company?">
    <meta name="keywords" content="Attila Hajzer, Attila, Hajzer, Web, Mobile, Device, Media, CSS, CSS3, HTML 5, PHP, jQuery, Website Design, Website Development, Web design, Web Development">
    <meta charset=uft-8>
    <title>Attila Hajzer Website Design Portfolio</title>
    <!--[if IE]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script&gt;
    <![endif]-->
    <link rel="stylesheet" href="global.css" media="screen"/>
    <link rel="stylesheet" href="mobile.css" media="only screen and (max-device width:480px)"/>
    <link href='http://fonts.googleapis.com/css?family=Gentium+Book+Basic' rel='stylesheet' type='text/css'> <!-- Header Font -->
    <link href='http://fonts.googleapis.com/css?family=Crimson+Text:400italic' rel='stylesheet' type='text/css'> <!-- Header Sub header -->
    </head>

    </body>
    <div id="wrapper">

    <header>
    <a href="/" id="header"><h1>Attila Hajzer</h1><span>Web Design Portfolio</span></a>
    <div id="navigation">
    <nav>
    <ul>
    <li><a href="home.php">Home</a></li>
    <li><a href="about.php">About</a></li>
    <li><a href="services.php">Services</a></li>
    <li><a href="portfolio.php">Portfolio</a></li>
    <li><a href="resume.php">Resume</a></li>
    </ul>
    </nav>
    </div>
    </header>



    <div id="main-content">


    <?php

    if (in_array($_GET['page'], array('home', 'about', 'services', 'portfolio' , 'resume' , 'skills')))
    {
    $page = file_get_contents('/' . $_GET['page'] . '.php');
    }
    else
    {
    $page = '404 sadface.';
    }

    ?>

    </div> <!-- end of main-content -->

    </div>

    <footer>
    <div class="inside">

    <section class="email">
    <h3>Email Inquiries</h3>
    <p>Attilahajzer5@gmail.com</p>
    </section>

    <section class="copyright">
    <h3>&copy; 2011 Attila Hajzer Designs</h3>
    <p>Web Design, Niagara Falls, ON, Canada</p>
    </section>
    <div class="left"></div>

    <nav>
    <ul>
    <li><a href="home.php">Home</a></li>
    <li><a href="about.php">About</a></li>
    <li><a href="services.php">Services</a></li>
    <li><a href="portfolio.php">Portfolio</a></li>
    <li><a href="resume.php">Resume</a></li>
    </ul>
    </nav>
    <script type="text/javascript"><!--
    google_ad_client = "ca-pub-8898760799249402";
    /* Website Footer */
    google_ad_slot = "1709532676";
    google_ad_width = 234;
    google_ad_height = 60;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
    </script>
    </div>
    </footer>


    </body>
    </html>


    here's the link to the website http://attilahajzer.host-ed.net/
  • please try to understand what the code actually does :)

    it is reading the file, and assigning the content to a variable ($page) -- but unless you echo that variable, of course nothing will show up! I just like to put stuff in a variable first, because then you can do more with it than just echo it.. but in this case that's unnecessary I guess. sorry for the confusion ^^
  • oh. my bad. lol well back to the drawling board.
  • To be honest, the way you do it with the includes, that's how I do it - except i use the require(); function.





    <?php //header.php

    //if i'm using mysql:
    require('mysqlstuff.php') // this requires functions i made for accessing mysql databases

    //set title dynamically
    if(isset($title)) $title=$title;
    else $title="My Page Title";

    //start document
    print <<< END
    <!DOCTYPE html>
    <html>
    <head>
    <title>$title</title>
    <!-- header stuff like meta/css links -->
    </head>
    <body>
    END;
    ?>






    <!-- footer -->
    </body>
    </html>





    <?php // index.php
    session_start(); // start session if needed

    //set title
    $title="Different title from the default one in header.php";
    require('header.php');

    print <<< END
    <!-- content stuff -->
    <h1> haha </h1>
    END;
    require('footer.html'); // or footer.php depending on the type of code in the file
    ?>

  • @ChristopherBurton

    There is a way to make it SEO friendly and I was assuming how he was talking about doing the page is not good for SEO. If you create the header information (Meta data, Title, etc.), or have a function that adds information to the page based on the current page. However, I assumed he would not do that and just use the current page to build the actual page content. And here is where he confirms my assumptions. Doing the page this way will cause duplicate page titles, descriptions, etc.
  • Why are you so adamant on not using the includes or requires? This is what I do.

    I use variables for things that need to be page specific, like title and desc tags. This is an example of a drummed down version of what I typically do for small static sites.

    header.php


    <!doctype html>
    <head>
    <title><?php echo $thisTitle; ?></title>
    <meta name="description" content="<?php echo $thisDescription; ?>">
    <meta name="keywords" content="">
    <link rel="stylesheet" href="css/style.css">
    </head>

    <body>
    <div id="container">
    <header>
    <nav>
    </nav>
    </header>



    index.php:


    <?php
    $thisTitle='This is the title you change for each page';
    $thisDescription='This is the desc you change for each page';
    include ('html/header.php');
    ?>

    <div id="main">
    </div>

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

  • so you do want to rewrite your header and footer includes on each page. Well, use Ajax ! It's bad for SEO but good for the soul. just kidding...
  • This topic has already been solved. thank you for your continued input but you can see the the results leading to the final post here: http://w3schools.invisionzone.com/index.php?s=9ed4f437859725d5aff8975846b0fd31&showtopic=39783