Search code examples
phpfpdf

how to pass session data in fpdf


I'm making a Elearning webpage where the admins of the website are able to generate reports from the users data. I want to generate report base on what the admin want to have reports on. I've tried passing the session data to fpdf just like passing session data to another page but it is not working.

Is it necessary to give my sample code or not? if it is, I will edit my question and put my codes in it.

Sorry for my english, I hope you understand me well.

I dynamically generate my report so I am using that session data for the WHERE clause in my SELECT statement.

<?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();

This is where I want to get the session data:

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';
        }
    }
}

}

And this is the page where the admins will generate reports:

<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
}
?>

I just want to get the whID though, so I can use it to my WHERE clause in the SELECT statement. Thank you!


Solution

  • I've fixed my problem. I put the session data before the require('../fpdf/fpdf.php'); I've search that fpdf don't want to have a variable in between require('../fpdf/fpdf.php'); until the end of fpdf code.

    This is what I did to fix my problem:

    <?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();

    As you can see at the top of the code, there is the session data I have from the previous page. And I use whID='{$_SESSION['warehouseID']}' in the SELECT statement so that fpdf will read the variable from the session. I tried everything but this is the only way (in my case) fpdf will read session variable. I hope this will be helpful for beginner programmers just like me!