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>