Grow your CSS skills. Land your dream job.

PHP website Layout

  • # September 17, 2011 at 2:09 pm

    example:
    Header.php


    < !DOCTYPE html>


    Head stuff here such as meta, and stylesheets etc.



    Website Title


    Subheading






    pages such as index.php and about.php



    This is a paragraph with body text and lists and such


    Footer.php






    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?

    Bob
    # September 17, 2011 at 2:22 pm

    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 and closing tags to your footer file though.

    # September 17, 2011 at 2:29 pm

    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?

    # September 17, 2011 at 2:37 pm

    I used to use this for my index.



    All the header stuff and nav



    < ?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");
    }
    ?>




    All of the footer stuff


    and every page had to be

    < ?php
    echo "

    this is the page content

    "
    ?>

    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.

    # September 17, 2011 at 2:46 pm

    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.

    Bob
    # September 17, 2011 at 2:51 pm

    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.

    # September 17, 2011 at 3:11 pm

    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.

    # September 17, 2011 at 3:15 pm

    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.

    # September 17, 2011 at 3:23 pm

    For what, why do you need it to go through the loop?

    # September 17, 2011 at 4:06 pm

    so i dont need to put include header and include footer into EVERY page. incase i add more pages.

    # September 17, 2011 at 4:10 pm

    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?

    # September 17, 2011 at 4:14 pm

    yes.

    # September 17, 2011 at 4:15 pm

    or some thing like



    < ?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");
    }
    ?>


    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.

    # September 17, 2011 at 4:23 pm

    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

    # September 17, 2011 at 7:08 pm

    I guess i got a bit of learning to do.

Viewing 15 posts - 1 through 15 (of 30 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".