Jump to content

Recommended Posts

I have print page using window.print in the body tag at the beginning of php file and i am trying to make the rest of the code as second page, the last table will be on the second page. it is not working.

code:

<b>Absent Report:</b>
    <table style ="page-break-before: always;" border='1'>
		
        <tr><th>ID</th><th>Name</th><th>Absent</th></tr>
        <?=$tdata?>
    </table>
	
	<a href="singlereportbyid.php" width="100%">Click here to go back to Main Menu</a>

			</div>
		</div>
	</div>
</div>
</body>
</html>

why it is not working?

Link to comment
https://forums.phpfreaks.com/topic/310385-page-break-for-table/
Share on other sites

4 minutes ago, ginerjm said:

What is a "second page"?  Is it another web page being sent to the client?  I don't know how you define "page" since there are no real "pages" on web displays.

screenshot, the data split to second page, instead i want the absent table to all move to second page in the printer

 

screen.png

55 minutes ago, Barand said:

https://css-tricks.com/almanac/properties/p/page-break/

Or write a custom script using FPDF or similar so you have complete control

i am trying to use FPDF as you mention it but i am able to find out the problem.

and here is what i tried:

require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Absent Report');
$pdf->SetFont('Arial','',8);

and i am adding the pages at the end after displaying my data

\\ code before

foreach ($res as $r) {
    $tdata .= "<tr><td>" . join('</td><td>', $r) . "</td></tr>\n";
}

?>
		<p>Absent Report:</p>
		<table border='1'>
			<tr><th>ID</th><th>Name</th><th>Absent</th></tr>
			<?=$tdata?>
			
<?php

$pdf->AddPage();
$pdf->Cell(40,10,$tdata);
$pdf->Output();

?>		

		</table>
		<a href="singlereportbyid.php" width="100%">Click here to go back to Main Menu</a>

			</div>
		</div>
	</div>
</div>
</body>
</html>

I get this error

Fatal error
: Uncaught Exception: FPDF error: No page has been added yet in C:\xampp\htdocs\AttendanceSystem\login\fpdf\fpdf.php:271 Stack trace: #0 C:\xampp\htdocs\AttendanceSystem\login\fpdf\fpdf.php(1447): FPDF->Error('No page has bee...') #1 C:\xampp\htdocs\AttendanceSystem\login\fpdf\fpdf.php(643): FPDF->_out('BT 2.83 822.92 ...') #2 C:\xampp\htdocs\AttendanceSystem\login\singlereportidfinal.php(22): FPDF->Cell(40, 10, 'Absent Report') #3 {main} thrown in
C:\xampp\htdocs\AttendanceSystem\login\fpdf\fpdf.php
on line
271

I am trying to put the last table (absents) in new page

Edited by ramiwahdan
additional information

Looks like you have a bit of reading to do regarding the use of FPDF. You can't just call the cell() function and output a PHP table into it.

Don't mix the screen output code with FPDF code. Put the FPDF into a separate script. Display the web page and put a link to the pdf page EG

<a href='attendance_pdf.php?oracleid=533349&sdate=2020-03-01&edate=2020-03-05'>Print version</a>

Below is a sample FPDF script (read, learn and digest) to produce a report which looks like this ...

image.png.e2ef5d84538d6f5568ed37341e94c31d.png

attendance_pdf.php...

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

                                 // ADD YOUR OWN PDO CONNECTION CODE //

class attendPDF extends FPDF
{
    protected $today;
    protected $headbase;
    protected $db;
    protected $sid;
    protected $name;
    protected $sdate;
    protected $edate;
                                                                                                    
    //constructor
    public function __construct($db, $staffid, $sdate, $edate)
    {
        parent::__construct();
        $this->today = date('jS M Y');
        $this->db = $db;
        $this->sid = $staffid;
        $this->sdate = new DateTime($sdate);
        $this->edate = new DateTime($edate);
        $res = $this->db->prepare("SELECT name
                                   FROM staff
                                   WHERE oracleid = ?
                                  ");
        $res->execute( [ $staffid ] );
        $this->name = $res->fetchColumn();
    }

    //Page header
    public function Header()
    {        
        //Helvetica bold 12
        $this->SetFont('Helvetica', '', 14);
        $this->Cell(0, 15, "Attendance Report", 1, 1, 'C');
        //Title
        $sd = $this->sdate->format('l jS F Y');
        $ed = $this->edate->format('l jS F Y');
        
        $this->SetFont('Helvetica', '', 12);
        $this->Cell(60,15,$this->sid . ' - ' . $this->name,1,0,'L');
        $this->Cell(120, 15, "$sd - $ed" , 1, 0, 'C') ;
        $this->Ln();
        $this->headbase = $this->GetY();
    }
    
    //Page footer
    public function Footer()
    {
        $this->setY(-22);
        $this->setX(15);
        $this->SetFont('Helvetica', '', 10);
        $this->Cell(0,5,'( '.$this->today.' )', 'T');
    }
    
    public function attendanceReport()
    {
        $widths = [60, 60, 60];
        $aligns = [ 'L', 'L', 'C' ];
        $heads  = ['Clocked In Time', 'Clocked Out Time', 'Duration'];
        $this->SetY(50);
        $this->SetFont('Helvetica', 'B', 12);
        $this->Cell(0, 10, 'Attendances', 0, 1);
        $this->SetFontSize(10);
        foreach ($heads as $k => $h) {
            $this->Cell($widths[$k], 10, $h, 1, 0, $aligns[$k], 1);
        }
        $this->Ln();
        $this->setFont('', '');
        $res = $this->db->prepare("SELECT date_format(clockingindate, '%a %d/%m/%Y %l:%i %p') as clkin
                                        , date_format(clockingoutdate, '%a %d/%m/%Y %l:%i %p') as clkout
                                        , timediff(clockingoutdate, clockingindate) as duration
                                   FROM attendance_record
                                   WHERE oracleid = ?
                                         AND DATE(clockingindate) BETWEEN ? AND ?
                                   ORDER BY clockingindate     
                                  ");
        $res->execute([ $this->sid, $this->sdate->format('Y-m-d'), $this->edate->format('Y-m-d') ] );
        while ( $r = $res->fetch(PDO::FETCH_NUM) ) {
            foreach ($r as $k => $v) {
                $this->Cell($widths[$k], 6, $v, 0, 0, $aligns[$k]);
            }
            $this->Ln();
        }
    }
    
    public function absenceReport()
    {
        $this->SetY(50);
        $this->SetFont('Helvetica', 'B', 12);
        $this->Cell(0, 10, 'Absences', 0, 1);
        $this->SetFontSize(10);
        $this->Cell(0, 10, 'Dates Absent', 1, 0, 'L', 1);
        $this->setFont('', '');
        $this->Ln();
        // set up a temporary date table - each working day in the reporting period
        $this->db->exec("CREATE TEMPORARY TABLE date(date date)");
        $incr = DateInterval::createFromDateString('next weekday');
        $sd = clone $this->sdate;
        $ed = clone $this->edate;
        $sd->modify('+1 days');                            // adjust for Islamic working week
        $ed->modify('+2 days');                            // adjust for Islamic working week
        $range = new DatePeriod($sd, $incr, $ed);
        foreach ($range as $d) {
            $dt = $d->sub(new DateInterval('P1D'))->format('Y-m-d');              // adjust for Islamic working week
            $dates[] = "('$dt')";
        }
        $this->db->exec("INSERT INTO date VALUES " . join(',', $dates));
        // get days absent
        $res = $this->db->prepare("SELECT DATE_FORMAT(date, '%W %d/%m/%Y' ) as date
                                   FROM staff s
                                        CROSS JOIN 
                                        date d
                                        LEFT JOIN
                                        attendance_record a ON s.oracleid = a.oracleid
                                                            AND d.date = DATE(a.clockingindate)
                                        WHERE s.oracleid = ? AND a.oracleid IS NULL 
                                  ");
        $res->execute( [ $this->sid ] );
        foreach ($res as $r) {
            $this->Cell(0, 6, $r['date'], 0, 1);
        }
    }
}# end class



if (!isset($_GET['oracleid']) || !isset($_GET['sdate']) || !isset($_GET['edate'])) {
    exit;
}

//
// GENERATE REPORT
//

$pdf = new attendPDF($db, $_GET['oracleid'], $_GET['sdate'], $_GET['edate']);

$pdf->AliasNbPages();
$pdf->setAutoPageBreak(1,25);
$pdf->setMargins(15,15,15);
$pdf->SetDrawColor(102);
$pdf->SetFillColor(220);

$pdf->AddPage();
$pdf->attendanceReport();
$pdf->AddPage();
$pdf->absenceReport();

$pdf->output();
?>

 

Edited by Barand
  • Like 1
11 hours ago, Barand said:

Looks like you have a bit of reading to do regarding the use of FPDF. You can't just call the cell() function and output a PHP table into it.

Don't mix the screen output code with FPDF code. Put the FPDF into a separate script. Display the web page and put a link to the pdf page EG


<a href='attendance_pdf.php?oracleid=533349&sdate=2020-03-01&edate=2020-03-05'>Print version</a>

First, thanks for the big help. So i will keep my original untouched or i will only take the code you gave me? If i keep the original, what changes i need to do to that? your screenshots shows both attendance and absent tables, is if from the file you gave me? If i need to add the link you gave me to my original php file, it changes based on choices, how to get that? Lastly, I used to get the dates from a form, in your file, how are we getting these dates? 

19 minutes ago, ramiwahdan said:

it changes based on choices, how to get that?

The whole content of your web page changes based on choices. The link is just another bit of output.

Here's an idea - try thinking.

Edited by Barand
1 hour ago, Barand said:

The whole content of your web page changes based on choices. The link is just another bit of output.

Here's an idea - try thinking.

Hi,

So I put the link in my original php file and when i am directed to the fpdf page i get the first error as i am walking my way through...

First my code:

<?php
require('fpdf/fpdf.php');
include('dbcon.php');

                                 // ADD YOUR OWN PDO CONNECTION CODE //
								 
$servername = "localhost";
$username = "???";
$password = "???";
$db = "timeclock";
		
	$conn = new PDO("mysql:host=$servername;dbname=$db", $username, $password);
	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

class attendPDF extends FPDF
{
    protected $today;
    protected $headbase;
    protected $db;
    protected $sid;
    protected $name;
    protected $sdate;
    protected $edate;
                                                                                                    
    //constructor
    public function __construct($db, $oracleid, $sdate, $edate)
    {
        parent::__construct();
        $this->today = date('jS M Y');
        $this->db = $db;
        $this->sid = $oracleid;
        $this->sdate = new DateTime($sdate);
        $this->edate = new DateTime($edate);
        $res = $this->db->prepare("SELECT staffname
                                   FROM staff
                                   WHERE oracleid = ?
                                  ");
        $res->execute( [ $oracleid ] );
        $this->name = $res->fetchColumn();
    }

and here is the error:

Fatal error: Uncaught Error: Call to a member function prepare() on string in C:\xampp\htdocs\AttendanceSystem\login\fpdf.php:36 Stack trace: #0 C:\xampp\htdocs\AttendanceSystem\login\fpdf.php(148): attendPDF->__construct('timeclock', '533349', '2020-03-01', '2020-03-31') #1 {main} thrown in C:\xampp\htdocs\AttendanceSystem\login\fpdf.php on line 36

I confirm all info is correct so why there is an error

8 minutes ago, Barand said:

What's your code to create the new attendPDF object?

I am using your code, i passed link from my old page to new page that has your code, the line that makes the select statement is having that error. for fpdf i am using your code with page named fpdf.php

7 minutes ago, ramiwahdan said:

I am using your code

Haven't you learnt yet that you can't always copy/paste code and expect it to work straight out of the box?

I created a connection $db and passed that as a parameter. However, you created a connection $conn so pass that instead.

$pdf = new attendPDF($db, $_GET['oracleid'], $_GET['sdate'], $_GET['edate']);
                      ^
                      ^
                    $conn

 

  • Haha 1
6 hours ago, Barand said:

Haven't you learnt yet that you can't always copy/paste code and expect it to work straight out of the box?

I created a connection $db and passed that as a parameter. However, you created a connection $conn so pass that instead.


$pdf = new attendPDF($db, $_GET['oracleid'], $_GET['sdate'], $_GET['edate']);
                      ^
                      ^
                    $conn

I am feeling little dump...thanks for all the help and support. I managed to send the user from my php file to the fpdf file if i am searching for single user (oracle id) but now i am struggling to figure out myself how to send to fpdf if i choose for all users (all oracle id's). I tried to add this below code, it will do get to fpdf file but each record is separated by single link and want i need is one link for all id's.

code:


echo "<h1><a href='fpdf.php?oracleid=$userid&sdate=$thefirstday&edate=$curdate'>Print Attendance and Absent Reports for $name</a></h1>";

 

i am getting a link for each record not each oracle id! i need to have one link for all the id's

Print Attendance and Absent Reports for Rami T Wahdan
Print Attendance and Absent Reports for Rami T Wahdan
Print Attendance and Absent Reports for Shahed R Wahdan
Print Attendance and Absent Reports for Shahed R Wahdan

As you see, I have 2 records for Rami and 2 records for Shahed, the reason why i am getting for each record because i have this inside while loop. I can figure it out to print once for each oracle id, but the problem is, how to send one link for all oracle ids?

Thanks

This is going to come as a surprise to you, but from where I am sitting I cannot see over your shoulder and look at the code on your screen. I do not what query you ran, what data your loop is processing or what your loop is outputting to the screen, or anything else it may be doing.

Therefore it not possible for me to tell you what to do in your situation.

  • Haha 1
41 minutes ago, Barand said:

This is going to come as a surprise to you, but from where I am sitting I cannot see over your shoulder and look at the code on your screen. I do not what query you ran, what data your loop is processing or what your loop is outputting to the screen, or anything else it may be doing.

Therefore it not possible for me to tell you what to do in your situation.

I really feel very stupid today sorry about that. here is the full code for the php part:

<?Php 

include('session.php');
include('dbcon.php');

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="bootstrap.css">

</head>

<body>
	
	<div class="container">
		<div class="row">
			<div class="col m-auto">
				<div class="card mt-5">
					<table class="table table-bordered">
					
<?php
	
$servername = "localhost";
$username = "rwahdan";
$password = "fatima2010";

    $conn = new PDO("mysql:host=$servername;dbname=timeclock", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$res = $conn->query("SELECT oracleid
                         , name 
                         , des
						 , theday
                         , clockingindate   as clockin
						 , permissionoutdate as perout
						 , permissionindate as perin
                         , clockingoutdate  as clockout
						 , fullday
						 , reason
						 , timediff(permissionindate,permissionoutdate ) as perduration
                         , timediff(clockingoutdate, clockingindate) as duration
                         , total
						 , total2
                    FROM attendance_records
                         JOIN (
                                SELECT oracleid
                                     , sec_to_time(sum(timestampdiff(SECOND, clockingindate, clockingoutdate))) as total
									 , sec_to_time(sum(timestampdiff(SECOND, permissionoutdate, permissionindate))) as total2
                                FROM attendance_records
								where isdone =-1
                                GROUP BY oracleid
                              ) tots USING (oracleid)
							  where isdone =-1
                    ORDER BY oracleid, clockingindate
                    ");

?>

		<?php
		
			$t=time();
			$curdate = date("d-m-Y",$t);
			$curTime = date("g:i:s A",$t);

            $previd = 0;
            foreach ($res as $row) {
				
                if ($row['oracleid'] != $previd) {                              // id changed so output total
                    if ($previd != 0) {
						
						echo "<tr><td colspan='4'</td><td><td><td>Total Permission Time:</td></td></td><td>$total2</td></tr>";
						echo '<tr>';
						echo "<tr><td colspan='4'</td><td><td><td><td>Total Attendance Time:</td></td><td>$total</td></tr>";
						
						 echo "
						<tr>
						  <td>OracleID</td>
						  <td>{$row['oracleid']}</td>
						  <td>Name</td>
						  <td>{$row['name']}</td>
						  <td>Designation</td>
						  <td>{$row['des']}</td>
						</tr>
						"; 
						
						$userid = $row['oracleid'];						
						$qry = "select joindate from staff where OracleID = '$userid'";
						$answ = mysqli_query($con, $qry);
						$rowanswer = mysqli_fetch_assoc($answ);
						$thefirstday = $rowanswer['joindate'];	
						
                    }
					else{
						
						$userid = $row['oracleid'];
						$qry = "select joindate from staff where OracleID = '$userid'";
						$answ = mysqli_query($con, $qry);
						$rowanswer = mysqli_fetch_assoc($answ);
						$thefirstday = $rowanswer['joindate'];		
								
								echo "
								<tr>
								  <td>OracleID</td>
								  <td>{$row['oracleid']}</td>
								  <td>Name</td>
								  <td>{$row['name']}</td>
								  <td>Designation</td>
								  <td>{$row['des']}</td>
								</tr>
								";								
							}
                    $previd = $row['oracleid'];
					$name = $row['name'];

                }
								
				$fullday = $row['fullday'];
				$perindate = $row['perin'];
				$perduration = $row['perduration'];
				$peroutdate = $row['perout'];		
						
							if ($peroutdate != Null)
							{
								if ($fullday == -1)
								{
									
									$fullday = "Yes";
									
									if ($perindate == Null)
									{
									
									$perindate = "All Day";
									
									}
								
									if ($perduration == Null)
									{
									
									$perduration = "All Day";
									
									}
									
								}
								else
								{
									
									$fullday = "No";
									
								}
							}
							else{
								
								$fullday = Null;
								$perindate = Null;
								$perduration = Null;
								
							}
				
				
               

				?>
						
				<th colspan='9'></th>
						  
				<?php
				
				echo "
						<tr>
						
                            <td>Day</td>
							<td>Clocking In</td>
							<td>Permission Out</td>
							<td>Full Day?</td>
							<td>Permission Reason</td>
							<td>Permission In</td>
							<td>Clocking Out</td>
							<td>Permission Duration</td>
							<td>Normal Duration</td>
							
						</tr>
						
						<tr>
						  <td>{$row['theday']}</td>
                          <td>{$row['clockin']}</td>
						  <td>{$row['perout']}</td>
						  <td>{$fullday}</td>
						  <td>{$row['reason']}</td>
						  <td>{$perindate}</td>
                          <td>{$row['clockout']}</td>
						  <td>{$perduration}</td>
                          <td>{$row['duration']}</td>
                       </tr>
					  
                ";
                
				$total = $row['total'];
				$total2 = $row['total2'];
				
			
									if ($perduration == "All Day")
									{
										
										if ($total2 == Null)
										{
										
										$total2 = "All Day";
										
										}
									
									}
			
				echo "<h1><a href='fpdf.php?oracleid=$userid&sdate=$thefirstday&edate=$curdate'>Print Attendance and Absent Reports for $name</a></h1>";
								
			}
			
						echo "<tr><td colspan='4'</td><td><td><td>Total Permission Time:</td></td></td><td>$total2</td></tr>";
						echo '<tr>';
						echo "<tr><td colspan='4'</td><td><td><td><td>Total Attendance Time:</td></td><td>$total</td></tr>";
										
	?>
						
					</table>
					<a href="singlereportbyid.php" width="100%">Click here to go back to Main Menu</a>
			
				</div>
			</div>
		</div>
	</div>

</body>

</html>

as for the fpdf file it is what you have given me.

Thanks

5 minutes ago, ramiwahdan said:

it should output to a single link to go to the fpdf file you shared with me

Yes, i know. You already established that.

8 hours ago, Barand said:

it depends where you want to output them.

In my previous post, I suggested you might be able able to output the link with each person's total (ie once per person like the total).

image.png.4fb06db0eca260b174205b2636540277.png

If you want them somewhere else then the logic will be different.

  • Thanks 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.