如何在fpdf

时间:2019-02-07 06:39:59

标签: php fpdf

我正在制作一个学习网页,该网站的管理员可以根据用户数据生成报告。我想基于管理员想要报告的内容生成报告。我曾尝试将会话数据传递给fpdf,就像将会话数据传递给另一页一样,但是它不起作用。

是否需要提供示例代码?如果是这样,我将编辑我的问题并将代码放入其中。

对不起,我的英语,希望您能理解我。

我动态生成报告,因此我在WHERE语句中的SELECT子句中使用了该会话数据。

<?php

require('../fpdf/fpdf.php');
require('includes/dbh.inc.php');
session_start();



class myPDF extends FPDF {
	function header() {
		$this->Image('../images/logo(dbs).png',10,6);
		$this->SetFont('Arial','B',14);
		$this->Cell(276,5,'User Report',0,0,'C');
		$this->Ln();
		$this->SetFont('Times', '',12);
		$this->Cell(276,10,'WAREHOUSE DEPARTMENT',0,0,'C');
		$this->Ln(20);

	}
	function footer() {
		$this->SetY(-15);
		$this->SetFont('Arial','',8);
		$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
	}
	function headertable() {
		$this->SetFont('Times','B',12);
		$this->Cell(40,20,'WH#1 Finished',0,1,'C');
		$this->Cell(34,10,'Firstname',1,0,'C');
		$this->Cell(34,10,'Lastname',1,0,'C');
		$this->Cell(65,10,'Lesson Title',1,0,'C');
		$this->Cell(25,10,'Status',1,0,'C');
		$this->Cell(40,10,'Finished',1,0,'C');
		$this->Ln();

	}

	function headertable1() {
		$this->SetFont('Times','B',12);
		$this->Cell(40,20,'WH#1 Unfinished',0,1,'C');
		$this->Cell(34,10,'Firstname',1,0,'C');
		$this->Cell(34,10,'Lastname',1,0,'C');
		$this->Cell(65,10,'Lesson Title',1,0,'C');
		$this->Cell(25,10,'Status',1,0,'C');
		$this->Cell(40,10,'Finished',1,0,'C');
		$this->Ln();

	}
	function viewTable($conn) {
		$this->SetFont('Times', '',12);
		$stmt = $conn->query('SELECT DISTINCT firstname, lastname, lessontitle, status, finished FROM report 
				      WHERE lessontitle!="no lesson taken yet" AND user_type="user" AND whID="1" AND acnt_stats="active"
			              ORDER BY lessontitle DESC');
		while($data = $stmt->fetch_object()) {
			$this->Cell(34,10,$data->firstname,1,0,'L');
			$this->Cell(34,10,$data->lastname,1,0,'L');
			$this->Cell(65,10,$data->lessontitle,1,0,'L');
			$this->Cell(25,10,$data->status,1,0,'L');
			$this->Cell(40,10,$data->finished,1,0,'L');
			$this->Ln();
		}
	}

	function viewTable1($conn) {
		$this->SetFont('Times', '',12);
		$stmt = $conn->query('SELECT DISTINCT firstname, lastname, lessontitle, status, finished FROM report 
				      WHERE lessontitle="no lesson taken yet" AND user_type="user" AND whID="1" AND acnt_stats="active"
				      ORDER BY lessontitle DESC');

		while($data = $stmt->fetch_object()) {
			$this->Cell(34,10,$data->firstname,1,0,'L');
			$this->Cell(34,10,$data->lastname,1,0,'L');
			$this->Cell(65,10,$data->lessontitle,1,0,'L');
			$this->Cell(25,10,$data->status,1,0,'L');
			$this->Cell(40,10,$data->finished,1,0,'L');
			$this->Ln();
		}
	}
}


$pdf = new myPDF();
$pdf->AliasNbPages();
$pdf->AddPage('P','Letter',0);
$pdf->headertable();
$pdf->viewTable($conn);
$pdf->AddPage('P','Letter',0);
$pdf->headertable1();
$pdf->viewTable1($conn);
$pdf->Output();

这是我要获取会话数据的地方:

if (!isset($submit)) {
    echo 'failed to update!';
 } else {   

    // for insert statement
    $acnt_status = $_SESSION['acnt_status'];
    $wh = $_SESSION["whID"];
    $user_type = $_SESSION['user']['user_type'];
    //update report data from database.
    $user = $_SESSION['user']['firstname'];
    $lname = $_SESSION['user']['lastname'];
    $dept = $_GET['dept'];
    $ica = $_SESSION['status2'];
    $lID = $_SESSION['title'];
    $stmt = mysqli_stmt_init($conn); 

    $result = $conn->query("SELECT * FROM report 
                            WHERE lessontitle = '$lID' AND status='registered' AND lastname='$lname' AND firstname='$user'");
    //check of there existing data in the database
    if ($result && mysqli_num_rows($result) > 0) {
        //if yes, update the data in database
        $query = "UPDATE report SET lessontitle='$lID', status='$ica', finished=NOW() 
                  WHERE lastname='$lname' AND firstname='$user' AND status='registered'";
        $result = mysqli_query($conn, $query);// use for executing multiple query a single query statement.
        echo'yes';
    } else {
        //check again if there's already data in database
        $result = $conn->query("SELECT * FROM report 
                                WHERE lessontitle = '$lID'
                                AND lastname='$lname' AND firstname='$user'");
        //check of there existing data in the database
        if ($result && mysqli_num_rows($result) > 0) {
            echo'there is';
        } else {
            //if no, insert new data in database
            mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);

            $sql = "INSERT INTO report (firstname, lastname, lessontitle, status, department, acnt_stats, whID, user_type, finished)        VALUES (?,?,?,?,?,?,?,?,NOW())";
            if (!mysqli_stmt_prepare($stmt, $sql)) {
            echo "SQL statement failed";
            } else {
                mysqli_stmt_bind_param($stmt, "ssssssis", $user, $lname, $lID, $ica, $dept, $acnt_status, $wh, $user_type);
                mysqli_stmt_execute($stmt);
                echo 'no';
        }
    }
}

}

这是管理员将在其中生成报告的页面:

<div class="listbody">
<div class="list" style="font-size:1.5vw;">
<?php
    if (!count($reportOpex)) {
        echo 'no record';
    } else {
?>
    <table>
        <thead>
            <tr>
                <th colspan="5" style=" background-color: #EF3842;" class="uReport">User Report</th>
            <tr>
                <th>Firstname</th>
                <th>LastName</th>
                <th>Lesson Title</th>
                <th>Status</th>
                <th>Date&Time finished</th>
            </tr>
        </thead>
        <tbody>
            <form action="GenReport.php" method="post">
            <?php
            foreach($reportOpex as $r) {
            ?>
            <tr>
                <td><?php echo escape($r->firstname)?></td>
                <td><?php echo escape($r->lastname); ?></td>
                <td><?php echo escape($r->lessontitle); ?></td>
                <td><?php echo escape($r->status); ?></td>
                <td><?php echo escape($r->finished); ?></td>

            </tr>

            <?php
            }
            ?>
        </tbody>
            <tr>
                <th colspan="5" style=" background-color: #6495ED"><button class="gReport"><b>Generate Report &#9658;</b></button></th>
            </tr>
            </form>
    </table>
<?php
}
?>

我只是想获取whID,因此可以在WHERE语句的SELECT子句中使用它。谢谢!

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题。我将会话数据放在require('../fpdf/fpdf.php');之前,我一直在搜索fpdf在require('../fpdf/fpdf.php');之间直到fpdf代码结尾之间都没有变量。

这是我解决问题的方法:

<?php
session_start();
$_SESSION['warehouseID'] = $_POST['warehouseID'];

require('../fpdf/fpdf.php');
require('includes/dbh.inc.php');




class myPDF extends FPDF {
	function header() {
		$this->Image('../images/logo(dbs).png',10,6);
		$this->SetFont('Arial','B',14);
		$this->Cell(276,5,'User Report',0,0,'C');
		$this->Ln();
		$this->SetFont('Times', '',12);
		$this->Cell(276,10,'WAREHOUSE DEPARTMENT',0,0,'C');
		$this->Ln(20);

	}
	function footer() {
		$this->SetY(-15);
		$this->SetFont('Arial','',8);
		$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
	}
	function headertable() {
		$this->SetFont('Times','B',12);
		$this->Cell(40,20,'WH#1 Finished',0,1,'C');
		$this->Cell(34,10,'Firstname',1,0,'C');
		$this->Cell(34,10,'Lastname',1,0,'C');
		$this->Cell(65,10,'Lesson Title',1,0,'C');
		$this->Cell(25,10,'Status',1,0,'C');
		$this->Cell(40,10,'Finished',1,0,'C');
		$this->Ln();

	}

	function headertable1() {
		$this->SetFont('Times','B',12);
		$this->Cell(40,20,'WH#1 Unfinished',0,1,'C');
		$this->Cell(34,10,'Firstname',1,0,'C');
		$this->Cell(34,10,'Lastname',1,0,'C');
		$this->Cell(65,10,'Lesson Title',1,0,'C');
		$this->Cell(25,10,'Status',1,0,'C');
		$this->Cell(40,10,'Finished',1,0,'C');
		$this->Ln();

	}
	function viewTable($conn) {
		$this->SetFont('Times', '',12);
		$stmt = $conn->query("SELECT DISTINCT firstname, lastname, lessontitle, status, finished FROM report 
							  WHERE lessontitle!='no lesson taken yet' AND user_type='user' AND whID='{$_SESSION['warehouseID']}' AND acnt_stats='active'
							  ORDER BY lessontitle DESC");
		while($data = $stmt->fetch_object()) {
			$this->Cell(34,10,$data->firstname,1,0,'L');
			$this->Cell(34,10,$data->lastname,1,0,'L');
			$this->Cell(65,10,$data->lessontitle,1,0,'L');
			$this->Cell(25,10,$data->status,1,0,'L');
			$this->Cell(40,10,$data->finished,1,0,'L');
			$this->Ln();
		}
	}

	function viewTable1($conn) {
		$this->SetFont('Times', '',12);
		$stmt = $conn->query("SELECT DISTINCT firstname, lastname, lessontitle, status, finished FROM report 
							  WHERE lessontitle='no lesson taken yet' AND user_type='user' AND whID='{$_SESSION['warehouseID']}' AND acnt_stats='active'
							  ORDER BY lessontitle DESC");

		while($data = $stmt->fetch_object()) {
			$this->Cell(34,10,$data->firstname,1,0,'L');
			$this->Cell(34,10,$data->lastname,1,0,'L');
			$this->Cell(65,10,$data->lessontitle,1,0,'L');
			$this->Cell(25,10,$data->status,1,0,'L');
			$this->Cell(40,10,$data->finished,1,0,'L');
			$this->Ln();
		}
	}
}


$pdf = new myPDF();
$pdf->AliasNbPages();
$pdf->AddPage('P','Letter',0);
$pdf->headertable();
$pdf->viewTable($conn);
$pdf->AddPage('P','Letter',0);
$pdf->headertable1();
$pdf->viewTable1($conn);
$pdf->Output();

如您在代码顶部所见,有上一页中的会话数据。而且我在whID='{$_SESSION['warehouseID']}'语句中使用了SELECT,以便fpdf从会话中读取变量。我尝试了一切,但这是fpdf读取会话变量的唯一方法(以我为例)。我希望这将对像我这样的初学者程序员有所帮助!