sql >> Database teknologi >  >> RDS >> Mysql

Grundlæggende tabel oprettelse fpdf

Læser og følger en tutorial på FPDF-webstedet kan være en god start.

Forudsat at du har en tabel (lad os kalde det people ) og eksempeldata som denne

CREATE TABLE People
    (id int, 
     first_name varchar(5), 
     middle_name varchar(4), 
     last_name varchar(5), 
     age int, 
     email varchar(15));

INSERT INTO People
    (id, first_name, middle_name, last_name, age, email)
VALUES
    (1, 'Jhon', NULL, 'Doe', 27, '[email protected]'),
    (2, 'Mark', 'J', 'Lee', 35, '[email protected]'),
    (3, 'Helen', 'P', 'Smith', 30, '[email protected]');

Her er et grundlæggende php-script, der gør, hvad du vil. Bemærk: kode mangler enhver fejlhåndtering for korthedens skyld.

<?php
require('fpdf.php');

class People {
    public function all() {
        try {
            $db = new PDO('mysql:host=localhost;dbname=test;charset=UTF-8', 'user', 'password');
            $query = $db->prepare("SELECT first_name, middle_name, last_name, age, email FROM people ");
            $query->execute();
            $people = $query->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            //echo "Exeption: " .$e->getMessage();
            $result = false;
        }
        $query = null;
        $db = null;
        return $people;        
    }
}

class PeoplePDF extends FPDF {
    // Create basic table
    public function CreateTable($header, $data)
    {
        // Header
        $this->SetFillColor(0);
        $this->SetTextColor(255);
        $this->SetFont('','B');
        foreach ($header as $col) {
            //Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]])
            $this->Cell($col[1], 10, $col[0], 1, 0, 'L', true);
        }
        $this->Ln();
        // Data
        $this->SetFillColor(255);
        $this->SetTextColor(0);
        $this->SetFont('');
        foreach ($data as $row)
        {
            $i = 0;
            foreach ($row as $field) {
                $this->Cell($header[$i][1], 6, $field, 1, 0, 'L', true);
                $i++;
            }
            $this->Ln();
        }
    }
}

// Column headings
$header = array(
             array('First Name',  30), 
             array('Middle Name', 30), 
             array('Last Name',   30),
             array('Age',         12),
             array('Email',       47)
          );
// Get data
$people = new People();
$data = $people->all();

$pdf = new PeoplePDF();
$pdf->SetFont('Arial', '', 12);
$pdf->AddPage();
$pdf->CreateTable($header,$data);
$pdf->Output();

Sørg for at ændre forbindelsesstrengen

$db = new PDO('mysql:host=localhost;dbname=test;charset=UTF-8', 'user', 'password');
                          ^^^^^^^^^        ^^^^                  ^^^^    ^^^^^^^^



  1. Hvordan designer man en filmdatabase?

  2. Oracle ojdbc8 12.2.0.1 Forbudt af Maven

  3. Alle kolonner i tabellen skal indekseres eller ej mysql database?

  4. Fordele ved SQL Server 2008 frem for SQL Server 2005?