Jump to content

Php Website Layout


attila2452

Recommended Posts

i started a site with includes. a header file, all my content files and footer. every content file would include a header and a footer. after a few files i figured over the course of the next year it would be a bit of a redundancy to keep including the header and footer in the new content pages that would just expand, so i want to find a way which is better then that. aswell, i had friend code one of my websites before with

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

with that, I had to ECHO every file out of my website , which honestly looked really bad from a text editor one color. it also created confussion when editing the pages. so i am looking for a better alternitave. I would like an Index page with the header and footer in it, and have some kind of code like above. but so i dont have to echo the pages and make it look really bad. on CSS-Tricks.com, ive posted relativly the same thread but came up with come confussion too. another programmer said i could use

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

but that didn't work because the comment she lead after was. "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 ^^"so im back the the beginning looking for an alternitave to what i already have. Please understand that im a new programmer learning PHP and would like to learn thank you. - Attila Hajzer

Link to comment
Share on other sites

I don't know if I'm reading this right, but all you require in index.php is

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title> <style type="text/css"></style>  </head><body> <?phpinclude("header.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");							}include("footer.php");?> </body></html> 

Then you create links that match pages you have created in ordinary html code, BUT! ONLY the code between NOT including <body></body> tags

Link to comment
Share on other sites

yeah i know but i want my index to by my header and footer file. and have every other page inside the index like when you view source, it will be the header content then footer. but i dont want to have to include header or footer into any of my pages. but let me try this.

Link to comment
Share on other sites

Look at the code, can you see a echo? I really don't understand? the index page will include the header and footer php pages, and between those it will show individual page depending on link selected index.php?page=firstpage or index.php?page=secondpage, with <a href="index.php?page=firstpage">first Page</a><a href="index.php?page=secondtpage">second Page</a> the footer.php, header.php, firstpage.php, secondpage.php would/could only contain html code the footer.php<h1>This is the header</h1> header.php<h1>This is the footer</h1> firstpage.php<h1>This is the first page</h1> secondpage.php<h1>This is the second page</h1> OR you could take the html code and paste it directly into the index.php page instead and do away with header and footer.php files

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title> <style type="text/css"></style>  </head><body> <h1>This will be my header</h1><?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");                                                        }?> <h1>This will be my footer</h1></body></html>

Link to comment
Share on other sites

THANK YOU so muchhh :) soo muchh, Like i never know why i didn't think of that. because .php could have been substituted by anything really, even .do but also when you want to show HTML content in PHP you have to ECHO it out. it just didn't make sense to me why that would work. hahah. but thank you everyone for you assistance and input during the time of discouragement. I truly appreciate all of your input. another thing also CHECK OUT MY PORTFOLIO: http://attilahajzer.host-ed.net/ i would appreciate that too. and yes. i know, i have to finish it too.

Link to comment
Share on other sites

CHECK OUT MY PORTFOLIO: http://attilahajzer.host-ed.net/ i would appreciate that too. and yes. i know, i have to finish it too.
There is a Critiques forum to do this. You will get more feedback if you post it there and you'll stay on-topic.
Link to comment
Share on other sites

The only time you would use echo is to display a php variable value, records from database table dynamically, which could have html code incorporated into it. One problem with using a single index page, is that you are stuck with single title, meta tag description, and keywords. What you could use is a switch function to look at page name and assign relative title and keywords etc.

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><?php		$title='';	$meta_description = '';	$meta_keywords = ''; if(isset($_GET["page"]) && $_GET["page"] != "home")	{	if(file_exists($_GET["page"].".php"))		{		$pagelink = $_GET['page'];				switch ($pagelink)			{			case "portfolio":				$title='portfolio';				$meta_description = 'This is my portfolio page blah, blah , blah';				$meta_keywords = 'portfolio page blah, blah , blah';			break;			case "another":				$title='This is another page';				$meta_description = 'This is another page blah, blah , blah';				$meta_keywords = 'another page, of whatever';				break;				case "about-me":				$title='This is another page an IT’S ABOUT ME!';				$meta_description = 'This is another page blah, blah , blah';				$meta_keywords = 'another page, of whatever';				break;				case "sumbenu2":				$title='This is submenu2 page, not 1, not 3 but 2!';				$meta_description = 'This is submenu2 page, not 1, not 3 but 2';				$meta_keywords = 'This is submenu2 page, not 1, not 3 but 2';				break;						}		}	else		{		$title ='Error! page not found';				}	}else	{	$title='Attila Hajzer | Home';	$meta_description = 'Welcome to my portfolio';	$meta_keywords = 'Welcome, home, Attila Hajzer, Attila, Hajzer, web, design, webdesign, web-design, web design';	} echo '<title>'.$title.'</title>'."\n";echo '<meta name="description" content="'.$meta_description.'" />'."\n";echo '<meta name="keywords" content="'.$meta_keywords.'" />'."\n";?> <style type="text/css"></style>  </head><body><h1>This will be my header</h1><?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");		 } ?> <h1>This will be my footer</h1></body></html>   

Link to comment
Share on other sites

I think what you are really looking for is an MVC like setup. I've been tinkering with my own one, where for me every page just calls a Controller which does the job of loading libraries, header, nav, pages as classes, footer, etc, and each one has a renderer, and then the controller just calls all their renderers and presto, instant page display with all the common code in one place, and each page handles it's own content implementation but still within the framework/structure of the overall site.

Link to comment
Share on other sites

you might want to check this out.

http://www.youtube.com/watch?v=CGiIVQPaOJQ

right now I have each page pass it's name using $_SERVER['PHP_SELF']. Basically every page include a page_setup.php which standardizes how pages are loaded, and delegates all the common rendering and individual page construction to the Controller. It's just a barebones example and just for my own needs, but it helps me keep things organized. index.php
 <?php require_once 'required/core/page_setup.php'; ?>

page_setup.php

 <?phpini_set('display_errors', 'on'); require_once 'required/lib/PHPLibs.php';require_once 'required/core/Controller.php'; $libs = new PHPLibs();$thisPage = $libs->getCurrentPageName($_SERVER['PHP_SELF']);$copyright = $libs->getCopyRight(); $controller = new Controller($thisPage, $copyright, $libs);$controller->renderPageOutput(); ?>

and inside Controller is all the meat and pototoes, which is always under review, constuction, etc and changes slightly per project.

 <?phpdate_default_timezone_set("UTC"); require_once 'required/core/Config.php';require_once 'required/render/common/DocumentEnd.php';require_once 'required/render/common/DocumentHeader.php';require_once 'required/render/common/DocumentNav.php';require_once 'required/render/common/DocumentStart.php';require_once 'required/render/common/Sidebar.php';require_once 'required/render/pages/Home.php'; class Controller{  private $config;  private $docEnd;  private $docHeader;  private $docNav;  private $docStart;  private $page;  private $library;   function __construct($page, $copyright, $libs){	$this->page = $page;	$this->library = $libs;	$this->config = new Config(new Sidebar());	$this->docStart = new DocumentStart();	$this->docEnd = new DocumentEnd($copyright);	$this->docHeader = new DocumentHeader($this->library, $this->config);	$this->docNav = new DocumentNav($this->config, $page);  }   public function renderPageOutput(){	$menu = $this->config->getNavigationMenu();	$pageToRender = $menu[$this->page]["create"]; 	$renderers = array($this->docStart, $this->docHeader, $this->docNav, $pageToRender, $this->docEnd); 	foreach($renderers as $k => $v){	  $v->render();	};  } }; ?>

as you can see more an more pieces start to come into play the deeper you go, but the bottom line is I have one main area for stitching together all my common page elements and the content for each page, using the name and the Config class to provide the correct page Class instances to use. Right now since is the bare bones implementation, I only have Home.php included, but I would create a new class for every page I need to add to the site.

Link to comment
Share on other sites

thats seriously indepth. im honestly learning PHP and getting by with majority of the basics. ill make this a toLEARN list of my things for php. cause there's a lot of stuff for me here to learn. thank you.

Link to comment
Share on other sites

can i put that in a seperate PHP page called title.php or meta.php or something like that? and how would i do nav highlighting ?
yes, just place below in file such as meta.php and include it, just like you did with header.php, and footer.php
 <?php				$title='';		$meta_description = '';		$meta_keywords = ''; if(isset($_GET["page"]) && $_GET["page"] != "home")		{		if(file_exists($_GET["page"].".php"))				{				$pagelink = $_GET['page'];								switch ($pagelink)						{						case "portfolio":								$title='portfolio';								$meta_description = 'This is my portfolio page blah, blah , blah';								$meta_keywords = 'portfolio page blah, blah , blah';						break;						case "another":								$title='This is another page';								$meta_description = 'This is another page blah, blah , blah';								$meta_keywords = 'another page, of whatever';								break;  						case "about-me":								$title='This is another page an IT’S ABOUT ME!';								$meta_description = 'This is another page blah, blah , blah';								$meta_keywords = 'another page, of whatever';								break;  						case "sumbenu2":								$title='This is submenu2 page, not 1, not 3 but 2!';								$meta_description = 'This is submenu2 page, not 1, not 3 but 2';								$meta_keywords = 'This is submenu2 page, not 1, not 3 but 2';								break;				  						}				}		else				{				$title ='Error! page not found';								}		}else		{		$title='Attila Hajzer | Home';		$meta_description = 'Welcome to my portfolio';		$meta_keywords = 'Welcome, home, Attila Hajzer, Attila, Hajzer, web, design, webdesign, web-design, web design';		} echo '<title>'.$title.'</title>'."\n";echo '<meta name="description" content="'.$meta_description.'" />'."\n";echo '<meta name="keywords" content="'.$meta_keywords.'" />'."\n";?> 

With the highlight of of menu, how is this produced manually of dynamically with php? can we see the code for this

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...