Jump to content

Using 1 table (for layout) in all pages


NoUser2U

Recommended Posts

Hi,When creating a site that has the same layout (table) for ALL the pages it contains, what is the correct way to use this table on all the pages?I currently have the following:

<!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>	<title>Home</title>	<link rel="stylesheet" type="text/css" href="tablemain.css" /></head><body><table class="main">	<tr class="header">		<td colspan="100%">HEADER</td>	</tr>	<tr>		<td id="links"><?php include("C:/wamp/www/project_01_login/scripts/links.php"); ?></td>		<td id="inhoud">TABLEBODY</td>	</tr>	<tr class="footer">		<td colspan="100%"><!--FOOTER--> <?php include("C:/wamp/www/project_01_login/scripts/footer.php"); ?></td>	</tr></table></body></html>

Let's say this is the homepage. The site contains a couple of pages that all need to have the same table as layout. I created those pages by copying and pasting the above code for the table, and editing it where necessary (inserting other scripts, adding text in the TABLEBODY cell, etc. etc.). So every single page i created has that exact same code as above, except some minor changes.Now while it was all working etc, i figured out i'd have a problem if i wanted to edit the table, for example inserting a new row and cells. Since ALL the pages should contain the exact same table-layout, i'd have to edit every single page so all tables (and thus, the layouts) match.Isn't there a way to solve this issue? For example, inserting a script or so that contains the table-layout....so everytime i'd want to to change the layout (thus the table), i simply have a single file to change and all tables/layouts of the website change according to that file (so basically the same principle as CSS). It would also make it a lot easier to maintain/edit the table/layout.Thnx in advance!

Link to comment
Share on other sites

One approach is to use a single template document that contains your table and all the other stuff. Links to the document come in the form of query strings. Data that you extract from your $_GET array tells you which auxiliary files or data need to be included in certain locations, much as you are already including your footer and navigation sections. This way, each unique query string results in a unique document.If you do not like the look of links with query strings, or you wish to disguise the mechanism by which these "pages" are generated, you can use .htaccess to rewrite "normal" looking URLs into query strings that are invisible to your users. Like this:1. your user clicks on a link or types in a URL that reads "www.somewhere.com/help.html"2. No file with that name exists.3. URL rewriting transforms the request into something like this: "www.somewhere.com/index.php?f=help"4. index.php extracts the value of f and includes data to construct a help page.5. the user's address bar still reads "www.somewhere.com/help.html"

Link to comment
Share on other sites

Just use what you currently have as the master template, and use links from menu to insert the content you require examplehttp://mypage.com/index.php?page=contactusthen insert code below where you require content to appear, it will get the querystring for page, look to see if file exists, if it does include the page else show error. <?phpif(isset($_GET['page'])) { if(file_exists($_GET['page'].'.php')) { include($_GET['page'].'.php'); } else { echo '<p style="color: red;">Error! page not found : '.$_GET['page'].'php</p>'; } }?>

Link to comment
Share on other sites

Ehhh....thnx both for your replies, i appreciate it much!But i forgot to mention i'm a beginner and you both mention query strings.....i've heard of that term before, but that's about it regarding my knowledge and experience with query strings hehe. (My "website creation" knowledge currently consists of the basics of HTML, PHP and CSS only).Since it's obviously needed to work with query strings, i guess i have to learn more about that. I just wikipediated it, and as far as i understood....it's part of the URL that gets processed by the server (with PHP). So PHP get's the 'command' from parts of the URL and runs a program based on that, but correct me if i'm wrong though.Isn't there a more beginner-friendly approach, or do you guys advise to learn about query strings as of now and use that approach to solve my 'problem'?

Link to comment
Share on other sites

I would not call your task a beginner task. The techniques are easy enough to learn, though. You already know how to include external files. dsonesuk and I both showed you examples of URLs with query strings, and the document you are looking at right now has a query string. And dsonesuk shows you how to extract the data from a query string.Here's more info: http://www.php.net/manual/en/language.variables.external.php

Link to comment
Share on other sites

the query string is the end part of the address http://mypage.com/index.php?page=contactusphp $_GET['page'] will retrieve the value passed with the querystring '?page' ie 'contactus';Its not that difficult really, just try a few of the tutorials to pass values from one page to another to get use to them.
Thanks a lot for the clarification!!! I have a book and have read dozens of tutorials about POST and GET methods....but none could explain it so simply. I also just tested the GET-method out using a simple form and i see querie strings for the first time in my URL and understand what they are!Thnx a lot for your post...:)
One approach is to use a single template document that contains your table and all the other stuff. Links to the document come in the form of query strings. Data that you extract from your $_GET array tells you which auxiliary files or data need to be included in certain locations, much as you are already including your footer and navigation sections. This way, each unique query string results in a unique document.If you do not like the look of links with query strings, or you wish to disguise the mechanism by which these "pages" are generated, you can use .htaccess to rewrite "normal" looking URLs into query strings that are invisible to your users. Like this:1. your user clicks on a link or types in a URL that reads "www.somewhere.com/help.html"2. No file with that name exists.3. URL rewriting transforms the request into something like this: "www.somewhere.com/index.php?f=help"4. index.php extracts the value of f and includes data to construct a help page.5. the user's address bar still reads "www.somewhere.com/help.html"
I've reread your post about queries and it makes much more sense now, now i have a very basic understanding of queries. Thank you for pointing this method out. I still won't be able to directy use it because i it's simply over my level now....but i sure will keep your idea in mind and start to look more info about queries and practice with it more!So GET is used when passing values by the URL by querie strings. I have a question about it though. Let's say i use queries and have the following URL (based on your example):3. "www.somewhere.com/index.php?f=help"4. index.php extracts the value of f and includes data to construct a help page.Does step 4 mean that index.php "constructs" a help page by simply including a script that contains the info for the help page???For example:
<body><table class="main">	<tr class="header">		<td colspan="100%">HEADER</td>	</tr>	<tr>		<td id="links"><?php include("C:/wamp/www/project_01_login/scripts/links.php"); ?></td>		<td id="inhoud">TABLEBODY 						<?php 							if(isset($_GET['f'])){								include('help.php');							} else {								echo "Page \"Help\" not found!";							}						?>		</td>	</tr>	<tr class="footer">		<td colspan="100%"><!--FOOTER--> <?php include("C:/wamp/www/project_01_login/scripts/footer.php"); ?></td>	</tr></table></body>

So all the time i have 1 html file (for example: index.html) and it gets 'refreshed' everytime a link is selected while also gets some kind of script inserted corresponding to the link a users clicked on?

Link to comment
Share on other sites

It will have to be a php (index.php) page to process the value/s passed through querystring.not quite right! you are defeating the object by inserting the the page name manually it would be<?php if(isset($_GET['f'])){ include($_GET['f'].'.php'); /* if f value = 'help' it would produce an equivalent to include('help.php'); } else { echo "Page \"Help\" not found!"; }?>the other problem is, someone could have bookmarked an now non existent page, or entered in wrong manually the address and querystring, and because the value has been set, it would try to find the document in question, the else condition would not take affect, that's why i used the check if file exist condition, rather than if querystring f with value used or set. yes! that is basically what it would do, but a few things to remember, the content to be inserted wont have anything but the content BETWEEN the <body> ....</body> tags, it must not include body tags as they will inside the master page itself.

Link to comment
Share on other sites

dsonesuk is correct. The point of the technique is to be able to send many query strings, like?f=help?f=about?f=recipesetc. Now when you follow dsonesuk's technique, you will automagically include a different file for each query string. Obviously, those files must exist for that to work. :)And if there are other little things that must happen, depending on the query string, you would do those also. But to master the techniques, I would practice with a simple include in one spot only, as you illustrated.

Link to comment
Share on other sites

Regarding dsonesuk's remarks on content, I imagine you've discovered that you need much less than what would fall between your body tags. You need only the HTML required to fill your table cell, same as you are probably doing for your footer file.Oh, yes. You will probably need the index file (or wherever the script runs) to have a .php extension, not html, since most servers are configured by default NOT to parse PHP in .html files. The setting that controls that behavior can be changed, but there is no reason to bother.

Link to comment
Share on other sites

It will have to be a php (index.php) page to process the value/s passed through querystring.not quite right! you are defeating the object by inserting the the page name manually it would be<?php if(isset($_GET['f'])){ include($_GET['f'].'.php'); /* if f value = 'help' it would produce an equivalent to include('help.php'); } else { echo "Page \"Help\" not found!"; }?>
Wow amazing!, i didn't know that was possible, using $_GET['f'] and concatenating inside an include() statement. Thnx for pointing it up. I hadn't thought about including the page automatically....so also thanks for pointing that out, and
...the other problem is, someone could have bookmarked an now non existent page, or entered in wrong manually the address and querystring, and because the value has been set, it would try to find the document in question, the else condition would not take affect, that's why i used the check if file exist condition, rather than if querystring f with value used or set.
Thank you for pointing that out....i hadn't thought about that too!
dsonesuk is correct. The point of the technique is to be able to send many query strings, like?f=help?f=about?f=recipesetc. Now when you follow dsonesuk's technique, you will automagically include a different file for each query string. Obviously, those files must exist for that to work. :)And if there are other little things that must happen, depending on the query string, you would do those also. But to master the techniques, I would practice with a simple include in one spot only, as you illustrated.
About the last sentence: So for example, let's say when the user clicks the "Help" link, the script inside uses the querie string to "generate" the "Help"-page. Let's say at the SAME time i want the page title to change from "Home" to "Help", then i would use this for example?:
<html><head><title>	<?php	if(isset($_GET['f'])){	   // check if file exists condition here 		if(file_exists($_GET['f'].'.php')){			echo $_GET['f']; // if f='Help' then the title of page will be Help		} else {			echo "ERROR";		}	}	?>	</title></head><body><table class="main">	<tr class="header">		<td colspan="100%">HEADER</td>	</tr>	<tr>		<td id="links"><?php include("C:/wamp/www/project_01_login/scripts/links.php"); ?></td>		<td id="inhoud">TABLEBODY						<?php						 if(isset($_GET['f'])){						 // check if file exists condition here 				if(file_exists($_GET['f'].'.php')){					include($_GET['f'].'.php');				} else {					echo "Page \"Help\" not found!";				}			}						?>		</td>	</tr>	<tr class="footer">		<td colspan="100%"><!--FOOTER--> <?php include("C:/wamp/www/project_01_login/scripts/footer.php"); ?></td>	</tr></table></body></html>

Link to comment
Share on other sites

It will work, but it's not efficient. It might be more efficient to remove your script from the HTML. Just put it on top. Something like this, which I'm deriving from your own code:

<?php   if(isset($_GET['f'])){	  $file = "";	  if (file_exists($_GET['f'] . '.php') ) {		 $file = $_GET['f'];	  }   }?>

Now you can embed mini-scripts in your HTML, like so:

<title>   <?php echo $file; ?></title>...<?php   if ($file != "") {	  include($file . '.php');   }   else {	  echo $_GET['f'] . " not found!";   }?>

Like I said, this is mostly your code. I'm just suggesting ways of breaking it into more efficient pieces, so we're not calling the same routine more than once. (I have not tested this, but it looks pretty close.)

Link to comment
Share on other sites

or you could use a switch to give you more control of title and meta tags tied to specific page value

<?php $title=""; $meta_description =""; $meta_keywords="";$pagelink ="";  if(isset($_GET['page']))	 {	 if(file_exists($_GET['page'].'.php'))		 {		 $pagelink = $_GET['page'];		 		 switch ($pagelink)			 {			 case "gallery":				 $title='This is gallery page';				 $meta_description = 'This is my gallery page with image i have gathered blah, blah , blah';				 $meta_keywords = 'gallery, images, of whatever';			 break;			 case "help:				 $title='This is help page';				 $meta_description = 'This is a help page blah, blah , blah';				 $meta_keywords = 'help page, of whatever';				 break;				 case "another":				  $title='This is another page';				  $meta_description = 'This is another page blah, blah , blah';				  $meta_keywords = 'another page, of whatever';				  break;   			 }		 }	 else		 {		 $title ='Error! page not found</p>';		 }	 }  echo '<title>'.$title.'</title>'."\n"; echo '<meta name="description" content="'.$meta_description.'" />'."\n"; echo '<meta name="keywords" content="'.$meta_keywords.'" />'."\n"; ?>

then use dd method <td id="inhoud"><?php if ($pagelink != "") { include($pagelink . '.php'); } else { echo '<p style="color: red;">Error! '.$_GET['page].'php page not found</p>'; }?></td>

Link to comment
Share on other sites

Thnx both for your replies, they really help A LOT! I can understand both the methods you guys proposed and i think i can get it working once i start with it, but there is a small problem which i have to deal with first:I was practicing with the GET method today, and by using forms with the <input> element & a submit button i can pass values by the method to an other page.BUT...then i tried to create anchors with the <a> elements, for the 'links' script. I was practicing with this .php file:

// for the record, this is a .php file :)<head><title>	<?php	if(isset($_GET['page'])){		if(file_exists($_GET['page'] . '.php') == 1){			echo $_GET['page'];		} else {			echo "ERROR: file does not exist!";		}	}	?>	</title><link rel="stylesheet" type="text/css" href="tablemain.css" /></head><body><table class="main">	<tr class="header">		<td colspan="100%">HEADER<!-- <img border="0" src="mycal.jpg"> --></td>	</tr>	<tr>		<td id="links"><?php include("C:/wamp/www/gettest/scripts/links.php"); ?></td>		<td id="body">INHOUD</td>	</tr>	<tr class="footer">		<td colspan="100%"><!--FOOTER--> <?php include("C:/wamp/www/gettest/scripts/footer.php"); ?></td>	</tr></table></body></html>

The links.php file contains the following code:

<form method="GET" action=""><a href="">Home</a><br /><a href="">Register</a><br /><a href="">Login</a><br /><a href="">Help</a><br /></form>

I use the method=GET to pass the values to the previes main table in order to let the script change the title of the page. But it doesn't work though. I tried assigning a name="page", id="page", value="page" to each of above anchors, but none seemed to work for the GET method. (previously, i had a link assigned to the href attribute, but that directed the user straight to an other page, without the use of a script, so that's why i left it black right now, though i think that is not the way to do it either).So my goal is to have a few words (like Home, Register, Login and Help), assign them a name and get that passed by the GET-method to a script. For example, the following code i can easily work with:

<form method="GET" action="">Input name: <input type="text" name="user_input">Submit? <input type="submit" value="Send!"></method>....<?phpecho $_GET['user_input'];?>

But do i also have to use the <form> element in order to pass the 'name' of an <a> element? For example: Let's say i have a link called "Help". How can i let the file pass the value "Help" to a script or other page after the user clicks the "Help"-link? I thought this was the correct way:

<form method="GET" action=""><a href="" name="page">Help</a></form>....<?phpecho $_GET['page'];?>

But obviously, that doesn't work. How do i do this?

Link to comment
Share on other sites

name attributes serve a different purpose altogether. We pass GET data in the href or URL. It has to be all there, not in some other part of the <a> tag. Your href attributes will need to look something like this:<a href="index.php?page=Help">or even this:<a href="./?page=Help">. . . if in fact the processing document is named index.phpEDIT. I hate it when this happens. :)

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...