I've been searching for easier method for generating fpdf report based on selected columns from mysql. Example:
_________________________________________________________
|NO |NAME | COMPANY | ADDRESS | PHONE |
|-------------------------------------------------------|
| 1 | Andrew | AT&T | 123 Street | 123456 |
| 2 | Darwin | Verizon | 888 Road | 222222 |
|___|________|_____________|_______________|____________|
I've made forms that include checkboxes so user can select which columns he/she wants to generate like :
[] Name
[] Company
[] Address
[] Phone
Now here's my initial code if i want to display Name and Company :
if(isset($_POST['Name'])){
if(isset($_POST['Company'])){
(I copied the code below and pasted here, then I added:
$this->Cell(6,1,'Company','TB',0,'L',1); in //HEADER
$cell[$i][2]=$d[2]; in //ARRAY
$pdf->Cell(6,1,$cell[$j][1],'B',0,'L'); in //PDF
)
}
class PDF extends FPDF{
//HEADER
function Header(){
$this->SetTextColor(128,0,0);
$this->SetFont('Arial','B','8');
$this->SetFont('Arial','B','7');
$this->SetFillColor(192,192,192);
$this->SetTextColor(0,0,0);
$this->Cell(1,1,'No','TB',0,'L',1);
$this->Cell(5,1,'Name','TB',0,'L',1);
$this->Ln();
}
}
$net = new mysqli($server, $user, $pass, $data);
if($net->connect_error){
die("Connection: ".$net->connect_error);
}
$q = "select * from people where name between 'Andrew' and 'Darwin'";
$h = $net->query($q) or die($net->error);
$i = 0;
//ARRAY
while($d=$h->fetch_array()){
$cell[$i][0]=$d[0];
$cell[$i][1]=$d[1];
$i++;
}
//PDF
$pdf = new PDF('L','cm','A4');
$pdf->Open();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','','6');
for($j=0;$j<$i;$j++){
$pdf->Cell(1,1,$j+1,'B',0,'L');
$pdf->Cell(5,1,$cell[$j][0],'B',0,'L');
$pdf->Ln();
}
$pdf->Output();
}
The output will be like:
____________________________
|NO |NAME | COMPANY |
|--------------------------|
| 1 | Andrew | AT&T |
| 2 | Darwin | Verizon |
|___|________|_____________|
As you can see I'm using nested if method, I would like to know if there is any efficient and easier way to do this, because I have more than 10 columns in my real table, which is so much pain if i still use nested if.
Thanks in advance.
Not quite sure what you are asking but it might be this:
for(loop){
if (isset($_POST['Name'])) {
$pdf->Cell(Name);
}
if (isset($_POST['Company'])) {
$pdf->Cell(Company);
}
if (isset($_POST['Address'])) {
$pdf->Cell(Address);
}
etc etc
$pdf->Ln();
}