使用TCPD php的自定义页脚文本

huangapple go评论51阅读模式
英文:

Custom footer text using TCPD php

问题

我尝试在TCPDF页脚中创建自定义文本,并放置已经从数据库中定义的变量$name,但是一直出现错误,提示Notice: Undefined variable: name

public function Footer() {
    // 从底部距离15毫米的位置开始
    $this->SetY(-15);
    // 设置字体
    $this->SetFont('helvetica', 'I', 8);
    
    $fhtml = '
        <table>
        <tr>
        <td>用户:</td><td colspan="10"> ' . $name . '</td>
        </tr>
        </table>';
    
    $this->writeHTML($fhtml, true, false, true, false, '');

}

而这个变量$name已经在以下代码中定义:

$user = mysqli_query($conn, "SELECT * FROM users WHERE users='$users'");
$sqluser = mysqli_fetch_assoc($user);
$name = $sqluser["login"];

如何创建自定义TCPDF页脚?

英文:

I tried to make a custom text in footer TCPD and put variable that i already defined from database which is '$name'. But it keeps get error said 'Notice: Undefined variable: name'.

public function Footer() {
		
        // Position at 15 mm from bottom
        $this-&gt;SetY(-15);
        // Set font
        $this-&gt;SetFont(&#39;helvetica&#39;, &#39;I&#39;, 8);
		
		$fhtml = &#39;
        &lt;table&gt;
		&lt;tr&gt;
		&lt;td&gt;User &lt;/td&gt;&lt;td colspan=&quot;10&quot;&gt;: &#39;.$name.&#39;&lt;/td&gt;
		
	&lt;/tr&gt;
	&lt;/table&gt;
	&#39;;
	
	$this-&gt;writeHTML($fhtml, true, false, true, false, &#39;&#39;);

    }

and this variable that i already defined

$user = mysqli_query($conn,&quot;SELECT * FROM users WHERE users=&#39;$users&#39;&quot;);
$sqluser = mysqli_fetch_assoc($user);
$name = $sqluser[&quot;login&quot;];

How can i make a custom footer TCPDF

答案1

得分: 1

这个问题与TCPDF无关,而与PHP相关。您在类方法的范围内使用了一个全局变量。

将类方法修改为重新定义方法内的$name变量的作用域,如下所示:

public function Footer() {
  global $name
  ....
}
英文:

This issue is not related to TCPDF but related to PHP. You are using a global variable inside the scope of the class method.

Modify the class method to redefine the scope of the $name variable inside the method as follows

public function Footer() {
  global $name
  ....
}

huangapple
  • 本文由 发表于 2023年6月15日 10:30:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定