Jump to content

PHP Page Coding Style


ypkumar

Recommended Posts

Hello guys,

 

I am a beginner in PHP, so please be gentle.

 

I am building this website for a friend of mine. This website is made up by HTML/CSS. i.e., I have a page made with HTML & CSS.

there are a few menus on the page. say Home, About Us, Contact Us, etc.,

 

Now I want to separate the page elements (like header/footer) from the page so as its easy to work on.

the source is something like this

 

 

<HTML>
<HEAD>
	<TITLE>
		My Page
	</TITLE>
	<LINK REL="STYLESHEET" HREF="./mystyle.css" TYPE="text/css" />
</HEAD>

<BODY>
	<DIV ID="wrapper">
		<DIV ID="header">
			<DIV ID="logo">
				<UL ID="menu">
					<LI>
						<A HREF="./index.php"><SPAN>Home</SPAN></A>
					</LI>
					<LI>
						<A HREF="./login.php"><SPAN>Login</SPAN></A>
					</LI>
					<LI>
						<A HREF="./contactus.php"><SPAN>Contact Us</SPAN></A>
					</LI>
				</UL>
				<DIV ID="date">
					<?php echo Date("d M Y");?>
				</DIV>
			</DIV>
		</DIV>
		<DIV ID="bodywrapper">
			<DIV ID="main">
				<DIV ID="content">
					THIS IS A TEST!
				</DIV>
			</DIV>
		</DIV>		
                <DIV ID ="clearfooter"></DIV>
		</DIV>
	<DIV ID="footer">
		© <?php echo date("Y");?>
	</DIV>
</BODY>
</HTML>

 

 

now I will have my content in the DIV called 'content'.

 

now I have separated this one page into three different pages. like this:

 

header.php


<HTML>
<HEAD>
	<TITLE>
		My Page
	</TITLE>
	<LINK REL="STYLESHEET" HREF="./mystyle.css" TYPE="text/css" />
</HEAD>

<BODY>
	<DIV ID="wrapper">
		<DIV ID="header">
			<DIV ID="logo">
				<UL ID="menu">
					<LI>
						<A HREF="./index.php"><SPAN>Home</SPAN></A>
					</LI>
					<LI>
						<A HREF="./login.php"><SPAN>Login</SPAN></A>
					</LI>
					<LI>
						<A HREF="./contactus.php"><SPAN>Contact Us</SPAN></A>
					</LI>
				</UL>
				<DIV ID="date">
					<?php echo Date("d M Y");?>
				</DIV>
			</DIV>
		</DIV>

 

footer.php

	                <DIV ID ="clearfooter"></DIV>
		</DIV>
	<DIV ID="footer">
		© <?php echo date("Y");?>
	</DIV>
</BODY>
</HTML>

 

 

and index.php

	<?php include_once('header.php');?>
		<DIV ID="bodywrapper">
			<DIV ID="main">
				<DIV ID="content">
					THIS IS A TEST
				</DIV>
			</DIV>
		</DIV>		
	<?php include_once('footer.php');?>

 

 

Is this the right way to code? I am worried because I have to keep in mind the safety of the website as I have to include a basic login module to this.

 

is there any other style to write this?

please do let me know..

 

thanks :)

Link to comment
Share on other sites

You can just include the content as well.

 

<?php include_once('header.php');?>
         <div id="bodywrapper">
            <div id="main">
               <div id="content">
                  <?php include_once('content.php');?>
               </div>
            </div>
         </div>      
      <?php include_once('footer.php');?>

 

I feel is a good way to do it.

 

Sometimes would like the page different too, so can break in and out of php and call for different header and content area or styles if would like.

 

<?php
include_once('header.php');
echo"all php code up top";
?>
<div>
html code if need to
</div>
<?php
echo "back into php with some more code";
$variable = 'name your variables but echo it in the html area below';
?>
best practice is to place all html together at the bottom and echo any php into it, or even just a php section that echoes php and html

I guess it all just matters on what need to do with it or how complex it is.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.