Displaying Database Output with PHP
Posted: Mon Jul 24, 2006 9:28 am
I don't know whether this constitutes "Advanced" discussion or not, but here goes...
I'm using a very basic templating system, where I've got an HTML page split into various sections with delimited variables in it.
(really basic) Template File:
I load this into a template class on my PHP page, and process it, swapping in PHP variables for the delimited variables in the HTML template.
Template class: (I haven't included the entire template class - it's methods and attributes are pretty self-explanatory, I've included part of the display() method.)
Typical PHP file:
I am running into problems when I want to put a more complicated data, such as an HTML table, in the #content# area of the template.
I can pull all the data out of the database, and I can also write a script to add <TR> and <TD> tags to the data items, but what do I do when I've got a complicated table layout, for example with multiple rows per row of data returned from the database, or cells that span multiple rows/columns etc.?
My current solution is to create a "pattern" file, which is like the template file, but contains only the HTML necessary to display a single database record, again with delimited variables, like
I then process this pattern for each row of output from the database, and integrate the results into my $template->vars["content"] variable that finally is echoed to the page template.
The advantages are that this appears to have reduced my development times quite significantly, however, the method seems a bit "dirty", in that each table needs an associated "pattern" file. I can't say exactly why, but I'm always left thinking that this isn't the "right" way to do this...
How do you format and display data that comes out of the database in tabular form? Am I missing something really basic (a "forest for the trees" problem?)
Can you suggest any further reading?
Cheers for any help,
GM
I'm using a very basic templating system, where I've got an HTML page split into various sections with delimited variables in it.
(really basic) Template File:
Code: Select all
<HTML>
<HEAD>
#head_items#
</HEAD>
<BODY>
<DIV CLASS="TITLE">#title#</DIV>
<DIV CLASS="CONTENT">#content#</DIV>
</BODY>
</HTML>Template class: (I haven't included the entire template class - it's methods and attributes are pretty self-explanatory, I've included part of the display() method.)
Code: Select all
class template {
var $vars; // contains variable names and values that are substituted into the template
var $html; //contains the complete HTML output
//some more attributes and methods
function display($display_flag) {
// replace each HTML variable with the $this->vars of the same name
foreach($this->vars as $name=>$value) {
$this->html = str_replace("#$name#", $value, $this->html);
}
//if the $display_flag is true, echo the data, else return it.
if($display_flag) {
echo $this->html;
} else {
return $this->html;
}
}
} // template classTypical PHP file:
Code: Select all
//instantiate template object:
$template = new template('template.tmp');
//add stylesheet
$template->add_stylesheet('common.css');
//initialise and populate the variables that will be outputted to the template
$template->add_var("title", "Welcome to my page");
$template->add_var("content", "page content!");
//echo output to the screen
$template->display(true);I can pull all the data out of the database, and I can also write a script to add <TR> and <TD> tags to the data items, but what do I do when I've got a complicated table layout, for example with multiple rows per row of data returned from the database, or cells that span multiple rows/columns etc.?
My current solution is to create a "pattern" file, which is like the template file, but contains only the HTML necessary to display a single database record, again with delimited variables, like
Code: Select all
<TR>
<TD CLASS="LIST_DESCRIPTION"><A HREF="mypage.php?id=#ID_ITEM#">#DE_ITEM#</A></TD>
<TD ROWSPAN="2">#ITEM_COST#</TD>
</TR>
<TR>
<TD CLASS="LIST_DESCRIPTION">#DE_ITEM_LONG#</TD>
</TR>The advantages are that this appears to have reduced my development times quite significantly, however, the method seems a bit "dirty", in that each table needs an associated "pattern" file. I can't say exactly why, but I'm always left thinking that this isn't the "right" way to do this...
How do you format and display data that comes out of the database in tabular form? Am I missing something really basic (a "forest for the trees" problem?)
Can you suggest any further reading?
Cheers for any help,
GM