Auto Payslip Printing using Javascript, PHP and MySqli for 500 employees - javascript

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 a pdf file with the filename as the employee name alongwith the employee salary data.
Now I need to generate all the employee payslips together in this format but automatically or maybe by clicking a single button.
I have done this project in vb6,ms-access with success but I am not well versed in javascript and now I am stuck here.
Any Help will be appreciated
<HTML>
<HEAD>
<TITLE>Salary Sheet</TITLE>
</HEAD>
<BODY>
<?php
include "connect.php";
$mysql = "SELECT * FROM emp_ndata LIMIT 0,1";
$myquery = mysqli_query($con, $mysql);
if(! $myquery ) {
die('Could not get data: ' . mysql_error());
}
$row = mysqli_fetch_array($myquery, MYSQLI_ASSOC);
?>
<div id="pdata">
<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> : <?php echo $row ['Emp_psmonth']; ?> , <?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> </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 > </th>
<th> </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>
</BODY></HTML>

Related

How to delete from json table?

I have a table like below:
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Nazwa</th>
<th scope="col">Cena</th>
<th scope="col">Kupujący</th>
<th scope="col"> </th>
</tr>
</thead>
<tbody>
<?php foreach($data as $key => $value): ?>
<tr>
<td><?php echo $value['id']; ?></td>
<td><?php echo $value['name']; ?></td>
<td><?php echo $value['price']; ?></td>
<td><?php echo $value['buyer']; ?></td>
<td><button type="button" name="button2" >Usuń</button></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
how to delete item from this list via clicking on the button in last column of the table?
my php:
$jsonData = file_get_contents("data.json");
$data = json_decode($jsonData, true);
thanks for any help, I don't know how to get value by button clicking :/
It seems that your data is gotten from a file, so to get it done you need to go over all these steps in the same php script:
Get data from this file
Parse data with json_decode
Remove an item using unset($data['id'])
Save new data in the same file
Here is an example:
$jsonData = file_get_contents("data.json");
$data = json_decode($jsonData, true);
// here we go
if(isset($_POST['item_key']) && !empty($_POST['item_key'])) {
$data = array_values($data);
$i=0;
foreach($data as $element) {
//check if it's the right item
if(intval($_POST['item_key']) == $element['id']){ // intval only if you are sure id passed in POST[item_key] always integer
unset($data[$i]);
}
$i++;
}
file_put_contents('data.json', json_encode(array_values($data)));
}
$_POST['item_key'] will come after submiting the form in the html, see below.
In your html code, you will need to add the following:
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Nazwa</th>
<th scope="col">Cena</th>
<th scope="col">Kupujący</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<?php foreach($data as $key =>
$value): ?>
<tr>
<td><?php echo $value['id']; ?></td>
<td><?php echo $value['name']; ?></td>
<td><?php echo $value['price']; ?></td>
<td><?php echo $value['buyer']; ?></td>
<td>
<!-- action="#" means you will stay in the same script after submit -->
<form action="#" method="POST">
<input
hidden
type="text"
name="item_key"
value="<?php echo $value['id'];?>"
/>
<input type="submit" value="Remove" />
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
UPDATE
Used unset with array_values after json_decode
Removed header(refre..) since the refresh will be done by form submit to same script

How to add value in html table column

I am working on the following codes. I need to add all the values per category in my table. The data is from the database. Please see the picture first:
Here is my interface image
Here is my HTML codes:
<table id="data" class="table table-bordered table-striped">
<thead>
<tr align="center">
<th width="5%">Item</th>
<th width="30%">Description</th>
<th width="5%">Unit</th>
<th width="5%">Qty</th>
<th width="10%">Unit Cost</th>
<th width="15%">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td colspan="5"><i>General Requirement</i></td>
</tr>
<?php
$id = $_GET['id'];
$select_bill = "SELECT *, quantity * unit_cost AS amount FROM bill_tbl WHERE project_name = $id";
$select_bill_result = mysqli_query($con,$select_bill);
if (mysqli_num_rows($select_bill_result)> 0) {
while ($row = mysqli_fetch_assoc($select_bill_result)) {
if ($row['category'] == 'General Requirement') {
?>
<tr>
<td></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['unit']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['unit_cost']; ?></td>
<td><?php echo $row['amount']; ?></td>
</tr>
<?php
}
}
}
?>
<tr>
<td></td>
<td colspan="4"><b>Subtotal</b></td>
<td></td>
</tr>
<tr>
<td>2</td>
<td colspan="5"><i>Concrete and Masonry Works</i></td>
</tr>
<?php
$id = $_GET['id'];
$select_bill = "SELECT *, quantity * unit_cost AS amount FROM bill_tbl WHERE project_name = $id";
$select_bill_result = mysqli_query($con,$select_bill);
if (mysqli_num_rows($select_bill_result)> 0) {
while ($row = mysqli_fetch_assoc($select_bill_result)) {
if ($row['category'] == 'Concrete and Masonry Works') {
?>
<tr>
<td></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['unit']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['unit_cost']; ?></td>
<td><?php echo $row['amount']; ?></td>
</tr>
<?php
}
}
}
?>
<tr>
<td></td>
<td colspan="4"><b>Subtotal</b></td>
<td></td>
</tr>
</tbody>
</table>
Script code
<script>
var total = 0;
var rows = $("#data tr:gt(0)");
rows.children("td:nth-child(6)").each(function() {
total += parseInt($(this).html());
});
$("#total").html(total);
</script>
This is for my school project.
You can sum it at PHP part. Code like this will sum all amounts while retrieving from DB.
<table id="data" class="table table-bordered table-striped">
<thead>
<tr align="center">
<th width="5%">Item</th>
<th width="30%">Description</th>
<th width="5%">Unit</th>
<th width="5%">Qty</th>
<th width="10%">Unit Cost</th>
<th width="15%">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td colspan="5"><i>General Requirement</i></td>
</tr>
<?php
$id = $_GET['id'];
$select_bill = "SELECT *, quantity * unit_cost AS amount FROM bill_tbl WHERE project_name = $id";
$select_bill_result = mysqli_query($con,$select_bill);
if (mysqli_num_rows($select_bill_result)> 0) {
while ($row = mysqli_fetch_assoc($select_bill_result)) {
if ($row['category'] == 'General Requirement') {
$subtotal += $row['amount']; //Here you sum all amounts from rows
?>
<tr>
<td></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['unit']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['unit_cost']; ?></td>
<td><?php echo $row['amount']; ?></td>
</tr>
<?php
}
}
}
?>
<tr>
<td></td>
<td colspan="4"><b>Subtotal</b></td>
<td><?php echo $subtotal; //Here you display subtotal ?></td>
</tr>
<tr>
<td>2</td>
<td colspan="5"><i>Concrete and Masonry Works</i></td>
</tr>
<?php
$id = $_GET['id'];
$select_bill = "SELECT *, quantity * unit_cost AS amount FROM bill_tbl WHERE project_name = $id";
$select_bill_result = mysqli_query($con,$select_bill);
if (mysqli_num_rows($select_bill_result)> 0) {
while ($row = mysqli_fetch_assoc($select_bill_result)) {
if ($row['category'] == 'Concrete and Masonry Works') {
$subtotal += $row['amount']; //Here you sum all amounts from rows
?>
<tr>
<td></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['unit']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['unit_cost']; ?></td>
<td><?php echo $row['amount']; ?></td>
</tr>
<?php
}
}
}
?>
<tr>
<td></td>
<td colspan="4"><b>Subtotal</b></td>
<td><?php echo $subtotal; //Here you display subtotal ?></td>
</tr>
</tbody>
</table>
<table id="data" class="table table-bordered table-striped">
<thead>
<tr align="center">
<th width="5%">Item</th>
<th width="30%">Description</th>
<th width="5%">Unit</th>
<th width="5%">Qty</th>
<th width="10%">Unit Cost</th>
<th width="15%">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td colspan="5"><i>General Requirement</i></td>
</tr>
<?php
$id = $_GET['id']; $catA =0; //this variable will be use to get the some of the category. You can name the variable whatever name you want
$select_bill = "SELECT *, quantity * unit_cost AS amount FROM bill_tbl WHERE project_name = $id";
$select_bill_result = mysqli_query($con,$select_bill);
if (mysqli_num_rows($select_bill_result)> 0) {
while ($row = mysqli_fetch_assoc($select_bill_result)) {
if ($row['category'] == 'General Requirement') {
$catA+=$row['amount']; // this will add each amount value to the $catA variable until the loop finished;
?>
<tr>
<td></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['unit']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['unit_cost']; ?></td>
<td><?php echo $row['amount']; ?></td>
</tr>
<?php
}
}
}
?>
<tr>
<td></td>
<td colspan="4"><b>Subtotal</b></td>
<td><?php echo $catA; //the variable now has the total sum of the amount;?></td>
</tr>
<tr>
<td>2</td>
<td colspan="5"><i>Concrete and Masonry Works</i></td>
</tr>
<?php
$id = $_GET['id']; $catB =0; //this variable will be use to get the some of the category. You can name the variable whatever name you want
$select_bill = "SELECT *, quantity * unit_cost AS amount FROM bill_tbl WHERE project_name = $id";
$select_bill_result = mysqli_query($con,$select_bill);
if (mysqli_num_rows($select_bill_result)> 0) {
while ($row = mysqli_fetch_assoc($select_bill_result)) {
if ($row['category'] == 'Concrete and Masonry Works') {
$catB+=$row['amount']; // this will add the amount value to the $catB variable until the loop finished;
?>
<tr>
<td></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['unit']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['unit_cost']; ?></td>
<td><?php echo $row['amount']; ?></td>
</tr>
<?php
}
}
}
?>
<tr>
<td></td>
<td colspan="4"><b>Subtotal</b></td>
<td><?php echo $catB; //the variable now has the total sum of the amount;?></td>
</tr>
</tbody>
</table>

How to hide the value in table and anchor tag when user click the anchor return

I want to hide the row when the admin click the anchor return because It's already saved in return.php so just need hide the returned status while the pending is not. Thank you for helping me
<thead>
<tr>
<th>Book title</th>
<th>Borrower</th>
<th>Type</th>
<th>Date Borrow</th>
<th>Due Date</th>
<th>Date Returned</th>
<th>Fines</th>
<th>Borrow Status</th>
</tr>
</thead>
<tbody>
<?php $user_query=mysqli_query($dbcon,"select * from borrow
LEFT JOIN member ON borrow.member_id = member.member_id
LEFT JOIN borrowdetails ON borrow.borrow_id = borrowdetails.borrow_id
LEFT JOIN book on borrowdetails.book_id = book.book_id
ORDER BY borrow.borrow_id DESC
")or die(mysqli_error());
while($row=mysqli_fetch_array($user_query)){
$currentdate = date('Y/m/d');
$start = new DateTime($returndate=$row['due_date']);
$end = new DateTime($currentdate);
$fines =0;
if(strtotime($currentdate) > strtotime($returndate)){
$days= $start->diff($end, true)->days;
$fines = $days > 0 ? intval(floor($days)) * 10 : 0;
$fi = $row['borrow_details_id'];
mysqli_query($dbcon,"update borrowdetails set fines='$fines' where borrow_details_id = '$fi'");
}
$id=$row['borrow_id'];
$book_id=$row['book_id'];
$borrow_details_id=$row['borrow_details_id'];
?>
<tr class="del<?php echo $id ?>">
<td><?php echo $row['book_title']; ?></td>
<td><?php echo $row['firstname']." ".$row['lastname']; ?></td>
<td><?php echo $row['type']; ?></td>
<td><?php echo $row['date_borrow']; ?></td>
<td><?php echo $row['due_date']; ?> </td>
<td><?php echo $row['date_return']; ?> </td>
<td><?php echo "₱ ".$fines; ?></td>
<td><?php echo $row['borrow_status'];?></td>
<td> <a rel="tooltip" title="Return" id="<?php echo $borrow_details_id; ?>"
href="#delete_book<?php echo $borrow_details_id; ?>" data-toggle="modal"
class="btn btn-success"><i class="icon-check icon-large"></i>Return</a>
<?php include('modal_return.php'); ?>
<td></td>
</tr>
<?php } ?>
</tbody>
</table>
This is the view_return.php where will admin see the all books pending
This is the history/transaction where the book is already return
For this , you can do like below example, Add a class "test" in anchorTag or in td
and by using jquery
$('.test').click(function(){
$(this).parent().hide();
});
<table>
<tr>
<td>Test</td>
<td class="test" id="returnId1">Return</td>
</tr>
<tr>
<td>Test2</td>
<td class="test" id="returnId2">Return</td>
</tr>
</table>

How to update/add/delete rows in table with AJAX

I'm new to Javascript. I'm trying to implement Ajax system in my coursework in order to update the html table without updating the whole page. My HTML table is basically the table from my database, so Ajax should update the MySQl table, but how can I refresh the actual table on webpage? Another question is can I use different .php files for each action and just call them with ajax? How can I display the forms in the message box for adding the row or should I just redirect to a new page where the will be the forms for a new row?
Here is my table:
<?php
include("audentificate.php");
include ('db.php');
$query = "SELECT * FROM Trains";
$result = mysqli_query($conn, $query);
?>
<html>
<head>
<link href = "style.css" rel = "stylesheet" type = "text/css">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Departures table for workers </title>
</head>
<body>
<div class="table" align="center">
<div style = "background-color:#FFFFFF; padding:20px;"><b>Departures Table for workers</b></div>
<table border="1" align="center" width="700">
<thead>
<tr>
<th>Change row</th>
<th>ID</th>
<th>Train Company</th>
<th>Destination</th>
<th>Time</th>
<th>Platform</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_assoc($result)): ?>
<tr>
<td><input id = 'tick' type="checkbox" name="name1" /> </td>
<td><?php echo $row['id']?></td>
<td><?php echo $row['Traincompany'] ?></td>
<td><?php echo $row['Destination'] ?></td>
<td><?php echo $row['Time'] ?></td>
<td><?php echo $row['Platform'] ?></td>
<td><?php echo $row['Date'] ?></td>
</tr>
<?php endwhile ?>
</tbody>
</table>
<p class="message">Logout from userLogout
</div>
</body>
</html>

Using AJAX to Send Data on Button Click

I have a table with multiple columns (index.php). One column is a checkbox. Whenever the checkbox is checked, it displays another column where you can select a quantity.
I want to be able to hit a button called "Add to Order" and have it use AJAX to add any selected rows to the index-order.php page. After all selections have been made, I want to be able to click the "Checkout" button and have it take me to the index-order.php page where it should be displaying all of the added selections in a table.
I know how to navigate to the index-order.php page so my main question is how can I add the selected rows to the index-order.php page by using ajax when pressing the button?
Index.php code:
<form name="form1" action="index-order.php">
<section id="checkout-btn">
<button type="submit" id="checkout" name="order">Checkout</button>
</section>
</form>
<form name="form2" method="POST" action="index-order.php">
<section id="addToOrder">
<button type="submit" class="order" id="order" name="order" value="AddToOrder">Add to Order</button>
</section>
<br>
<table id="merchTable" cellspacing="5" class="sortable">
<thead>
<tr class="ui-widget-header">
<th class="sorttable_nosort"></th>
<th class="sorttable_nosort">Loc</th>
<th class="merchRow">Report Code</th>
<th class="merchRow">SKU</th>
<th class="merchRow">Special ID</th>
<th class="merchRow">Description</th>
<th class="merchRow">Quantity</th>
<th class="sorttable_nosort">Unit</th>
<th style="display: none;" class="num">Quantity #</th>
</tr>
</thead>
<tbody>
<?php foreach ($dbh->query($query) as $row) {?>
<tr>
<td class="ui-widget-content"><input type="checkbox" class="check" name="check"></td>
<td name="rows[0][0][loc]" class="loc ui-widget-content" id="loc-<?php echo intval ($row['Loc'])?>"><?php echo $row['Loc'];?></td>
<td name="rows[0][0][rp-code]" class="rp-code ui-widget-content" align="center" id="rp-code-<?php echo intval ($row['Rp-Code'])?>"><?php echo $row['Rp-Code'];?></td>
<td name="rows[0][0][sku]" class="sku ui-widget-content" id="sku-<?php echo intval ($row['SKU'])?>"><?php echo $row['SKU'];?></td>
<td name="rows[0][0][special-id]" class="special-id ui-widget-content" align="center" id="special-id-<?php echo intval ($row['Special-ID'])?>"><?php echo $row['Special-ID'];?></td>
<td name="rows[0][0][description]" class="description ui-widget-content" id="description-<?php echo intval ($row['Description'])?>"><?php echo $row['Description'];?></td>
<td name="rows[0][0][quantity]" class="quantity ui-widget-content" data-quantity="<?php echo $row['Quantity'] ?>" align="center" id="quantity-<?php echo intval ($row['Quantity'])?>"><?php echo $row['Quantity'];?></td>
<td name="rows[0][0][unit]" class="unit ui-widget-content" id="unit-<?php echo intval ($row['Unit'])?>"><?php echo $row['Unit'];?></td>
<td name="rows[0][0][quant]" style="display: none;" class="quantity_num ui-widget-content"><input type="textbox" style="width: 100px;" class="spinner" name="value" id="test"></td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
<div id="log"></div>
Index-order.php:
<?php if(isset($_POST['rows'])): ?>
<table cellspacing="20">
<tr align="center">
<th>Loc</th>
<th>Report Code</th>
<th>SKU</th>
<th>Special ID</th>
<th>Description</th>
<th>Quantity</th>
<th>Unit</th>
<th>Quantity #</th>
</tr>
<?php
foreach($_POST['rows'][0] as $row):
?>
<tr align="center">
<td><?php echo $row['loc']; ?></td>
<td><?php echo $row['rp-code']; ?></td>
<td><?php echo $row['sku']; ?></td>
<td><?php echo $row['special-id']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['unit']; ?></td>
<td><?php echo $row['quant']; ?></td>
</tr>
<?php
endforeach;
?>
</table>
<?php endif; ?>
JavaScript:
$(function () {
$("#order").click(function() {
//reset the logger
$('#log').empty();
$('#merchTable input:checkbox:checked').each(function() {
//for each checked checkbox, iterate through its parent's siblings
var array = $(this).parent().siblings().map(function() {
return $(this).text().trim();
}).get();
console.log(array);
//to print the value of array
$('#log').append(JSON.stringify(array))
var request = $.ajax({
type: 'post',
url: 'index-order.php',
data: array,
success: function(){
alert('success');
},
error: function(){
alert('failure');
}
});
})
});
});
To pass data from one page to another using php you will have to use a session.
So in your ajax call just save your data into the session like so:
add-to-cart.php
session_start();
if (!isset($_SESSION['shopping_cart'])) {
$_SESSION['shopping_cart'] = [];
} else {
$_SESSION['shopping_cart'][] = [
'id' => $_POST['item_id'],
'amount' => $_POST['amount']
];
}
If you then click your checkout button and navigate via a page reload to your index-order.php, you could then access the data via the same session index.
session_start();
if (isset($_SESSION['shopping_cart'])) {
// display your data
} else {
echo "Your shopping cart is empty.";
}

Categories

Resources