Jump to content

how to get query variable inside class


Scooby08

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.