Categories > TinyButStrong general >

Problem putting dynamic data into Sub-Template

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: TREVOR
Date: 2006-12-27
Time: 14:02

Problem putting dynamic data into Sub-Template

Please help a newby who cannot get his head round the information in your manual on how to insert PHP code into a sub-template.

data.php 

<?php

include_once('tbs_class.php') ;

// connect to the database
$cnx_id = mysql_connect("humbug", "handsel", "a611a2f74add4d51");
if (!$cnx_id) {
  echo '<p>Unable to connect to the ' .
        'database server at this time.</p>' ;
  exit();
}
mysql_select_db("handsel_pn",$cnx_id);
if (!@mysql_select_db("handsel_pn")) {
  exit('<p>Unable to locate the ' .
        'database at this time.</p>');
}

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate('data.htm') ;
$TBS->MergeBlock('blk',$cnx_id,'SELECT * from STUDS2 ORDER BY sequence') ;
mysql_close($cnx_id) ;
$TBS->Show() ;

?>

together with data.htm give me a nice table showing rows of product images with their associated codes and prices underneath each one.

test1.php

<?php

include_once('tbs_class.php') ;
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate('test1.htm') ;
$TBS->Show() ;

?>

displays test1.htm nicely and I can get my header and menu showing ok. - but I cannot get my dynamic content to display in its cell in the table. I either get an error message in the cell (if I use "onformat") or data.php obliterates everything else an just shows my data (if I use "onshow" or "onload") .

test1.htm (part)

  <table width="750" border="1" cellspacing="0" cellpadding="2">
    <tr valign="top" >
      <td height="40" colspan="2" >[onload;file='test1_header.htm']</td>
    </tr>
    <tr>
      <td valign="top" bgcolor="#B1B1B1">[onformat;script=data.php;subtpl]</td>
      <td width="150" valign="top" bgcolor="#FFCCCC">[onload;file='test1_menu.htm']</td>
    </tr>
    <tr bgcolor="#D1F3D2">
      <td colspan="2" valign="top" bgcolor="#D1F3D2">[onload;file='tbs_us_examples_subtpl_footer.htm']</td>
    </tr>
  </table>

what should I have instead of [onformat;script=data.php;subtpl] 

best wishes...Trevor
By: Skrol29
Date: 2006-12-28
Time: 00:51

Re: Problem putting dynamic data into Sub-Template

Hi,

Instead of [onformat;script=data.php;subtpl], you should have [onload;script=data.php;subtpl] or [onshow;script=data.php;subtpl].
But you script "data.php" is not ok to be a script for subtemplate.

It should be somethink like this :
<?php

if (!isset($this)) {
  // this enables you to test your subtemplate in normal mode
  include_once('tbs_class.php') ;
  $TBS = new clsTinyButStrong ;
}

// connect to the database
$cnx_id = mysql_connect("humbug", "handsel", "a611a2f74add4d51");
if (!$cnx_id) {
  echo '<p>Unable to connect to the ' .
        'database server at this time.</p>' ;
  exit();
}
mysql_select_db("handsel_pn",$cnx_id);
if (!@mysql_select_db("handsel_pn")) {
  exit('<p>Unable to locate the ' .
        'database at this time.</p>');
}

$TBS->LoadTemplate('data.htm') ;
$TBS->MergeBlock('blk',$cnx_id,'SELECT * from STUDS2 ORDER BY sequence') ;
mysql_close($cnx_id) ;
$TBS->Show() ;

?>
By: TREVOR
Date: 2006-12-28
Time: 09:55

Re: Problem putting dynamic data into Sub-Template

Dear Skol29  - Thank you for coming to my help at this time of year.

I have made the changes to my data.php file and it tests OK (as shown at http://ccgi.handsel.plus.com/TEST_TBS/data.php)...(the data is a test table with only the first four images specified.)

but when I replace [onformat;script=data.php;subtpl] in my test1.htm with either [onload;script=data.php;subtpl] or [onshow;script=data.php;subtpl] I now get a completely blank screen!   (see http://ccgi.handsel.plus.com/TEST_TBS/test1.php )
If you go to http://ccgi.handsel.plus.com/TEST_TBS/test2.php you can see where I got stuck originally - with the error message showing in the sub template.

My www.handsel.com website has become too big and complicated to maintain with the "knife & fork" methods I have taught myself so far. TinyButStrong plus MySql should provide me with an elegant and efficient way to upgrade and improve....once I can get these sub-templates to work properly!
Best wishes.....Trevor

By: Skrol29
Date: 2006-12-28
Time: 10:39

Re: Problem putting dynamic data into Sub-Template

Hi Trevor,

The blank page happens when a subscript raises a PHP error that stops the current script. In this case, the error message is unfortunately stored into the TBS buffer which manages the subtemplate.

In you case the error is about the MySQL connection. I guess the main script probably do a connection itself. Or there is maybe another connection problem.

First you can try to replace exit(); with $TBS->Show();
and

exit('<p>Unable to locate the '.'database at this time.</p>');
with
echo('<p>Unable to locate the '.'database at this time.</p>');
$TBS->Show();

You'll probably see the page with the error message.
If, as I belive, you're MySQM connection is also created by the main script, then in the subscript you can move the code for the connection inside the if(!isset($this)) block. And also delete the line with mysql_close($cnx_id);
By: TREVOR
Date: 2006-12-28
Time: 15:17

Re: Problem putting dynamic data into Sub-Template

Dear Skol29 - sorry I am being so stupid with this.... I have changed the subtemplate php file data,php as you have suggested and it happily prints out all my data when tested separately (http://ccgi.handsel.plus.com/TEST_TBS/data.php)

the code is now:-

<?php

if (!isset($this)) {
  // this enables you to test your subtemplate in normal mode
  include_once('tbs_class.php') ;
  $TBS = new clsTinyButStrong ;

// connect to the database
$cnx_id = mysql_connect("humbug", "handsel", "a611a2f74add4d51");
if (!$cnx_id) {
  echo '<p>Unable to connect to the ' .
        'database server at this time.</p>' ;
  $TBS->Show() ;
}
mysql_select_db("handsel_pn",$cnx_id);
if (!@mysql_select_db("handsel_pn")) {
  echo('<p>Unable to locate the ' .
        'database at this time.</p>');
    $TBS->Show() ;       
}
}

$TBS->LoadTemplate('data.htm') ;
$TBS->MergeBlock('blk',$cnx_id,'SELECT * from STUDS2 ORDER BY sequence') ;
$TBS->Show() ;

?>

but there is no connection to the MySql database in the main template php file test1.php :-

<?php

include_once('tbs_class.php') ;
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate('test1.htm') ;
$TBS->Show() ;

?>

nor in the main template htm file test1.htm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test1_Template</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="test1.css" />

</head>
<body>
<div align="center">
  <table width="750" border="1" cellspacing="0" cellpadding="2">
    <tr valign="top" >
      <td height="40" colspan="2" >[onload;file='test1_header.htm']</td>
    </tr>
    <tr>
      <td valign="top" bgcolor="#FFFFFF">[onshow;script=data.php;subtpl] </td>
      <td width="150" valign="top" >[onload;file='test1_menu.htm']</td>
    </tr>
    <tr bgcolor="#D1F3D2">
      <td colspan="2" valign="top" bgcolor="#D1F3D2">[onload;file='test1_footer.htm']</td>
    </tr>
  </table>
  </div>
</body>
</html>

The templates seem to work correctly if I load an html file into the data sub-template ....(go to http://ccgi.handsel.plus.com/TEST_TBS/test2.php)

But I still get a blank screen when I try to put data from MySql in. I must still be doing something wrong in "data.php".

Best wishes....Trevor
By: Skrol29
Date: 2006-12-28
Time: 19:26

Re: Problem putting dynamic data into Sub-Template

Ok, so put your MySQL connection code back to outside the if() block.

Then Neutralize the parameter "subtpl" in the template.
This will enables you to read the error message prompted by PHP before to stop the subscript. But the result will not be correctly arranged.
By: TREVOR
Date: 2006-12-28
Time: 22:53

Re: Problem putting dynamic data into Sub-Template

!!!*** GOT IT! ***!!!

(see http://ccgi.handsel.plus.com/TEST_TBS/test1.php)

the
[onshow;script=data.php;subtpl]
tag is still the same.

But the php file seems to need "$this" instead of "$TBS"

<?php

if (!isset($this)) {
  // this enables you to test your subtemplate in normal mode
  include_once('tbs_class.php') ;
  $TBS = new clsTinyButStrong ;
}
// connect to the database
$cnx_id = mysql_connect("humbug", "handsel", "a611a2f74add4d51");
if (!$cnx_id) {
  echo '<p>Unable to connect to the ' .
        'database server at this time.</p>' ;
  $TBS->Show() ;
}
mysql_select_db("handsel_pn",$cnx_id);
if (!@mysql_select_db("handsel_pn")) {
  echo('<p>Unable to locate the ' .
        'database at this time.</p>');
    $TBS->Show() ;       
}

$this->LoadTemplate('data.htm') ;
$this->MergeBlock('blk',$cnx_id,'SELECT * from STUDS2 ORDER BY sequence') ;
mysql_close($cnx_id) ;
$this->Show() ;

?>

Very pleased with the result - thank you for your help and for your patience.

Best wishes...Trevor
By: Skrol29
Date: 2006-12-28
Time: 23:33

Re: Problem putting dynamic data into Sub-Template

Yes of course !
I did see that :(

The code given in the example is:
if (isset($this)) {
  // We are under the TBS Subtemplate Mode =>
    //   variables are always local, not global,
    //   and the TBS object is referenced by variable $this.
    $TBS =& $this;
} else {
  // This sub-script can also be run under the normal mode =>
  //  its corresponding template will be displayed like a main template.
    include_once('tbs_class.php') ;
    $TBS = new clsTinyButStrong;
}