Jump to content

Separting Logic From Design, Ideas For Outputting


sn2005

Recommended Posts

Greetings,

 

I'm working on a small website with common features (login, members area, contact us, etc.) and using PHP to do the coding and MySQL as the database to store info. I've tried to separate the php code and all the functions and logic into php files and then have all the html code in separate files. I'll provide an example below of how I've done it so far.

 

contact.php

<?php
session_start();

// If user is logged in, redirect them to the overview page
if($_SESSION['ID']){ header('Location: main.php'); }

include("classes/Output.class.php");
$page = new Page;

// Output the HTML for the file
$page->title = "Contact us";
$page->meta_keywords = "main overview stats";
$page->cssLink = "css/style.css";
$page->cssLinkIE = "css/ie.css";
$page->jsLink = array("js/jquery-1.8.3.js", "js/ajaxlogin.js");
$page->path = "./html/contact.html.php";
$page->displayPage($page->path);
?>

 

So in my contact.php and other php files I call upon the Output class to output all of the data. Examples of the Output class are below

 

Output.class.php

 

<?php
class Page
{
public $title;
public $meta_keywords;
public $cssLink;
public $cssLinkIE;
public $jsLink;
public $body;
public $path;

public function displayHeader()
{
$output ='
<!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>'.$this->title.'</title>
<meta name="keywords" content="'.$this->meta_keywords.'" />
<link rel="stylesheet" href="'.$this->cssLink.'" type="text/css">';
//<!--[if IE]><link rel="stylesheet" href="'.$this->cssLinkIE.'" type="text/css" media="all" /><![endif]-->

for($i=0;$i<count($this->jsLink);$i++)
{
$output.= '
<script type="text/javascript" src="'.$this->jsLink[$i].'"></script>';
}

$output.= '
</head>

';
echo $output;
}

public function displayBody ($path, $var1=NULL, $var2=NULL)
{
include($path);
}

public function displayFooter ()
{
$output = '

 <div id="footer">
 </div>
</div>
</div>
</body>
</html>';
echo $output;
}


public function displayPage($path, $var1=NULL, $var2=NULL)
{
$this->displayHeader();
$this->displayBody($path, $var1, $var2);
$this->displayFooter();
}
}
?>

 

In my Output class, I have functions to output the header, main body and the footer. Then all these three functions are run within one main function named displayPage, which I call from my contact.php file: $page->displayPage($page->path); where the path is the file that contains the main body of the page, in this case the path refers to /html/contact.html.php.

 

contact.html.php

<body>
<div id="wrapper">
<div id="container">
<?php include("./menu.login.php"); ?>

 <div id="content">
 <img src="images/contactlogo.png" class="center">
 <p1>If you have any problems please contact us at [email removed]</p1>
<br/><br/>

<form id="contact" method="post" action="contact.php">
<table>
<tr>
<td class="twocolright">Your Email</td>
<td class="twocolleft"><input type="text" name="email" id="email" class="reginfo" /></td>
</tr>
<tr>
<td class="twocolright">Subject</td>
<td class="twocolleft"><input type="text" name="subject" id="subject" class="reginfo" /></td>
</tr>
<tr>
<td class="twocolright">Message</td>
<td class="twocolleft"><textarea name="msg" id="msg" class="msgbox" /></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="msgbtn" id="msgbtn" value="" class="center" /></td>
</tr>
 </table>
</form>
 </div>

 

So essentially what I'm trying to do is do all the coding/functions in the php and class files. Then call upon the Output class to handle the output of the page and store the core html body in a separate file/folder, away from the php code.

 

What I'm curious about is, is the idea I'm using above a good way of separating the logic from the output and if you guys have any suggestions on what I could do to improve this or a different way of approaching this. Any insight would be appreciated. Thanks for your time :)

 

Regards,

SN2005

 

Edit: Some of the code is not being tabbed correctly, forgive any spacing/tab issues

Edited by sn2005
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.