Categories > TinyButStrong general >

MergeField with userfunction

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: phg
Date: 2012-10-22
Time: 17:20

MergeField with userfunction


I have a problem understanding MergeField with a MySQL-query and a userfunction.

I have a database which contains records with fields, which I want to modify before output.

May be, I am on a total wrong way. I think, can use a function, which makes the modification.

First question: Must I call the MergeField-call before or after the MergeBlock-Call?

Second question: What is the correct syntax calling my function?

I looked for e complete example, but I dont find one.

Here is my code:

    require_once 'tbs/tbs_class.php';
    setlocale(LC_TIME, 'german');

    $tmpl = new clsTinyButStrong;

    // DATABASE variables and query
    mysql_connect(HOST, USER, PASSWORD);
    $select = 'SELECT benutzer,art FROM wrr_benutzer WHERE art = 3';
    $tmpl->MergeBlock('table_data', 'mysql', $select);
    $tmpl->MergeField('table_data', 'a2', true);

function a2($x)
  return($x * 2);

And here is the template test.htm:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "">
   <title>MySQL data in a table using TBS and MERGEBLOCK</title>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />


<table border="1" width="70%" summary="MySQL data in a table using TBS and MERGEBLOCK">
   <caption>MySQL data in a table using TBS and MERGEBLOCK</caption>
         <th align="left">Benutzer</th>
         <th align="left">Art</th>
         <td valign="top">[table_data.benutzer;block=tr]</td>
         <td valign="top">[]</td>


Many thanks for help.

By: Skrol29
Date: 2012-10-23
Time: 02:57

Re: MergeField with userfunction

Hi phg,

In your snippet, the line with MergeField() will have no effect because there is no "table_data" fields left in the template. They all were already merged by MergeBlock().

If you want to catch the data before to merge them. They are two possibilities.

1) Fetch yourself the SQL data in a php variable such as $data. Change the data. Then merge the variable with a MergeBlock().
2) Use parameter "ondata" on the block. This enables you to define a custom PHP function that can read and modify each records before it is merged.