how to print data in php with css and js? - javascript

Above Image is image of invoice before print it look good with css and js.
and this is the image after i print it, it not look good as before printing so please help me to take print same as Image 1 ['Before Print'].
CSS
<link rel="stylesheet" type="text/css" href="css/custom_css/invoice.css">
CODE:
<aside class="right-side">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>Invoice</h1>
<ol class="breadcrumb">
<li>
<a href="index.html">
<i class="fa fa-fw fa-home"></i> Dashboard
</a>
</li>
<li> Pages</li>
<li class="active">
Invoice
</li>
</ol>
</section>
<!-- Main content -->
<section class="content p-l-r-15" id="invoice-stmt">
<div class="panel panel-primary">
<div class="panel-body">
<div class="row printableArea">
<h1 align="center">National Battery Centre</h1>
<h4 align="center" style="text-transform:uppercase;" > Tax Invoice </h4>
<div class="col-md-12">
<div class="col-md-6 col-sm-12 col-xs-12">
<h4><strong>Jameel Khan</strong></h4>
<address>
<br/> Shop No : 1, Patel Tower
<br/> Bardoli, Dist - Surat
<br/> +91 9426890590
</address>
</div>
<?php
$query = mysql_query("SELECT * FROM customer_mst WHERE CUSTOMER_ID = '".$_SESSION['cust']."'");
$row = mysql_fetch_array($query);
$query_cart = mysql_query("SELECT * FROM final_invoice_mst WHERE CUSTOMER_ID = '".$_SESSION['cust']."' AND DATE = '".$_SESSION['date']."' AND FLAG = '0'");
$row_cart = mysql_fetch_array($query_cart);
?>
<div class="col-md-6 col-sm-12 col-xs-6 text-right">
<div class="pull-right">
<h4><strong><?php echo $row['CUSTOMER_NAME']; ?></strong></h4>
<address>
<br/> <?php echo $row['CUSTOMER_ADDRESS']; ?>
<br/> <?php echo $row['CUSTOMER_CITY'] ?>
<br/> <?php echo $row['CUSTOMER_CONTACT'] ?>
<br/> <?php echo $row['VEHICLE_NO'] ?>
</address>
</div>
</div>
</div>
<div class="col-md-12">
<div class="pull-right">
<font color="#000000" size="4">Bill No : <?php echo $row_cart['BILL_NO'] ?></font>
</div>
<div class="pull-left">
<font color="#000000" size="4">Date : <?php echo $_SESSION['date'] ?></font>
</div>
</div>
<div class="col-md-12">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr class="bg-primary">
<th width="10%"><strong>Sr No.</strong></th>
<th class="text-left"><strong>Item Name</strong></th>
<th class="text-left"><strong> Serial No </strong></th>
<th class="text-right"><strong> CGST </strong></th>
<th class="text-right"><strong> SGST </strong></th>
<th class="text-right"><strong> Net Amount </strong></th>
</tr>
</thead>
<tbody>
<?php
$query_cart1 = mysql_query("SELECT * FROM final_invoice_mst, item_details WHERE final_invoice_mst.CUSTOMER_ID = '".$_SESSION['cust']."' AND final_invoice_mst.FLAG = '0' AND final_invoice_mst.ITEM_NAME = item_details.ITEM_NAME");
$counter = 0;
while($row_cart1 = mysql_fetch_array($query_cart1))
{
?>
<tr>
<td ><?php echo ++$counter; ?></td>
<td class="text-left"><?php echo $row_cart1['ITEM_NAME']; ?></td>
<td class="text-left"><?php echo $row_cart1['SERIAL_NO'];?></td>
<td class="text-right"><?php echo $row_cart1['CGST'];?></td>
<td class="text-right"><?php echo $row_cart1['SGST'];?></td>
<td class="text-right"><?php echo $row_cart1['NAMT'];?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
<div class="col-md-12">
<?php
//Battery Inverter Both
$sum_cgst = mysql_query("SELECT SUM(CGST) FROM final_invoice_mst, item_details WHERE final_invoice_mst.CUSTOMER_ID = '".$_SESSION['cust']."' AND final_invoice_mst.FLAG = '0' AND final_invoice_mst.ITEM_NAME = item_details.ITEM_NAME AND item_details.TYPE = 'BAT'");
$row_sum_cgst = mysql_fetch_array($sum_cgst);
$sum_sgst = mysql_query("SELECT SUM(SGST) FROM final_invoice_mst, item_details WHERE final_invoice_mst.CUSTOMER_ID = '".$_SESSION['cust']."' AND final_invoice_mst.FLAG = '0' AND final_invoice_mst.ITEM_NAME = item_details.ITEM_NAME AND item_details.TYPE = 'BAT'");
$row_sum_sgst = mysql_fetch_array($sum_sgst);
$sum_namt = mysql_query("SELECT SUM(NAMT) FROM final_invoice_mst, item_details WHERE final_invoice_mst.CUSTOMER_ID = '".$_SESSION['cust']."' AND final_invoice_mst.FLAG = '0' AND final_invoice_mst.ITEM_NAME = item_details.ITEM_NAME AND item_details.TYPE = 'BAT'");
$row_sum_namt = mysql_fetch_array($sum_namt);
//Inverter
$isum_cgst = mysql_query("SELECT SUM(CGST) FROM final_invoice_mst, item_details WHERE final_invoice_mst.CUSTOMER_ID = '".$_SESSION['cust']."' AND final_invoice_mst.FLAG = '0' AND final_invoice_mst.ITEM_NAME = item_details.ITEM_NAME AND item_details.TYPE = 'INV'");
$irow_sum_cgst = mysql_fetch_array($isum_cgst);
$isum_sgst = mysql_query("SELECT SUM(SGST) FROM final_invoice_mst, item_details WHERE final_invoice_mst.CUSTOMER_ID = '".$_SESSION['cust']."' AND final_invoice_mst.FLAG = '0' AND final_invoice_mst.ITEM_NAME = item_details.ITEM_NAME AND item_details.TYPE = 'INV'");
$irow_sum_sgst = mysql_fetch_array($isum_sgst);
$isum_namt = mysql_query("SELECT SUM(NAMT) FROM final_invoice_mst, item_details WHERE final_invoice_mst.CUSTOMER_ID = '".$_SESSION['cust']."' AND final_invoice_mst.FLAG = '0' AND final_invoice_mst.ITEM_NAME = item_details.ITEM_NAME AND item_details.TYPE = 'INV'");
$irow_sum_namt = mysql_fetch_array($isum_namt);
if($rque1['TYPE'] == 'BAT' && $rque2['TYPE'] != 'INV')
{
?>
<div class="col-lg-9">
</div>
<div class="col-lg-3">
<table class="table">
<thead>
<tbody>
<tr>
<td>Net Amount</td>
<td class="text-left"><?php $inamt = $row_sum_namt['SUM(NAMT)']; echo "$inamt"; ?></td>
</tr>
<tr>
<td>CGST 14%</td>
<td class="text-left"><?php $icgst = $row_sum_cgst['SUM(CGST)']; echo "$icgst"; ?></td>
</tr>
<tr>
<td>SGST 14%</td>
<td class="text-left"><?php $isgst = $row_sum_sgst['SUM(SGST)']; echo "$isgst"; ?></td>
</tr>
<tr>
<td>IGST 28%</td>
<td class="text-left"> </td>
</tr>
<tr>
<td>TOTAL</td>
<td class="text-left"><?php $total = $inamt + $icgst + $isgst; echo round($total); ?></td>
</tr>
</tbody>
</table>
</div>
<?php
}
if($rque2['TYPE'] == 'INV' && $rque1['TYPE'] != 'BAT')
{
?>
<div class="col-lg-9">
</div>
<div class="col-lg-3">
<table class="table">
<tbody>
<tr>
<td>Net Amount</td>
<td class="text-left"><?php $inamt = $irow_sum_namt['SUM(NAMT)']; echo "$inamt"; ?></td>
</tr>
<tr>
<td>CGST 9%</td>
<td class="text-left"><?php $icgst = $irow_sum_cgst['SUM(CGST)']; echo "$icgst"; ?></td>
</tr>
<tr>
<td>SGST 9%</td>
<td class="text-left"><?php $isgst = $irow_sum_sgst['SUM(SGST)']; echo "$isgst"; ?></td>
</tr>
<tr>
<td>IGST 18%</td>
<td class="text-left"> </td>
</tr>
<tr>
<td>TOTAL</td>
<td class="text-left"><?php $total = $inamt + $icgst + $isgst; echo round($total); ?></td>
</tr>
</tbody>
</table>
</div>
<?php
}
if($rque1['TYPE'] == 'BAT' && $rque2['TYPE'] == 'INV')
{
?>
<div class="col-lg-6">
</div>
<div class="col-lg-3">
<table class="table">
<thead>
<tbody>
<tr>
<td>Net Amount</td>
<td class="text-left"><?php $inamt = $row_sum_namt['SUM(NAMT)']; echo "$inamt"; ?></td>
</tr>
<tr>
<td>CGST 14%</td>
<td class="text-left"><?php $icgst = $row_sum_cgst['SUM(CGST)']; echo "$icgst"; ?></td>
</tr>
<tr>
<td>SGST 14%</td>
<td class="text-left"><?php $isgst = $row_sum_sgst['SUM(SGST)']; echo "$isgst"; ?></td>
</tr>
<tr>
<td>IGST 28%</td>
<td class="text-left"> </td>
</tr>
<tr>
<td>TOTAL</td>
<td class="text-left"><?php $total = $inamt + $icgst + $isgst; echo round($total); ?></td>
</tr>
</tbody>
</table>
</div>
<div class="col-lg-3">
<table class="table">
<tbody>
<tr>
<td>Net Amount</td>
<td class="text-left"><?php $inamt = $irow_sum_namt['SUM(NAMT)']; echo "$inamt"; ?></td>
</tr>
<tr>
<td>CGST 9%</td>
<td class="text-left"><?php $icgst = $irow_sum_cgst['SUM(CGST)']; echo "$icgst"; ?></td>
</tr>
<tr>
<td>SGST 9%</td>
<td class="text-left"><?php $isgst = $irow_sum_sgst['SUM(SGST)']; echo "$isgst"; ?></td>
</tr>
<tr>
<td>IGST 18%</td>
<td class="text-left"> </td>
</tr>
<tr>
<td>TOTAL</td>
<td class="text-left"><?php $total = $inamt + $icgst + $isgst; echo round($total); ?></td>
</tr>
</tbody>
</table>
</div>
<?php
}
?>
<h4><Strong>Terms and conditions:</Strong></h4>
<ul>
<li>An invoice must accompany products returned for warantty</li>
<li>Balance due within 10 days of invoice date,1.5% interest/month thereafter.</li>
<li>All goods returned for replacement/credit must be saleable condition with original
packaging.
</li>
</ul>
</div>
</div>
<div class="btn-section">
<div class="col-md-12 col-sm-12 col-xs-12">
<span class="pull-right">
<form id="form1">
<input type="button" value="Print" class="btn btn-danger" onclick='PrintDiv();' id="print" />
</form>
</span>
</div>
</div>
</div>
</div>
</section>
<!-- /.content -->
</aside>
<script src="js/jquery.PrintArea.js" type="text/JavaScript"></script>
<script>
$(document).ready(function() {
$("#print").click(function() {
var mode = 'iframe'; //popup
var close = mode == "popup";
var options = {
mode: mode,
popClose: close
};
$("div.printableArea").printArea(options);
});
});
</script>

You can specify a print style in a different file and import it with:
<link rel="stylesheet" type="text/css" href="print.css" media="print">
The default media is all which applies for all devices, printers included, and I guess is the one you are using now. If that doesn't work you need to create a new CSS file.

After i tried all, I find best solution is open printing page another tab via target="_blank" or window.open(url).
For Hiding the url on top of the page
<style type="text/css" media="print">
#page {
size: auto; /* auto is the initial value */
margin: 0; /* this affects the margin in the printer settings */
}
</style>
And javascript for immediate open the print dialogue and close after print done
<script>
window.onload= function () {
window.print();
setTimeout(function(){
window.close();
},500 )
}
</script>

Related

Pass PHP variable with button to popup via javascript?

I have a table created dynamically with PHP to display requests. In each row of my table there is a button to open a popup. The ID of each request should be transferred to this popup in order to read out all data.
How can I make it so that in a dynamic table (all button names are the same) each button is differentiated, or recognized which button I pressed? And how can I pass the variable from PHP to the popup via javascript?
I would appreciate any help, I've been trying for days now, but I'm not getting any results.
Here's the table view
Popup window
<table id="meineTabelle" data-role="table" class="content"
data-mode="columntoggle" data-column-btn-text="Spalten">
<thead>
<div class="tablehead">
<tr>
<th class="thblackborder" data-priority=""></th>
<th class="thblackborder" data-priority="">1.Projektant</th>
<th class="thblackborder" data-priority=""></th>
<th class="thblackborder" data-priority="">2.Projektant</th>
<th class="thblackborder" data-priority=""></th>
<th class="thblackborder" data-priority="">3.Projektant</th>
<th class="thblackborder" data-priority=""></th>
<th class="thblackborder" data-priority="">4.Projektant</th>
<th class="thblackborder" data-priority=""></th>
<tr>
<th class="">ID</th>
<th class="">Vorname</th>
<th class="">Nachname</th>
<th class="">Vorname</th>
<th class="">Nachname</th>
<th class="">Vorname</th>
<th class="">Nachname</th>
<th class="">Vorname</th>
<th class="">Nachname</th>
<th class="">Titel</th>
<th class="">Standort</th>
<th class="">Klasse</th>
<th class="">Beginn</th>
<th class="">Abgabe</th>
<th class="">Beschreibung</th>
<th class="">Status</th>
<th class="">Erstellt</th>
</tr>
</tr>
</div>
</thead>
<tbody>
<?php
foreach ($Ausgabe as $row) {
?>
<form>
<tr onclick="dialogOeffnen('loslegen-dialog')">
<td>
<?php echo $row["ID"] . "<br>"; ?>
</td>
<td>
<?php echo $row["Vorname"] . "<br>"; ?>
</td>
<td>
<?php echo $row["Nachname"] . "<br>"; ?>
</td>
<td>
<?php echo $row["Vorname2"] . "<br>"; ?>
</td>
<td>
<?php echo $row["Nachname2"] . "<br>"; ?>
</td>
<td>
<?php echo $row["Vorname3"] . "<br>"; ?>
</td>
<td>
<?php echo $row["Nachname3"] . "<br>"; ?>
</td>
<td>
<?php echo $row["Vorname4"] . "<br>"; ?>
</td>
<td>
<?php echo $row["Nachname4"] . "<br>"; ?>
</td>
<td>
<?php echo $row["Titel"] . "<br>"; ?>
</td>
<td>
<?php echo $row["Standort"] . "<br>"; ?>
</td>
<td>
<?php echo $row["Klasse"] . "<br>"; ?>
</td>
<td>
<?php echo $row["Beginn"] . "<br>"; ?>
</td>
<td>
<?php echo $row["Abgabe"] . "<br>"; ?>
</td>
<td>
<center>Link</center>
</td>
<td>
<?php echo $row["Genehmigt"] . "<br>"; ?>
</td>
<td>
<?php echo $row["Erstellt"] . "<br>"; ?>
</td>
<td>
<input type="button" value="" onclick="">
</td>
</tr>
</form>
<?php
}
?>
</tbody>
</table>
Popup HTML
<div id="body-overlay"></div>
<div class="dialog" id="loslegen-dialog">
<a href="#" role="button" class="dialog-schließen-button" onclick="dialogSchliessen('loslegen-dialog')">
<i class="fas fa-times"></i>
</a>
<div class="textarea">
<h1><?php echo $row['ID'] ?></h1>
<textarea placeholder="Platz für Bemerkungen" name="Bemerkungen" id="" cols="30" rows="10"></textarea>
</div>
<form classaction="">
<div class="txt_field">
<input type="text" name="email" value="<?= $fetch_profile['email'];?>" required>
<span></span>
<label>E-Mail</label>
</div>
<div class="txt_field">
<input type="text" name="password" required>
<span></span>
<label>Passwort</label>
</div>
<input type="submit" value="Bestätigen" name="submit">
<div class="signup_link">
</form>
</div>
<script src="dialoge.js">
</script>
<script>
</script>
</body>
</html>
</body>
</html>
dialoge.js
function dialogOeffnen(dialogId) {
document.getElementById(dialogId).classList.add("sichtbar");
document.getElementById("body-overlay").classList.add("sichtbar");
}
function dialogSchliessen(dialogId) {
document.getElementById(dialogId).classList.remove("sichtbar");
document.getElementById("body-overlay").classList.remove("sichtbar");
}
If I echo out $row["ID"] for sure it shows me the last ID in my table
First, let's clean up your HTML a bit. Tables have predetermined structures. That means that a only some elements can be used as a parent or child. For example, you can't nest a <tr> inside a another <tr>. Learn about the basics of tables.
<table id="meineTabelle" data-role="table" class="content" data-mode="columntoggle" data-column-btn-text="Spalten">
<thead>
<tr>
<th>ID</th>
<th>Vorname</th>
<th>Nachname</th>
<th>Vorname</th>
<th>Nachname</th>
<th>Vorname</th>
<th>Nachname</th>
<th>Vorname</th>
<th>Nachname</th>
<th>Titel</th>
<th>Standort</th>
<th>Klasse</th>
<th>Beginn</th>
<th>Abgabe</th>
<th>Beschreibung</th>
<th>Status</th>
<th>Erstellt</th>
</tr>
</thead>
<tbody>
<?php foreach ($Ausgabe as $row) : ?>
<tr>
<td><?= $row["ID"] ?></td>
<td><?= $row["Vorname"] ?></td>
<td><?= $row["Nachname"] ?></td>
<td><?= $row["Vorname2"] ?></td>
<td><?= $row["Nachname2"] ?></td>
<td><?= $row["Vorname3"] ?></td>
<td><?= $row["Nachname3"] ?></td>
<td><?= $row["Vorname4"] ?></td>
<td><?= $row["Nachname4"] ?></td>
<td><?= $row["Titel"] ?></td>
<td><?= $row["Standort"] ?></td>
<td><?= $row["Klasse"] ?></td>
<td><?= $row["Beginn"] ?></td>
<td><?= $row["Abgabe"] ?></td>
<td>Link</td>
<td><?= $row["Genehmigt"] ?></td>
<td><?= $row["Erstellt"] ?></td>
<td>
<button class="dialog-open" type="button" value="<?= $row["ID"] ?>">Button</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I've changed your button to a <button> element because it's a bit more flexible to use. Output the ID of the row in the value attribute. Give the button a class value to identify it with. We'll need this for JS.
Select the table in JavaScript. Listen for click events that happen within the table. The code below checks if the clicked element (event.target) is a button with the class we gave it. If it is, then we'll read the value from button and pass it to the open modal function.
const table = document.querySelector('#meinTabelle');
table.addEventListener('click', event => {
if (event.target.matches('.dialog-open') {
const dialogId = event.target.value;
dialogOeffnen(dialogId);
}
});
function dialogOeffnen(dialogId) {
document.getElementById(dialogId).classList.add("sichtbar");
document.getElementById("body-overlay").classList.add("sichtbar");
}

highlight column differences

I need to Highlight columns that have different values.
If the value on $b['nama'] and $c['nama'] is not the same i need to highlight the <tr>. I wanted the system admin to easily see which of the field is having different values so the admin can decide to approve user edit request or to reject the request.
<?php
error_reporting(0);
$b = $data->row_array();
$c = $data2->row_array();
?>
<!-- Begin Page Content -->
<div class="container-fluid">
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">User requested changes</h6>
</div>
<div class="card-body">
<table id="tabel" class="table table-bordered table-striped" style="width: 75%">
<thead>
<tr>
<th>Variable</th>
<th>Current data</th>
<th>Requested changes</th>
</tr>
</thead>
<tbody>
<tr>
<td>Nama</td>
<td>
<?php echo $b['nama']; ?>
</td>
<td>
<?php echo $c['nama']; ?>
</td>
</tr>
<tr>
<td>NIP Baru</td>
<td>
<?php echo $b['nipBaru']; ?>
</td>
<td>
<?php echo $c['nipBaru']; ?>
</td>
</tr>
<tr>
<td>NIP lama</td>
<td>
<?php echo $b['nipLama']; ?>
</td>
<td>
<?php echo $c['nipLama']; ?>
</td>
</tr>
<tr>
<td>Gelar depan</td>
<td>
<?php echo $b['gelarDepan']; ?>
</td>
<td>
<?php echo $c['gelarDepan']; ?>
</td>
</tr>
<tr>
<td>Gelar belakang</td>
<td>
<?php echo $b['gelarBelakang']; ?>
</td>
<td>
<?php echo $c['gelarBelakang']; ?>
</td>
</tr>
<tr>
<td>Tempat lahir</td>
<td>
<?php echo $b['tempatLahir']; ?>
</td>
<td>
<?php echo $c['tempatLahir']; ?>
</td>
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-primary btn-sm" style="width:10%"><span class="icon-cursor"></span> Approve</button>
<button type="submit" class="btn btn-primary btn-sm" style="width:10%"><span class="icon-cursor"></span> Reject</button>
</div>
</div>
</div>
Check if those two values are different and apply a class to the row
<tr<?php if ($b['nama'] != $c['nama']) { echo ' class="highlight"'; } ?>>
<td>Nama</td>
<td>
<?php echo $b['nama']; ?>
</td>
<td>
<?php echo $c['nama']; ?>
</td>
</tr>
then use CSS to highlight the cells
.highlight td {
background: yellowgreen;
}
try this code
you can change the bgcolor value or you can create css class and inside of if you echo the class='my_class'
<?php
error_reporting(0);
$b = $data->row_array();
$c = $data2->row_array();
?>
<!-- Begin Page Content -->
<div class="container-fluid">
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">User requested changes</h6>
</div>
<div class="card-body">
<table id="tabel" class="table table-bordered table-striped" style="width: 75%">
<thead>
<tr>
<th>Variable</th>
<th>Current data</th>
<th>Requested changes</th>
</tr>
</thead>
<tbody>
<tr <?php if ($b['nama'] != $c['nama']){echo "bgcolor='#FF0000'";} ?>>
<td>Nama</td>
<td>
<?php echo $b['nama']; ?>
</td>
<td>
<?php echo $c['nama']; ?>
</td>
</tr>
<tr <?php if ($b['nipBaru'] != $c['nipBaru']){echo "bgcolor='#FF0000'";} ?> >
<td>NIP Baru</td>
<td>
<?php echo $b['nipBaru']; ?>
</td>
<td>
<?php echo $c['nipBaru']; ?>
</td>
</tr>
<tr <?php if ($b['nipBaru'] != $c['nipBaru']){echo "bgcolor='#FF0000'";} ?> >
<td>NIP lama</td>
<td>
<?php echo $b['nipLama']; ?>
</td>
<td>
<?php echo $c['nipLama']; ?>
</td>
</tr>
<tr <?php if ($b['gelarDepan'] != $c['gelarDepan']){echo "bgcolor='#FF0000'";} ?> >
<td>Gelar depan</td>
<td>
<?php echo $b['gelarDepan']; ?>
</td>
<td>
<?php echo $c['gelarDepan']; ?>
</td>
</tr>
<tr <?php if ($b['gelarBelakang'] != $c['gelarBelakang']){echo "bgcolor='#FF0000'";} ?> >
<td>Gelar belakang</td>
<td>
<?php echo $b['gelarBelakang']; ?>
</td>
<td>
<?php echo $c['gelarBelakang']; ?>
</td>
</tr>
<tr <?php if ($b['tempatLahir'] != $c['tempatLahir']){echo "bgcolor='#FF0000'";} ?> >
<td>Tempat lahir</td>
<td>
<?php echo $b['tempatLahir']; ?>
</td>
<td>
<?php echo $c['tempatLahir']; ?>
</td>
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-primary btn-sm" style="width:10%"><span class="icon-cursor"></span> Approve</button>
<button type="submit" class="btn btn-primary btn-sm" style="width:10%"><span class="icon-cursor"></span> Reject</button>
</div>
</div>
</div>
You can define a class named 'same' and 'different'. Then in php check if the values are same and echo the class property based on being same or different.
php
<tr>
<td>Nama</td>
<td class="<?php echo ($b['nama'] == $c['nama'])? 'same': 'different'; ">
<?php echo $b['nama']; ?>
</td>
<td class="<?php echo ($b['nama'] == $c['nama'])? 'same': 'different'; ">
<?php echo $c['nama']; ?>
</td>
</tr>
In Styles
.same{
color:green;
}
.different{
color:red;
}
We have used ternary operator to figure out if the variables are same or not.
https://www.abeautifulsite.net/how-to-use-the-php-ternary-operator
Since you seem to be using Bootstrap, you could just apply an already defined style to the cells to highlight the difference. For instance:
<tr>
<td>Nama</td>
<td class="<?php echo ($b['nama'] == $c['nama'])? 'bg-success': 'bg-danger'; ">
<?php echo $b['nama']; ?>
</td>
<td class="bg-<?php echo ($b['nama'] == $c['nama'])? 'bg-success': 'bg-danger'; ">
<?php echo $c['nama']; ?>
</td>
</tr>
By doing this, if $b['nama'] and $c['nama'] are different, the cells would have an near-red background and if they are equal, they'd have a near-green background. If you'd like to only highlight differences (in order to make the screen less overwhelming, just replace the 'bg-success' with '' so the equal cells don't get styled

PHP generated table with user input

I am working on a little project for a clan of mine, we just want a little dashboard to track stuff from the game.
Now what I am looking for is to be able to put notes into my generated table where we can put stuff when members are on leave and such.
Can anyone help me with this, I am genrating the table with all the data its just the note that is not working out for me.
<div id="content">
<div class="limiter">
<div class="container-table100">
<div class="wrap-table100">
<div class="table100 ver1 m-b-110">
<table data-vertable="ver1" id="myTable">
<tr class="row100 head">
<th class="column100 column1" ><b>Lid</b></th>
<th class="column100 column2"><b>Donaties Gedaan</b></th>
<th class="column100 column3"><b>Donaties Ontvangen</b></th>
<th class="column100 column4"><b>Donaties: Gedaan - Ontvangen</b></th>
<th class="column100 column5"><b>Kroonkist</b></th>
<th class="column100 column6"><b>Notities</b></th>
</tr>
<?php
foreach ($data['members'] as $key => $value): ?>
<tr class="row100">
<td class="column100 column1"><? echo $value['name']; ?></td>
<?php if(intval($value['donations'])<150) {
echo '<td class="column100 column2" style="background:red; color: white">'.$value['donations'].'</td>';
}
else {
echo '<td class="column100 column2">'.$value['donations'].'</td>';
}?>
<td class="column100 column3"><? echo $value['donationsReceived']; ?></td>
<td class="column100 column4"><? echo $value['donationsDelta']; ?></td>
<?php if(intval($value['clanChestCrowns'])<20) {
echo '<td class="column100 column2" style="background:red; color: white">'.$value['clanChestCrowns'].'</td>';
}
else {
echo '<td class="column100 column2">'.$value['clanChestCrowns'].'</td>';
}?>
<td>
<textarea class="column100 column5" id="verwittiging" placeholder="verwittiging-Test"></textarea>
<button type="button" id="saveNote" onclick="setNote()">Voeg notitie toe</button>
</td>
</tr>
<?
endforeach;
?>
</table>
</div>
</div>
</div>
</div>
This is the code how I get the data
<?php
$token = "apitoken";
$opts = [
"http" => [
"header" => "auth:" . $token
]
];
$context = stream_context_create($opts);
$test = file_get_contents("http://api.cr-api.com/clan/9QLUCV22",true,
$context);
$data = json_decode($test, true);
?>

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.";
}

Send Table Row Data via AJAX Based on Checkbox Checked

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 then 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 display 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 page while using ajax and staying on the current page when doing so?
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>
<div id="my-div2" class="ui-widget">
<div class="ui-widget">
<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>
</div>
</div>
</form>
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 (obviously needs some work. Posting what I have so far):
$(function () {
$('#form2').on('submit', function (e) {
if($('input.check').is(':checked')) {
$(this).closest('tr');
e.preventDefault();
var request = $.ajax({
type: 'post',
url: 'index-order.php',
data: $('#form2').serialize(),
success: function(){
alert('success');
},
error: function(){
alert('failure');
}
});
}
});
});
What you can do is get all the siblings of the current checked checkbox and based on that get all the columns and separate the data to finally send it via ajax.
You can do something like this:
$('#form2').on('submit', function (e) {
$checkbox = $(this).find('input.check:checked').parent('td'); //Finds the checked checkbox's column inside #form2
$otherColumns = $checkbox.nextAll(); //Gets all the siblings
});
The above code will get you all the other columns next to the checked checkbox inside your form. What you an do next is travel across $otherColumns and get each individual value using $otherColums.eq(n).html() where n is the number of the column you are trying to get.

Categories

Resources