Jump to content

Recommended Posts

I'm having troubles getting a variable inside of a class.. Here is the code..

 

<?php
$q  = "SELECT * FROM dw_company";
$r = mysql_query($q) or die(mysql_error());
while($row = mysql_fetch_array($r)) {
$company_name = $row['company_name'];
}

class PDF extends FPDF {
function Header() {
	$this->Cell(0,10,$company_name,0,1,'C',0);
}
}
?>

 

How can I get $company_name to work inside of the class?

 

Link to comment
https://forums.phpfreaks.com/topic/124748-how-to-get-query-variable-inside-class/
Share on other sites

the best way (one of them at least)? pass it as an argument, such as to the Header() method.... or to the constructor.

 

<?php
$q  = "SELECT * FROM dw_company";
$r = mysql_query($q) or die(mysql_error());
while($row = mysql_fetch_array($r)) {
$company_name = $row['company_name'];

$pdf = new PDF();
$pdf->Header($company_name);
}

class PDF extends FPDF {
public function Header($company_name) {
	$this->Cell(0,10,$company_name,0,1,'C',0);
}
}
?>

 

granted I have no idea what you're trying to do here, so you may not want to create a new object in there, and you may want to do it differently, but that's one way of doing it.

No problem guyfromfl.. Thanks for the response...

 

genericnumber1, I have tried your code and it would probably work, but for this code it needs to be in this fashion.. This code spits out the word "test" on the pdf..

 

<?php
$q  = "SELECT * FROM dw_company";
$r = mysql_query($q) or die(mysql_error());
while($row = mysql_fetch_array($r)) {
$company_name = $row['company_name'];
}

class PDF extends FPDF {
function Header() {
	$this->SetFont('Arial','B',10);
	$this->Cell(0,10,'test',0,1,'C',0);
}
}

$pdf=new PDF();
$pdf->Output();
?>

 

new PDF() automatically calls the header here, so how would I arrange it now?? Sorry I didnt post this in the first place..  I have tried this:

 

<?php
$q  = "SELECT * FROM dw_company";
$r = mysql_query($q) or die(mysql_error());
while($row = mysql_fetch_array($r)) {
$company_name = $row['company_name'];
}

class PDF extends FPDF {
public function Header($company_name) {
	$this->SetFont('Arial','B',10);
	$this->Cell(0,10,$company_name,0,1,'C',0);
}
}

$pdf=new PDF();
$pdf->Output();
?>

 

but its still a bit off.. What am I missing??

No problem.. I appreciate the attempt.. However I did try global like so and it seemed to work:

 

<?php
$q  = "SELECT * FROM dw_company";
$r = mysql_query($q) or die(mysql_error());
while($row = mysql_fetch_array($r)) {
$company_name = $row['company_name'];
}

class PDF extends FPDF {
function Header() {

                global $company_name;

                $this->SetFont('Arial','',9);
	$this->Cell(0,10,$company_name,0,1,'C',0);
}
}
?>

 

Would there be anything wrong with doing it that way??

<?php
class PDF extends FPDF
{
    public $header;

    public function Header()
    {

        //Title
        $this->Cell(60,10,$this->header,0,1,'C');
    
    }
    public function setHeader($header)
    {
        $this->header = $header;
    }
}
?>

 

Then:

<?php
$header = "This is from my SQL";
$pdf  = new PDF('P','mm','Letter');
$pdf->setHeader($header);
?>
//...

 

It should work as its from some work that I have done before, but I stripped a lot out for simplicity.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.