Categories > TinyButStrong general >

Multiple TBS modules (long)

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: TomH
Date: 2006-07-19
Time: 20:56

Multiple TBS modules (long)

continuing from the previous posted code...which now works thanks to Skrol.

I am creating a column which displays the results several TBS modules. The filenames for these modules are maintained in a database (not the actual TBS code). I want to get the list of TBS script filenames and runn the scripts in place.

The following works for the right hand column on the page...
PHP
// =================================================================
// get the   R I G H T   P O S I T I O N modules that are ((not content))                                                                                           
$dbright = new ezSQL_mysql($db_user,$db_password,$db_name,$db_host);
$sql = "select * from $main_table WHERE position='right' and publish = 'all' ORDER BY sequence"; // LIMIT 0,1";
$queryright = $dbright->get_results($sql); // do the basic query
//$TBS->MergeBlock('blk_cat', $db);
$dbright->debug();

$TBS->MergeBlock('blk_mod', $dbright);
HTML
<!-- begin RIGHT BLOCKS  -->
<td  rowspan=99  bgcolor="#F7F7F7"  valign=top width="120">
        <table class=rightblocks width="100%">
        <tr><td><!-- *** [blk_mod.categurl] *** [blk_mod.category] *** id=[blk_mod.id] *** -->
                    [blk_mod.categurl;script=[blk_mod.categurl]; subtpl;block=tr;bmagnet=(td)]
         </td></tr></table>
[var.dummyright;noerr]
</td>
<!-- end RIGHT BLOCKS  -->

This is a good thing -- the block merge of multiple scripts actually processes each script sequentially and correct results render for each of the TBS scripts.

Now, the bad thing... if I attempt to do a similar column on the left side of the page, the main page aborts completely.

The PHP snippet with the two cols looks like this...
<tr bgcolor="#CACACA">
<!--  LEFT COLUMN -->
<td bgcolor="#F7F7F7" valign=top width=100 rowspan=99>
<!--  LEFT MODULES/BLOCKS -->
         <table class=leftblocks width="100%">
        <tr><td><!-- *** [blk_leftmod.categurl] *** [blk_leftmod.category] *** id=[blk_leftmod.id] *** -->
                    [blk_leftmod.categurl; script=[blk_leftmod.categurl]; subtpl; block=tr; ]
         </td></tr>
        </table>
[var.dummyleft;noerr]
  </td>
<!--  MAIN BODY - Middle COLUMN -->
<td valign=top  bgcolor="#F7F7F7" >
          <!--  TOP BLOCKS TABLE-->
<!-- meta_topblocks -->
[onload;file=[var.topblocks]]
<!-- MAIN CONTENT BLOCK -->
<!-- meta_body -or- meta_article -->
[onload;file=[var.body]]
<!-- meta_footer -->
<P />
[onload;file=[var.bottomblocks]]
<!-- meta_footer -->
<P />
[onload;file=[var.footer]]
<!--  END MAIN BLOCK -->
</td>       
<!-- begin RIGHT BLOCKS  -->
<td  rowspan=99  bgcolor="#F7F7F7"  valign=top width="120">
        <table class=rightblocks width="100%">
        <tr><td><!-- *** [blk_mod.categurl] *** [blk_mod.category] *** id=[blk_mod.id] *** -->
                    [blk_mod.categurl;script=[blk_mod.categurl]; subtpl;block=tr;bmagnet=(td)]
         </td></tr></table>
[var.dummyright;noerr]
</td>
<!-- end RIGHT BLOCKS  -->
</tr>
And the snippet HTML with both cols...
<tr bgcolor="#CACACA">
<!--  LEFT COLUMN -->
<td bgcolor="#F7F7F7" valign=top width=100 rowspan=99>
<!--  LEFT MODULES/BLOCKS -->
         <table class=leftblocks width="100%">
        <tr><td><!-- *** [blk_leftmod.categurl] *** [blk_leftmod.category] *** id=[blk_leftmod.id] *** -->
                    [blk_leftmod.categurl; script=[blk_leftmod.categurl]; subtpl; block=tr; ]
         </td></tr>
        </table>
[var.dummyleft;noerr]
  </td>
<!--  MAIN BODY - Middle COLUMN -->
<td valign=top  bgcolor="#F7F7F7" >
          <!--  TOP BLOCKS TABLE-->
<!-- meta_topblocks -->
[onload;file=[var.topblocks]]
<!-- MAIN CONTENT BLOCK -->
<!-- meta_body -or- meta_article -->
[onload;file=[var.body]]
<!-- meta_footer -->
<P />
[onload;file=[var.bottomblocks]]
<!-- meta_footer -->
<P />
[onload;file=[var.footer]]
<!--  END MAIN BLOCK -->
</td>       
<!-- begin RIGHT BLOCKS  -->
<td  rowspan=99  bgcolor="#F7F7F7"  valign=top width="120">
        <table class=rightblocks width="100%">
        <tr><td><!-- *** [blk_mod.categurl] *** [blk_mod.category] *** id=[blk_mod.id] *** -->
                    [blk_mod.categurl;script=[blk_mod.categurl]; subtpl;block=tr;bmagnet=(td)]
         </td></tr></table>
[var.dummyright;noerr]
</td>

<!-- end RIGHT BLOCKS  -->
</tr>

While troubleshooting I discover
(1) if I do either the left col -or- the right col  but not both -- then the result renders correctly for multiple rows in the array.
(2) if I limit the queries to one row each col -- then the left and right cols do render the correct TBS scripts and the content is correct.

So here's what I wonder - since the block(s) work but NOT if the two blocks (left col and right col) have more than one row in the array -- is there something trying to keep track of the block/rows that is getting confused because there are two blocks running?

Or -- much more likely -- am I missing something simple but fundamental about the TBS class?

Many thanks fore any help you can provide,
TomH
By: Skrol29
Date: 2006-07-19
Time: 22:21

Re: Multiple TBS modules (long)

Hi TomH,

You've posted the new HTML snippet twice, but it's not important.
I cannot see what could make this behavior. Maybe someting in the called subscripts. Could you send a piece of them to me ?
By: TomH
Date: 2006-07-19
Time: 22:46

Re: Multiple TBS modules (long)

Ooops..
here's the PHP I omitted...
// =================================================================
// get the  L E F T  P O S I T I O N modules that are ((not content))                                                                                           
//$sqlxxx = "select * from $main_table WHERE type != 'content' and publish = 'all' ORDER BY position,type,category ";
//$sqlxxx = "select * from $main_table WHERE category = 'module' and position='right' and publish = 'all' ORDER BY sequence ";
$sqlzzz = "select * from $main_table WHERE position='left' and publish = 'all' AND (type='module' or type='block') ORDER BY sequence  LIMIT 0,1";
$dbleft = new ezSQL_mysql($db_user,$db_password,$db_name,$db_host);
$queryleft = $dbleft->get_results($sqlzzz); // do the basic query
//$db->debug();

$m=0;
foreach($queryleft AS $module){
   if($module->position=="left"){
// echo "<HR>".$module->id." and \$m=$m<HR>";
    $blk_leftmod[$m]['id']=$module->id;
//    $blk_leftmod[$m]['flag']="Flag=$m";
//    ($module->type=="module") ? $blk_leftmod[$m]['mod_left_flag']="mod" : $blk_leftmod[$m]['mod_left_flag']="block" ;
//    $descript = $module->descript;
    $blk_leftmod[$m]['descript']=$module->descript;
    $blk_leftmod[$m]['categurl']=$module->categurl;
    $blk_leftmod[$m]['category']=$module->category;
    }
$m++;
}
if (is_null($blk_leftmod)) $blk_leftmod = array();
//is_null($blk_leftmod) ? $blk_leftmod = array() : $dummyleft = print_r($blk_leftmod,true);

//$TBS->MergeBlock('blk_leftmod', $blk_leftmod);
$TBS->MergeBlock('blk_leftmod', $blk_leftmod);
//$dummyleft = print_r($blk_leftmod,true);


// == top == user location
$sql3 = "select * from $main_table WHERE type='block' AND publish = '$publish' AND position='top' ";
$query = $db->get_results($sql3); // do the basic query using SQL_CALC_FOUND_ROWS trick
$TBS->MergeBlock('blk_top', $db);
//$db->debug();

/*
// == right == user location
//$category="xxx";
//$script="xxx.php";
$sql4 = "select * from $main_table WHERE type='block' AND publish = '$publish' AND position='right'  ORDER BY category,sequence ";
$query = $db->get_results($sql4); // do the basic query using SQL_CALC_FOUND_ROWS trick
//$db->debug();
$TBS->MergeBlock('blk_right', $db);
*/

// =================================================================
// get the   R I G H T   P O S I T I O N modules that are ((not content))                                                                                           
$dbright = new ezSQL_mysql($db_user,$db_password,$db_name,$db_host);
$sql = "select * from $main_table WHERE position='right' and publish = 'all' ORDER BY sequence  LIMIT 0,1";
$queryright = $dbright->get_results($sql); // do the basic query
//$TBS->MergeBlock('blk_cat', $db);
$dbright->debug();

$TBS->MergeBlock('blk_mod', $dbright);

Examples of the included scripts...
<?php
include_once("../php_inc/tbs30ez21-tbs_ddwork.inc");
$main_table = "index_page";
$user_table = "index_users";

$left_category = 10; // Set the num of chars to be displayed in sidebar output
$left_descript = 10;  // Set the num of chars to be displayed in sidebar output

$TBS = new clsTinyButStrong ;
$this->LoadTemplate("module_newcontent.html");

$sql = "SELECT id, changed, LEFT(descript,$left_descript) as descript from $main_table where publish='all' and type='content' ORDER BY changed DESC ";
$db->get_results($sql);
//$db->debug();
$this->MergeBlock('blk', $db);
$this->Show() ;
?>

<?php
include_once("../php_inc/tbs30ez21-tbs_ddwork.inc");
$main_table = "index_page";
$user_table = "index_users";

$left_category = 10; // Set the num of chars to be displayed in sidebar output
$left_descript = 10;  // Set the num of chars to be displayed in sidebar output

$TBS = new clsTinyButStrong ;
$this->LoadTemplate("module_mainmenu.html") ;

$sql = "select DISTINCT LEFT(category,$left_category) as category from $main_table WHERE type='content' ORDER BY sequence ASC ";

$db->get_results($sql);
//$db->debug();
$this->MergeBlock('blk', $db);
$this->Show() ;
?>

<table width="100%" cellspacing="0" cellpadding="0" border="1">
<tr><td bgcolor="#F2F2F2" align=center>
<?php
include("./calendarclass.inc");
$cal = new Calendar;
echo $cal->getCurrentMonthView();
?>
</td></tr>
</table>

Just a reminder... if only ONE column is processed then ALL of the included scripts run correctly -- that goes for either the left-side scripts and the right-side scripts.
All of the subscripts run correctly in the mains script -- I can test them by simply adding a " LIMIT 0,1", " LIMIT 2,1", " LIMIT 2,1" to the sql's
When there is only ione item in the returned sql object then the problem does not appear and any/all of the subscripts run correctly.

Thanks for helping.


By: Skrol29
Date: 2006-07-19
Time: 23:00

Re: Multiple TBS modules (long)

In the subscripts, I suggest that you replace all
  $TBS = new clsTinyButStrong ;
by :
if (isset($this)) {
  $TBS =& $this;
} else {
  $TBS = new clsTinyButStrong;
}

This should make a difference.
By: TomH
Date: 2006-07-19
Time: 23:46

Re: Multiple TBS modules (long)

There is no difference at all.
By: Skrol29
Date: 2006-07-20
Time: 00:10

Re: Multiple TBS modules (long)

Ok, I see my mistake : you $TBS is not used after being created. So you can delete this line.

I'll start to try to reprodude you problem. When you say that "the main page aborts completely", can can you see exactly ? 

Other questions :
1/ if you merge the two blocks with all records but with parameters "script" disabled (rename it "scriptz=..." for example), deso it work ?
2/ Idem but with only "blk_leftmod" disabled ?
3/ Idem but with only "blk_mod" disabled ?
4/ Still merging all records but having all records with the same value for  the column [categurl] (the goal is to test which subscript can cause a problem when called several times)
By: TomH
Date: 2006-07-20
Time: 02:06

Re: Multiple TBS modules (long)

Thanks you for your very good ideas for testing...
I am working on the several propositions you suggest.
Stay tuned :)
By: TomH
Date: 2006-07-20
Time: 05:48

Re: Multiple TBS modules (long)

Well, I believe I have established the conditions for the unwelcomed behaviour ;), and it now makes a little more sense why it seeme so non-repeatable. Anyway...

When doing like this
<td bgcolor="#F7F7F7" valign=top width=100 rowspan=99>
<!--  LEFT MODULES/BLOCKS -->
        <table class=leftblocks width="100%">
        <tr><td><!-- *** [blk_leftmod.categurl ; block=table] *** [blk_leftmod.category] *** id=[blk_leftmod.id] *** -->
                    [blk_leftmod.categurl; script=[blk_leftmod.categurl]; subtpl]
         </td></tr>
        </table>
  </td>

And the files that are represented by the "categurl" values include _more_than_one_  TBS file that also has within it's own code a repeating block -- the code breaks fatally (no output from TBS)

If the files are PHP files, including php classes like the calendar class BUT without TBS repeating blocks -- then the failure mode does not take effect.

I am packaging now some files to help to find the source of problem.

Oooooops... while preparing some files I decided to create a test case from arrays instead of database -- well, the problem disappeared!!!

So then I createwd a test suite using MySQL but with native TBS to connect -- yes you're correct -- without ezSQL the problem does NOT reproduce.

If you would like to continue to work on this it would be appreciated and I willl package up the files that work using TBS $cnx_id for connection. ANd a set using ezsSQL for connecting.

Very sorry to have been so slow to figure out the (likely) source of the problem.



By: TomH
Date: 2006-07-20
Time: 06:07

Re: Multiple TBS modules (long)

One last thing, all these scripts running

TBS v 3.1.1 with ezSQL 2.03 under Apache 1.2.33 PHP 5.1.4
By: Skrol29
Date: 2006-07-20
Time: 10:25

Re: Multiple TBS modules (long)

Ok, so I need your package to reproduce the problem. No problem, you can send it to me. Please include the ezSQL version you are using and data toi insert by SQL instructions.
By: TomH
Date: 2006-07-20
Time: 17:54

Re: Multiple TBS modules (long)

Okay,

Here it comes...

Tom
By: Skrol29
Date: 2006-07-21
Time: 13:17

Re: Multiple TBS modules (long)

Hi Tom,

Thanks for the package. I think I've found the problem.
In the file "calendartest.php" replace include() by include_once() and all should run better.

Other miscelaneous comments on your scripts (if I may):

- The lines "$TBS = new clsTinyButStrong;" in subscript can be deleted. Creating a TBS class for nothing makes an unuseful process cost.

- The lines "require('./tbs_ddwork_cnx_mysql.inc');" in subscripts makes unuseful MySQL connexions. It could be replaced with include_once().
Maybe you have this lines in order to run the subscript in standalone mode. If it's so, I suggest that you add (isset($this)) conditions, they enable you to know if the script is runing standalone or by a TBS subtemplate.
Also note that the "tbs_ddwork_cnx_mysql.inc" file has an include() command that better be include_once() too.

- In the main script, your are merging blocsk "blk" in the same time that "blk_leftmod" and "blk_right". This is touchy because "blk" is also the name of several blocks in subtemplates. But note that if you have several "blk" blocks to merge at different places, they will be gathered into on after beeing merged.
By: TomH
Date: 2006-07-21
Time: 16:36

Re: Multiple TBS modules (long)

Thank you very, very much for you help on this.

(1) When the calandartest.php is changed then all is okay -- can you explain why that was the thing breaking the script?

(2) When I remove subscript lines $TBS = new... is okay

(3) But when I change  require("./tbs_ddwork_cnx_mysql.inc") or the require("./tbs_ddwork_ezsql.inc") in subscripts:  NOT okay, script breaks

(4) And when in the scripts tbs_ddwork_cnx_mysql.inc ortbs_ddwork_ezsql.inc if I change from include() to include_once() again: NOT okay, script breaks

(5) About your comment "when merging blocsk 'blk' in the same..."should the blocks be always named unique even in subscripts?  is that more safe? or more efficient?

(6) Thank you very, very much for you help on this.

Sincerely,
TomH
By: Skrol29
Date: 2006-07-21
Time: 17:10

Re: Multiple TBS modules (long)

(3) : It doesn't work only because your MergeBlock() commands in those subscripts are using a $cnx_id variable. But making the connection again just in order to have this local variable existing, that is killing a fly with a bazouka. You could just using a global variable instead (I guess $cnx_id does exist in global, so you just have to insert "globals $cnx_id;"). Or you could just replace $cnx_id by the keywork 'mysql' in MergeBlock() commands.

(4) : Yes, my mistake. This one should stay using "include()".

(5) : Blocks in different subtemplates can have the same names without problem if you get sure that they are merged during the subscript. Other wise you should name them differently in order to be sure that they will be merged as expected. This is just about avoiding bad surprises.
By: TomH
Date: 2006-07-21
Time: 18:38

Re: Multiple TBS modules (long)

Your explanations are very helpful, thanks, I learn a lot from them.

re: (3) when using native TBS $cnx_id for connection (in parent script) I see by testing that the following works
<?php
//include('./tbs_ddwork_cnx_mysql.inc');
//include_once('./tbs_ddwork_cnx_mysql.inc');  // Skrol comment  -TJH- actually even this can be commented OUT

//$TBS = new clsTinyButStrong ; // Skrol comment
$this->LoadTemplate("module_newcontentcnx.html");

$main_table = "index_page";  // makin this global in top include file NOT work when ezSQL is used for connection
$sql = "SELECT * from $main_table where publish='all' and type='content' ORDER BY changed DESC "; //LIMIT 1 ";

//$this->MergeBlock('blk',$cnx_id, $sql);
$this->MergeBlock('blk',mysql, $sql); //  Skrol comment
$this->Show() ;
?>
n.b. there is now NO include at top so (a) there is already a connection and (b) the "mysql" keyword is doing all the work ;-)

When I test with those subscripts using ezSQL it is different - I need the to do the include() to get a connection.

So what does this mean about using ezSQL in these situations - maybe not so good?
By: Skrol29
Date: 2006-07-21
Time: 18:58

Re: Multiple TBS modules (long)

> So what does this mean about using ezSQL in these situations
> - maybe not so good?

Yes it's good. You simply have to make you main ezSQL object (or any other connection ID) beeing reachable from any PHP scope (that is from main script and subscripts as well). This can be done using a global variable for your ezSQL object.

In the connection script:
global $db; // To ensure that $db is global whatever the current scope is.
$db = new ezSQL_mysql(...);
Or:
$GLOBALS['db'] = new ezSQL_mysql(...);

In subscripts:
global $db;
...
$TBS->MergeBlock('zz',$db,...);

Or :
$TBS->MergeBlock('zz',$GLOBALS['db'],...);