jquery or php if X is greater than Y - javascript

I'm making a website that displays a basic crud that shows Companyname, Stock, Minumum, Maximum.
I would like to display an alert or a message somewhere if the Stock is greater than the maximum. How could I go about doing this? Here is my display page code.
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Company Name</th>
<th>Stock</th>
<th>Minumum</th>
<th>Maximum</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['companyname']; ?></td>
<td><?php echo $row['stock']; ?></td>
<td><?php echo $row['minumum']; ?></td>
<td><?php echo $row['maximum']; ?></td>
<td><a class="btn btn-info" href="update1.php?id=<?php echo $row['id']; ?>">Edit</a> <a class="btn btn-danger" href="delete1.php?id=<?php echo $row['id']; ?>">Delete</a></td>
</tr>

You can put an additional message in the Stock column with an if statement.
<td><?php echo $row['stock']; ?></td>
with
<td><?php echo $row['stock']; if ($row['stock'] > $row['maximum']) { echo " > maximum"; } ?></td>

Related

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>

Extract data from a table created by a loop (PHP)

I have the following problem: I'm creating an HTML table with a PHP for-loop. There is data and a button in each row. When the user presses the button (id="detail"), the "id"-field of the corresponding row (id="patID") is supposed to be stored in a PHP variable. Every attempt I have made failed because javascript simply takes the first element on the page with the id "patID" and (to my knowledge) I don't have a way to select this element in PHP. This is my code:
<?php
if (isset($_POST['search']))
{
//irrelevant details of MySQL PDO-connection omitted
$result = $statement->fetchAll();
if ($result && $statement->rowCount() > 0)
{ ?>
<h2>Ergebnisse</h2>
<table>
<thead>
<tr>
<th>#</th>
<th>Vorname</th>
<th>Nachname</th>
<th>Geburtstag</th>
<th>Klasse/Kurs</th>
<th>Vorerkrankungen</th>
<th>Allergien</th>
<th>Anmerkung</th>
<th>Aktualisiert</th>
<th> Optionen</th>
</tr>
</thead>
<tbody>
<?php
foreach ($result as $row)
{ ?>
<tr id="row">
<td id="patID"><?php echo escape($row["id"]); ?></td>
<td><?php echo escape($row["firstName"]); ?></td>
<td><?php echo escape($row["lastName"]); ?></td>
<td><?php echo escape($row["birthday"]); ?></td>
<td><?php echo escape($row["course"]); ?></td>
<td><?php echo escape($row["preIllnesses"]); ?></td>
<td><?php echo escape($row["allergies"]); ?> </td>
<td><?php echo escape($row["note"]); ?> </td>
<td><?php echo escape($row["created"]); ?> </td>
<td>
<button id="detail">Mehr</button>
</td>
</tr>
<? } ?>
</tbody>
</table>
<?php
}
else
{ ?>
<blockquote><b>Keine Ergebnisse gefunden.</b></blockquote>
<?php
}
}
?>
The 'id' attribute can only be given once to an html-element. You should you a class instead:
<button class="detail">Mehr</button>
You should store the id in an extra attribute if you want to grab it later in javascript. For example, if you are using jQuery, you can use a data attribute:
<button class="detail" data-id="some_id">Mehr</button>
<script>
$('.detail').click(function () {
var id = $(this).data('id');
// do something with id
});
</script>
This will add a click-listener to all elements with the 'detail' class. So if a user clicks on an element having that class , it will execute the given function with this pointing to the clicked element. And since that element has the 'data-id' attribute, we can use jQuery's data function to get the contents of that attribute.
Your way if you change ID to class
<tr class="row">
<td class="patID"><?php echo escape($row["id"]); ?></td>
<td><?php echo escape($row["firstName"]); ?></td>
<td><?php echo escape($row["lastName"]); ?></td>
<td><?php echo escape($row["birthday"]); ?></td>
<td><?php echo escape($row["course"]); ?></td>
<td><?php echo escape($row["preIllnesses"]); ?></td>
<td><?php echo escape($row["allergies"]); ?> </td>
<td><?php echo escape($row["note"]); ?> </td>
<td><?php echo escape($row["created"]); ?> </td>
<td>
<button class="detail">Mehr</button>
</td>
</tr>
<script>
$(function() {
$('.detail').click(function(){
// var id = $(this).parents('.row').find('.patID').html();
alert($(this).parents('.row').find('.patID').html());
});
});
</script>

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>

jquery how can I get data from html table row

I have the following table:
<table cellspacing="0" cellpadding="0" id="product">
<tr>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th colspan="2">Nr products</th>
</tr>
<?php foreach ($productsInStock as $product) : ?>
<tr>
<td><?php echo $product->getName(); ?></td>
<td><?php echo $product->getCategory(); ?></td>
<td><?php echo $product->getPrice().ProductController::coin; ?></td>
<td><?php echo $product->getNrProducts(); ?></td>
<td><button type="submit" value="Delete" class="upload" onclick="deleteDataTable();">Delete</button></td>
<input type="hidden" name="hiddenfieldname" class="hidden" value="<?php echo $product->getId();?>">
</tr>
<?php endforeach; ?>
</table>
I need the value for every hidden field but I only get the first :
x = ('.hidden').val() // gives the first value
How can I get the different values after every click on delete button
Simplest solution is pass the ID as parameter to deleteDataTable() function.
<td><button type="submit" value="Delete" class="upload" onclick="deleteDataTable(<?php echo $product->getId();?>);">Delete</button></td>
The problem is: Context.
When you use
x = $('.hidden')
you get all elements with class 'hidden'. .val() then gets the value from the first.
You need to limit the hidden input to the one on the same row as the delete button.
Unfortunately, your current hidden is not actually inside the table and you have this:
<table><tr>...</tr><tr>..</tr>
<input type="hidden"...>
<input type="hidden"...>
You need to change your markup to:
<tr>
<td><?php echo $product->getName(); ?></td>
<td><?php echo $product->getCategory(); ?></td>
<td><?php echo $product->getPrice().ProductController::coin; ?></td>
<td><?php echo $product->getNrProducts(); ?></td>
<td>
<button type="submit" value="Delete" class="upload" onclick="deleteDataTable();">Delete</button>
<input type="hidden" name="hiddenfieldname" class="hidden" value="<?php echo $product->getId();?>">
</td>
</tr>
or similar so that the input is inside a td.
You can then use relative elements on the delete click:
function deleteDataTable()
{
var x = $(this).closest("tr").find(".hidden").val();
}
or, using the amended html above, use .next but I would keep the above
var x = $(this).next(".hidden").val();
Maybe you are looking for this:
var x=[];
$('.hidden').each(function(){
x.push($(this).val());
});
x should contain all the values.
You can simply use this:
<tr>
<td><?php echo $product->getName(); ?></td>
<td><?php echo $product->getCategory(); ?></td>
<td><?php echo $product->getPrice().ProductController::coin; ?></td>
<td><?php echo $product->getNrProducts(); ?></td>
<td>
<button type="submit" value="Delete" class="upload getValue" data-product-id="<?php echo $product->getId();?>">Delete</button>
</td>
</tr>
<script>
$('.getValue').click(function(){
var val = $(this).data('product-id');
});
</script>
You can like that
<table cellspacing="0" cellpadding="0" id="product">
<tr>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th colspan="2">Nr products</th>
</tr>
<?php foreach ($productsInStock as $product) : ?>
<tr data-productid="<?php echo $product->getId();?>">
<td><?php echo $product->getName(); ?></td>
<td><?php echo $product->getCategory(); ?></td>
<td><?php echo $product->getPrice().ProductController::coin; ?></td>
<td><?php echo $product->getNrProducts(); ?></td>
<td><button type="submit" value="Delete" class="upload" onclick="deleteDataTable();">Delete</button></td>
</tr>
<?php endforeach; ?>
</table>
Script
function deleteDataTable()
{
var productIdOfCurrentTR = $(this).closest("tr").data("productid");
}

HTML Tags not rendered properly when passing to view PHP-JS

Hi i am using codeigniter framework ,
i am using an ajax request to get data from db and to show in view.
var from_date = jQuery('#form_date').val();
var to_date = jQuery('#to_date').val();
jQuery.ajax({
url:base_url+"index.php/eod_report/search_to_date",
type:"POST",
data:{from_date:from_date,to_date:to_date},
//datatype:'json',
success:function(data){
jQuery("#report_container").html(data);
}
my controler is
public function search_to_date()
{
$from_date = $this->input->post('from_date');
$to_date = $this->input->post('to_date');
$where_array = array('eod.created_time >='=>$from_date,'eod.created_time <='=>$to_date);
$eod_report_data = $this->eod_data->get_eod_report_data($where_array);
$eod_total_report_data = $this->eod_data->get_eod_total_report_data($where_array);
$formated_eod_report_data = $this->format_eod_report_array($eod_report_data);
krsort($formated_eod_report_data);
$formated_eod_all_report_data= $this->format_all_eodreport_array($eod_total_report_data);
krsort($formated_eod_all_report_data);
$data = array('eod_data'=>$formated_eod_report_data,'eod_all_data'=>$formated_eod_all_report_data);
$html = $this->load->view('eod-report-partial',$data,true);
echo json_encode($html);
}
eod-report-partial view
<table width="100%" align="center" border="1">
<thead>
<tr>
<td>ShopCode</td>
<td>Submitted By</td>
<td>RMS</td>
<td>ORM</td>
<td>ORM Profit</td>
<td>Total</td>
<td>Cash</td>
<td>Card</td>
<td>Opening Balance</td>
<td>Purchase Today</td>
<td>Final Total</td>
<td>T.Till Cash</td>
<td>Difference</td>
<td>Nxt Day Op Balance</td>
<td>Petty Cash</td>
<td>Banking</td>
</tr>
</thead>
<tbody>
<?php foreach($eod_data as $key=>$eods) {?>
<tr><td colspan="8""><?php echo $key; ?></tr>
<?php foreach($eods as $eod) { ?>
<tr>
<td><?php echo $eod['shop_code']; ?></td>
<td><?php echo $eod['first_name']; ?></td>
<td><?php echo $eod['rms_sell'];?></td>
<td><?php echo $eod['orm_repair']; ?></td>
<td><?php echo $eod['orm_repair'];?></td>
<td><?php echo $eod['total']; ?></td>
<td><?php echo $eod['cash']; ?></td>
<td><?php echo $eod['card']; ?></td>
<td><?php echo $eod['opening_bal']; ?></td>
<td><?php echo $eod['purchases']; ?></td>
<td><?php echo $eod['final_total']; ?></td>
<td><?php echo $eod['till_total']; ?></td>
<td><?php echo $eod['difference']; ?></td>
<td><?php echo $eod['nextday_opening_bal']; ?></td>
<td></td>
<td><?php echo $eod['banking']; ?></td>
</tr>
<?php } ?>
<tr>
<td></td>
<td>Total:</td>
<td><?php echo $eod_all_data[$key]['sum_rms_sell'];?></td>
<td><?php echo $eod_all_data[$key]['sum_orm_repair']; ?></td>
<td><?php echo $eod_all_data[$key]['sum_orm_repair'];?></td>
<td><?php echo $eod_all_data[$key]['total']; ?></td>
<td><?php echo $eod_all_data[$key]['sum_cash']; ?></td>
<td><?php echo $eod_all_data[$key]['sum_card']; ?></td>
<td><?php echo $eod_all_data[$key]['sum_opening_bal']; ?></td>
<td><?php echo $eod_all_data[$key]['sum_purchases']; ?></td>
<td><?php echo $eod_all_data[$key]['final_total']; ?></td>
<td><?php echo $eod_all_data[$key]['sum_till_total']; ?></td>
<td><?php echo $eod_all_data[$key]['difference']; ?></td>
<td><?php echo $eod_all_data[$key]['sum_next_day_bal']; ?></td>
<td></td>
<td><?php echo $eod_all_data[$key]['sum_banking']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
the problem is the view rendering to the page is
how to render my html properly , thanks in advance
You need to decode the json object before setting it as the html in the container.
var html_data = jQuery.parseJSON(data);
jQuery("#report_container").html(html_data);
Please note the use of jQuery.parseJSON function for backwards compatibility for browsers that don't have a JSON object.
Also you have a typo in your from_date variable. It reads #form_date where it should read #from_date.
EDIT
Here's a dirty way to deal with it for now:
html_data = jQuery.parseJSON(data);
html_data = html_data.replace("\r\n", "").replace("\", "");
jQuery("#report_container").html(html_data);

Categories

Resources