Scooby08 Posted September 18, 2008 Share Posted September 18, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/124748-how-to-get-query-variable-inside-class/ Share on other sites More sharing options...
guyfromfl Posted September 18, 2008 Share Posted September 18, 2008 you need to select the data before you can use it, then you have to pass it,, all this after you create an instance of the class. Quote Link to comment https://forums.phpfreaks.com/topic/124748-how-to-get-query-variable-inside-class/#findComment-644342 Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/124748-how-to-get-query-variable-inside-class/#findComment-644343 Share on other sites More sharing options...
guyfromfl Posted September 18, 2008 Share Posted September 18, 2008 sorry i read your SQL wrong..you have the select. im working on an insert problem so i have insert on my mind...sorry Quote Link to comment https://forums.phpfreaks.com/topic/124748-how-to-get-query-variable-inside-class/#findComment-644344 Share on other sites More sharing options...
Scooby08 Posted September 18, 2008 Author Share Posted September 18, 2008 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?? Quote Link to comment https://forums.phpfreaks.com/topic/124748-how-to-get-query-variable-inside-class/#findComment-644368 Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 It's fairly impossible for me to judge, as I don't know how the class you're extending works or how it is written. Quote Link to comment https://forums.phpfreaks.com/topic/124748-how-to-get-query-variable-inside-class/#findComment-644373 Share on other sites More sharing options...
Scooby08 Posted September 18, 2008 Author Share Posted September 18, 2008 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?? Quote Link to comment https://forums.phpfreaks.com/topic/124748-how-to-get-query-variable-inside-class/#findComment-644416 Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 I works. as I said earlier "the best way" is a different method. Calling the global keyword is sloppy programming, but I suppose if you don't care about all that... Quote Link to comment https://forums.phpfreaks.com/topic/124748-how-to-get-query-variable-inside-class/#findComment-644440 Share on other sites More sharing options...
Scooby08 Posted September 18, 2008 Author Share Posted September 18, 2008 Well I do care, but if this is all I can get to work then I guess I'm stuck with it until I learn me a better way.. Quote Link to comment https://forums.phpfreaks.com/topic/124748-how-to-get-query-variable-inside-class/#findComment-644456 Share on other sites More sharing options...
CroNiX Posted September 18, 2008 Share Posted September 18, 2008 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/124748-how-to-get-query-variable-inside-class/#findComment-644833 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.