user2430278
user2430278

Reputation: 17

How to print a value through inline php in dompdf

My query in inline PHP:

$sql='select * from tbl_1 where id=1';
$res=mysql_query($sql);
$row=mysql_fetch_assoc($res);
$some_value = $row['s_code'];

$text = $some_value;
$pdf->text($w / 1.2 - $width / 2,40, $text, $font, $size, $color);(this is a piece of code from footer)

It is not working. what is the wrong with this code? Thanks in advance.

Upvotes: 0

Views: 1365

Answers (1)

BrianS
BrianS

Reputation: 13944

I'm not sure your sample code is all part of the inline script or if you picked code from different parts of the process. I'm guessing you tried to simplify your sample for brevity. So I'm assuming your process is something along the lines of the following.

Your PHP script:

$sql='select * from tbl_1 where id=1';
$res=mysql_query($sql);
$row=mysql_fetch_assoc($res);
$some_value = $row['s_code'];
$dopmdf = new DOMPDF;
// etc.

Your HTML document:

<html><body>
<script type="text/php">
// populate $w, $width, $font, $size, $color, then ...
$text = $some_value;
$pdf->text($w / 1.2 - $width / 2,40, $text, $font, $size, $color);
</script>
<p>some text</p>
...
</body></html>

Inline script is run in a different context than code from your main PHP script. So if that's what you're doing you'll need to use the $GLOBALS variable instead of direct reference, i.e.

Your HTML document:

<html><body>
<script type="text/php">
// populate $w, $width, $font, $size, $color, then ...
$pdf->text($w / 1.2 - $width / 2,40, $GLOBALS['some_value'], $font, $size, $color);
</script>
<p>some text</p>
...
</body></html>

(If you fill out your sample code I'll update the answer.)

Upvotes: 1

Related Questions