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

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

My advice is to start again from the basics , what pdf is , what PHP , what HTML and what JavaScript. You definitely not printing any pdf here and you have mixed PHP logic with view with JavaScript code that uses directly PHP variables.

hello jkon,
Thanks for the advice. this code is printing pdf files via pdfcreator app. if you can help put with some ocde snippets then it will be nice. I have found out a way to print with fpdf .I can print a html table with mysql data in pdf format for a single employee with a print button but I want to print all the payslips all at once. I know it can be done but maybe I am missing something in the code which I need help.

See here:

$row = mysqli_fetch_array($myquery, MYSQLI_ASSOC);
?>
<div id="pdata" class="printArea">

You are only accessing one row from the result of your query. And that is all that there will be in pdata.

So maybe you should modify to have a loop like thiswhile($row = mysqli_fetch_array($myquery, MYSQLI_ASSOC)){} Then put pdata before that loop so that you can do whatever you are doing for all the rows from your query.

commented: +1 for reading this code and understood it . I have never used msqli (PDO fun here) +9

You have to use only one fpdf object for whole process
Loop through alll payslip record
you have already written code for one payslip, so put that code in your above loop
when next record start add page to your fpdf object
output your fpdf object after loop ends

javascript has no role in your problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.