如何在fpdf中传递会话数据

how to pass session data in fpdf

我正在制作一个 Elearning 网页,网站管理员可以在其中根据用户数据生成报告。我想根据管理员想要报告的内容生成报告。我试过将会话数据传递给 fpdf,就像将会话数据传递给另一个页面一样,但它不起作用。

有必要给我示例代码吗?如果是,我将编辑我的问题并将我的代码放入其中。

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

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

<?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,因此我可以将它用于 SELECT 语句中的 WHERE 子句。谢谢!

我已经解决了我的问题。我将会话数据放在 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();

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