简体   繁体   English

PHPWord动态表行

[英]Dynamic Table Rows with PHPWord

I am hoping someone can help me because I am attempting to do something that is beyond my limits, I don't even know if a function exists for this within PHP or MySQL so my search on google hasn't been very productive. 我希望有人可以帮助我,因为我试图做一些超出我极限的事情,我什至不知道PHP或MySQL中是否存在针对此功能的函数,因此我在Google上的搜索效率不高。

I am using PHPWord with my PHP/MySql Project, the intention is that I want to create a word document based on a template. 我在我的PHP / MySql项目中使用PHPWord,其目的是要基于模板创建Word文档。

I have used this guide which is also on stack exchange. 我使用了本指南 ,该指南也在堆栈交换中。

However this approach requires that the number of rows and the values are hard coded, ie in his example he has used cloneRow('first_name', 3), which then clones the table to have 3 rows, and then goes on to manually define the tags, ie 但是,此方法要求对行数和值进行硬编码,即在他的示例中,他使用了cloneRow('first_name',3),然后将表克隆为3行,然后手动定义标签,即

$doc->setValue('first_name#1', 'Jeroen');
$doc->setValue('last_name#1', 'Moors');
$doc->setValue('first_name#2', 'John');

I am trying to make this dynamic, in my instance I am trying to make a timetable, and one of the child tables is exactly that, so the query I have looks up how many entries there are and then collects a count of them, this $count is then used to dynamically create the correct number of rows. 我正在尝试使它动态化,在我的情况下,我正在尝试制定一个时间表,而其中一个子表正是该时间表,因此我查询的内容是查找有多少条目,然后收集它们的数量,然后,使用$ count动态创建正确的行数。 This is the count I am using: 这是我正在使用的计数:

$rs10 = CustomQuery("select count(*) as count FROM auditplanevents where AuditModuleFk='" . $result["AuditModulePk"]."'"); 
$data10 = db_fetch_array($rs10);
$Count = $data10["count"];

I then use this $document->cloneRow('date', $Count); 然后,我使用此$document->cloneRow('date', $Count); to executive the clonerow function, which works great and my document now looks something like this . 来执行clonerow功能,效果很好,我的文档现在看起来像这样

So, so far so good. 所以,到目前为止一切顺利。

What I now want is for a way to then append each row value of the query into the document, so rather than manually setting the tag value ie $doc->setValue('first_name#1', 'Jeroen'); 我现在想要的是一种将查询的每个行值附加到文档中的方法,而不是手动设置标签值,即$doc->setValue('first_name#1', 'Jeroen'); I could use something like $doc->setValue('first_name#1', '$name from row 1'); 我可以使用类似$doc->setValue('first_name#1', '$name from row 1'); I suspect this will involve a foreach query but not too sure. 我怀疑这将涉及一个foreach查询,但不太确定。

I hope the above makes sense, but please feel free to ask me for anything else and become my personal hero. 我希望以上所述是有道理的,但请随时向我询问其他事项,并成为我的个人英雄。 Thanks 谢谢

Update: Just for sake of clarity, what I would like is for the output to look something like this: 更新:为了清楚起见,我想要的是输出看起来像这样:

In my example are 5 results and therefore 5 rows created, I want to set values in following way: 在我的示例中,有5个结果,因此创建了5行,我想通过以下方式设置值:

${$date1} = date column from query 1st row
${$date2} = date column from query 2nd row
${$date3} = date column from query 3rd row
${$date4} = date column from query 4th row
${$date5} = date column from query 5th row

I was able to sort this out by inserting the records from the query into a temp table, with an AI ID, then using: 通过将查询中的记录插入具有AI ID的临时表中,然后使用以下命令,我能够解决此问题:

//update timetable with events from temp table
$rs14 = CustomQuery("select *  FROM tempauditplan where AuditModuleFk='" . $result["AuditModulePk"]."'"); 
while ($data14 = db_fetch_array($rs14))
    {
        $document->setValue('date#'.$data14["rowid"], date('d/m/y', strtotime($data14["date"])));
        $document->setValue('time#'.$data14["rowid"], date('H:i', strtotime($data14["time"])));
        $document->setValue('auditor#'.$data14["rowid"], $data14["auditor"]);
        $document->setValue('area#'.$data14["rowid"], $data14["area"]);
        $document->setValue('notes#'.$data14["rowid"], $data14["notes"]);
        $document->setValue('contact#'.$data14["rowid"], $data14["contact"]);
    }

The trick is to also have a function that truncates the table after use so can be used over again 诀窍是还具有在使用后将表截断的功能,因此可以再次使用

Might not be the most efficient way, but it works! 可能不是最有效的方法,但是它有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM