使用 tbs 将 sql 中的多条记录合并到带有 mergeblock 的单个块

Use tbs to merge multiple records from sql to single block with mergeblock

我正在使用模板 docx 和 tbs 模板引擎根据 sql 中的值生成文档。我想将 sql 中的多个记录动态加载到 table 我在模板 docx 中创建的,它有一个名为 vm.productname 的块。我想动态创建行并打印 sql array.But 中可用的记录数,未能动态加载多个记录。

这就是 template.docx 的样子。

<?php
include_once('../tinybutstrong/tbs_class.php');
include_once('../tinybutstrong/plugins/tbs_plugin_opentbs.php');
//$tbs  = new clsTinyButStrong("##","##");
$tbs    = new clsTinyButStrong;
$tbs->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);
$tbs->LoadTemplate('file_tobe_tested/1_template_defaultdelimiter.docx');

$db_name    ="template";
$db_con     =new PDO("mysql:host=localhost;dbname=template", 'root', '');

$query_get_companyinfo="select * from company where company_name='ABC Sdn Bhd'";
$data_get_company_info=get_data_from_sql_assoc($query_get_companyinfo,$db_con);

$tbs->MergeField('vs.company_name',$data_get_company_info[0]['company_name']);
$tbs->MergeField('vs.company_address_street',$data_get_company_info[0]['company_address_street']);
$tbs->MergeField('vs.company_address_zipcode',$data_get_company_info[0]['company_address_zipcode']);
$tbs->MergeField('vs.company_address_city',$data_get_company_info[0]['company_address_city']);
$tbs->MergeField('vs.company_address_state',$data_get_company_info[0]['company_address_state']);
$tbs->MergeField('vs.company_address_country',$data_get_company_info[0]['company_address_country']);

//assume customer_name is adam

$query_get_customerinfo="select * from customer where customer_name='Adam'";
$data_get_customer_info=get_data_from_sql_assoc($query_get_customerinfo,$db_con);

$tbs->MergeField('vs.customer_name',$data_get_customer_info[0]['customer_name']);
$tbs->MergeField('vs.customer_address_street',$data_get_customer_info[0]['customer_address_street']);
$tbs->MergeField('vs.customer_address_zipcode',$data_get_customer_info[0]['customer_address_zipcode']);
$tbs->MergeField('vs.customer_address_city',$data_get_customer_info[0]['customer_address_city']);
$tbs->MergeField('vs.customer_address_state',$data_get_customer_info[0]['customer_address_state']);
$tbs->MergeField('vs.customer_address_country',$data_get_customer_info[0]['customer_address_country']);
$date = date("Y-m-d");

//get customer_id from $data_get_customer_info
$customer_id=$data_get_customer_info[0]['customer_id'];

$query_get_products ="select product_name from product where customer_id='".$customer_id."'";
$data_get_products  =get_data_from_sql_assoc($query_get_products,$db_con);

$tbs->MergeBlock('vm.product_name',$data_get_products);
//$TBS->MergeBlock('blk_res',$result);

$output_file        ='output_1'.$date.'.docx';
$tbs->Show(OPENTBS_FILE,$output_file);

print "<a href=\"$output_file\">$output_file</a><br>";

function get_data_from_sql_assoc($query,$db)//fetch associative
    {
        $statement = $db->prepare($query);  
        $statement->execute();
        $data = $statement->fetchAll(PDO::FETCH_ASSOC);
        return $data;
    }
?>

我在执行代码时遇到这个错误

TinyButStrong Error in block's definition [vm.product_name...]: at least one tag corresponding to tr is not found. Check opening tags, closing tags and embedding levels.

提前致谢。

您的模板似乎没问题。

由于拼写和语法检查,内部 XML 可能以不可见的方式拆分。有时也会因为文本中不可见的格式更改或用户对文本的修改太多而拆分。

希望解决方案很简单:

  1. Select TBS 字段的文本(即“[vm.product_name;block=tr]”)
  2. 剪切它,然后使用 paste without formating 剪贴板选项再次粘贴它
  3. Select 再次将拼写和语法设置为 Do not check spelling and grammar

我在模板中将 [vm.product_name;block=tr] 更改为 [vm.product_name;block=tbs:row]

修改后的模板:

在 php 代码中,我将 $tbs->MergeBlock('vm.product_name',$data_get_products); 更改为

$tbs->MergeBlock('vm',$data_get_products);

因为我在vm下还有一个变量,那就是vm.product_price,我把product_price条记录包含在$data_get_products里了。然后,我使用 vm 作为块名称合并块,以便两个变量都将替换为各自的记录。这解决了我的问题。


或者,,* 应该适用于多个记录。但是我没能做到运行.

v3 中的方法 MergeBlock() 增强功能:from this link

  • 现在您可以通过添加 return 完整合并数据的方法 '*' 作为要合并的块名称。

    Example : $data = $TBS->MergeBlock('blk,*','SELECT id FROM table_a');