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

Hello,

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:
<?php

    define('HOST','localhost');
    define('USER','root');
    define('PASSWORD','');
   
    define('DATABASE','wrr_v_mh');
   
    require_once 'tbs/tbs_class.php';
    setlocale(LC_TIME, 'german');

    $tmpl = new clsTinyButStrong;
    $tmpl->LoadTemplate('tmpl/test.htm');

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

    mysql_close();
   
function a2($x)
{
  return($x * 2);
}
 
?>

And here is the template test.htm:

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

<body>

<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>
   <thead>
      <tr>
         <th align="left">Benutzer</th>
         <th align="left">Art</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td valign="top">[table_data.benutzer;block=tr]</td>
         <td valign="top">[table_data.art]</td>
      </tr>
   </tbody>
</table>

</body>
</html>

Many thanks for help.

phg
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().
or
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.
See http://www.tinybutstrong.com/manual.php#html_block_prm_ondata