Click here to Skip to main content
13,598,868 members
Rate this:
 
Please Sign up or sign in to vote.
Hi I am working on a payroll project and I am coding with php,html,js and mysql on xampp server. I have a salary sheet code which needs to be printed in pdf format for each employee with their names as the pdf filename.
on running the php page the js code triggers automatically and generates pdf files but the problem is the salary data of the first employee is printed on every payslip but
The filename changes with every pdf.
for example :
1st emp name is rajesh rao, 2nd emp name is a.ghosh.
now when I print the sheets, then 2 pdf files are created for these 2 employees with their names as filenames but the salary data is not changing on the 2nd emp sheet/

Any Help will be appreciated

What I have tried:

<HTML>
<HEAD>
<TITLE>Salary Sheet</TITLE>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="pure-blog/css/home.css" />
<link rel="stylesheet" href="pure-blog/css/pure-min.css" />
<link rel="stylesheet" href="pure-blog/css/pure-u.css" />
<link rel="stylesheet" href="pure-blog/css/pure-form.css" />
<link rel="stylesheet" href="pure-blog/css/pure-menu.css" />
<link rel="stylesheet" href="pure-blog/css/pure-table.css" />
</HEAD>
<BODY>
<?php
include "connect.php";
$cde = 1;
while ($cde < 3) {
$mysql = "SELECT * FROM emp_ndata WHERE ID = '$cde' ";
$myquery = mysqli_query($con, $mysql);
if(! $myquery ) {
die('Could not get data: ' . mysql_error());
}
$row = mysqli_fetch_array($myquery, MYSQLI_ASSOC);
?>
<div id="pdata" class="printArea">
<div class="pure-img" style="text-align:left">
<img src="pure-blog/img/companyLogoFull-pdf.png">
</div>

<table class="table_for_showdata table_for_showdata_small">
<tr>
<th colspan="2">
Salary Sheet of <b class="table_for_showdata_lg">
<?php echo $row['Emp_Name']; ?></b> :&nbsp; <?php echo $row ['Emp_psmonth']; ?>&nbsp; , &nbsp;<?php echo $row ['Emp_psyear']; ?>
</th>
</tr>

<tr>
<td>Emp-Code</td>
<td><?php echo $row['Emp_ID']; ?></td>
</tr>
<tr>
<td>Designation</td>
<td><?php echo $row['Emp_Designation']; ?></td>
</tr>

<tr>
<th colspan="2">EARNINGS</th>
</tr>

<tr>
<td >Basic</td>
<td><?php echo round($row ['Emp_Basic']).".00"; ?></td>
</tr>

<tr>
<td >House Rent Allowance</td>
<td><?php echo round($row ['Emp_HRA']).".00"; ?></td>
</tr>

<tr>
<th >DEDUCTIONS</th>
<th>&nbsp;</th>
</tr>

<tr>
<td >Professional Tax</td>
<td><?php echo $row ['Emp_Proffessional_Tax'].".00"; ?></td>
</tr>

<tr>
<td >Provident Fund</td>
<td><?php echo $row ['Emp_Provident_Fund'].".00"; ?></td>
</tr>

<tr>
<th >&nbsp;</th>
<th>&nbsp;</th>
</tr>

<tr>
<td class="tab_head_blue"><b>TOTAL PAY : </td>
<td style="font-size:18px"><b>Rs.<?php echo $row ['Emp_Total_Pay'].".00"; ?></b></td>
</tr>

</table>
</div>
<script type="text/javascript">
function printDiv() {
var divToPrint = document.getElementById('pdata');
var popupWin = window.open('', '_blank', 'width=1100,height=400');
popupWin.document.open();
document.title = "<?php echo $row['Emp_Name']; ?>";
popupWin.document.write('<html><head><title>' + document.title + '</title>');
popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
popupWin.document.close();
}
</script>

<script>
printDiv();
</script>
<?php
$GLOBALS['cde']++;
}
?>

</BODY>
</HTML>
Posted 13-Feb-18 8:40am
Updated 15-Feb-18 8:51am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Element IDs in an HTML document must be unique. You are creating a new <div> for each row, but they all have the same ID. How the browser behaves when you ask it to return the single element with that ID is undefined; in this case, it seems to be returning the first element.

Change the <div> from:
<div id="pdata" class="printArea">
to:
<div class="printArea" data-title="<?php echo $row['Emp_Name']; ?>">

Then move your script outside of the PHP loop, and loop through the elements to print:
<?php
    $GLOBALS['cde']++;
} 
?>

<script>
(function(){
    var divsToPrint = document.getElementsByClassName('printArea');
    for (var index = 0; index < divsToPrint.length; index++) {
        var divToPrint = divsToPrint[index];
        var title = divToPrint.getAttribute("data-title");
        var popupWin = window.open('', '_blank', 'width=1100,height=400');
        popupWin.document.open();
        popupWin.document.write('<html><head><title>' + title + '</title></head>');
        popupWin.document.write('<body onload="window.print()">' + divToPrint.innerHTML + '</body></html>');
        popupWin.document.close();
    }
})();
</script>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.180621.3 | Last Updated 15 Feb 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100