Displaying Database Output with PHP

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Displaying Database Output with PHP

Post by GM »

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:

Code: Select all

<HTML>
 <HEAD>
  #head_items#
 </HEAD>

 <BODY>
  <DIV CLASS="TITLE">#title#</DIV>
  <DIV CLASS="CONTENT">#content#</DIV>
 </BODY>
</HTML>
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.)

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 class

Typical 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 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

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>
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
alvinphp
Forum Contributor
Posts: 380
Joined: Wed Sep 21, 2005 11:47 am

Post by alvinphp »

If you create your 'pattern' file then you have html that is not in the template which kind of defeats the purpose of the template system. What you need is to have something like this

Code: Select all

#REPEAT_START_STUDENT#
<tr>
<td>#NAME#</td>
<td>#GPA#</td>
</tr>
#REPEAT_END_STUDENT#
Where your parser sees that it needs to cycle this bit of code.

Personally, I would just use native templating. Sure you have a little bit of PHP in the templates, but it is not not much more then the non html special tags you already have in your template. Or you could use an existing template engine like the one in smarty or phplib.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Your going to need a template tag that tells your parser to loop results. In Smarty/TemplateLite, that tag is the {section}{/section} tag pair. In the phpBB Template parser, is the tag <!-- BEGIN loopname -->/<!-- END loopname --> tag pair.

The only way around that would be to read your looped results into a string var and parse that string var in your template.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

Thanks for the responses - I'm quite relieved that the answer was so simple, and not along the lines of "What the hell are you doing???" :wink:

Couple of questions:
alvinphp wrote: Personally, I would just use native templating.
Can you elaborate a little on this - I've heard of smarty, and some other templating engines, but I don't understand your usage of "native" here.
alvinphp wrote: you could use an existing template engine like the one in smarty or phplib
Now where's the fun in that? :)

Thanks again for the responses - I'll have a play with my html_table class to make it read from the same template file as the template class.

Cheers, GM
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Native templating is using PHP as your template 'parser'.

Code: Select all

<?php if (content_available()): if (content()): ?>
<h1><?php content('title'); ?></h1>
<?php while(content('body')): ?>
<p><?php content('linebyline'); ?></p>
<?php endwhile; endif; endif; ?>
alvinphp
Forum Contributor
Posts: 380
Joined: Wed Sep 21, 2005 11:47 am

Post by alvinphp »

Pretty much like Everah stated. PHP can already work as a template system natively. You still have your template page that you load with your control/view page, but your template will have a minimal amount of PHP that inserts data, rows of data, and conditional data (something else you need to look at for your template system). The PHP you will have in your template is about equal to the amount of special tags you are already putting in now.

The downside of native templating is that you need to be disciplined and there is some risk if you have some really dumb graphic designers. Nothing that can't be managed though.
Last edited by alvinphp on Tue Jul 25, 2006 11:12 am, edited 1 time in total.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

I get it now. Thanks for your help guys.
Post Reply