How to change each html table row accordian - javascript

I want to create a dynamic table which gets its all its entries coming from database. I know how to make a simple table accordian by this Twitter Bootstrap Use collapse.js on table cells [Almost Done] .
If I apply that technique it turns table rows into accordian but no matter which row i click it only expands first row.
Here is the code of my table getting data from database.
<div class="container">
<h2>All the Suggestions of HR</h2>
<div class="table-responsive">
<table id="myTable" width="100%" class="table table-striped display responsive nowrap table-hover">
<thead>
<tr>
<th scope="col">Status</th>
<th scope="col">Date</th>
<th scope="col">Time</th>
<th scope="col">Subject</th>
<th scope="col">Message</th>
<th scope="col">department</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM hr;";
$result = mysqli_query($conn , $sql);
$nr = mysqli_num_rows($result);
while($row = mysqli_fetch_assoc($result)) :
$id = $row['hr_id'];
?>
<tr>
<?php $timestamp = $row['timeSent'];
$date = date('d-m-Y',strtotime($timestamp));
$time = date('H:i:s',strtotime($timestamp));
$status = $row['isAnswered'];
?>
<td><?php if ($status == 'true') {
echo "Answerd";
}
else{
echo "Pending";
} ?></td>
<td><?php echo $date;?></td>
<td><?php echo $time;?></td>
<td><?php echo $row['subject'];?></td>
<td><?php echo $row['message'];?></td>
<td><?php
$cat = $row['category_id'];
if($cat == 1){
echo "Suggestion";
}
elseif ($cat == 2) {
echo "Claim";
}
else{
echo "Help";
}?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
Whenever a row is clicked there is a content i want to show which is different for every row.

<?php
while($row = mysqli_fetch_assoc($result)) :
$id = $row['hr_id'];
?>
<tr data-toggle="collapse" data-target="#accordian_<?php echo $id; ?>" class="accordion-toggle">
<?php $timestamp = $row['timeSent'];
$date = date('d-m-Y',strtotime($timestamp));
$time = date('H:i:s',strtotime($timestamp));
$status = $row['isAnswered'];
?>
<td><?php echo $status == 'true' ? 'Answerd' : 'Pending'; ?></td>
<td><?php echo $date;?></td>
<td><?php echo $time;?></td>
<td><?php echo $row['subject'];?></td>
<td><?php echo $row['message'];?></td>
<td><?php
$cat = $row['category_id'];
if($cat == 1){
echo "Suggestion";
}
elseif ($cat == 2) {
echo "Claim";
}
else{
echo "Help";
}?></td>
</tr>
<tr >
<td colspan="6" class="hiddenRow">
<div class="accordian-body collapse" id="accordian_<?php echo $id; ?>"> Your Text here</div>
</td>
</tr>
<?php endwhile; ?>

Related

jquery or php if X is greater than Y

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>

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 output specific information in a modal from a database

I am trying to display more information about the user in my table. The goal is to see more information about the user in a modal, than you can see in the table overview. Unfortunately, I don't manage to display only the information of the respective user in the modal. I already have the following code which doesn't work like i want. At the moment, the modal stays emtpy.
Greetings
admin.php
<?php
include('connection.php');
if( !empty($_POST) ){
foreach( $_POST as $key_post => $value_post ){
if (preg_match('/delete_/i',$key_post) ) {
$id_to_delete = (integer) str_replace('delete_','', $key_post);
$deleteQuery = "DELETE FROM card WHERE id = " . $id_to_delete;
$statement = $pdo->prepare($deleteQuery);
$statement->execute();
}
}
}
include('read.php');
if( !empty($_POST) ){
foreach( $_POST as $key_post => $value_post ){
if (preg_match('/viewDetail_/i',$key_post) ) {
$id_to_view = (integer) str_replace('viewDetail_','', $key_post);
$viewQuery = "SELECT * FROM card WHERE id = " . $id_to_view;
$statement = $pdo->prepare($viewQuery);
$statement->execute();
$viewResult = $statement->fetchAll();
}
}
}
?>
<form action="admin.php" method="post">
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
<th>Edit</th>
</tr>
<?php
foreach ($result as $row) {
if ($row['Dispatched'] == 0) {
$dispatched = 'Pending';
} else {
$dispatched = 'Versendet';
}
?>
<tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
<td>
<input type="submit" name="delete_<?php echo $row['ID']; ?>" value="delete" >
<input type="submit" name="viewDetail_<?php echo $row['ID']; ?>" value="modal" data-target="modal1" class="modal-trigger">
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</form>
<br>
<div class="row">
<button class="btn waves-effect waves-light" type="submit" name="action">Absenden
<i class="material-icons right">send</i>
</button>
</div>
<div id="modal1" class="modal">
<div class="modal-content">
<h4>Modal Header</h4>
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
</tr>
<?php
foreach ($viewResult as $row){ ?>
<tr>
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</div>
<div class="modal-footer">
Zurück
</div>
</div>
read.php
<?php
include('connection.php');
$statement = $pdo->prepare("SELECT * FROM card ORDER BY ID ASC");
$statement->execute();
$result = $statement->fetchAll();
if ($statement->rowCount() > 0) {
foreach ($statement->fetchAll() as $row) {
$id = $row['ID'];
$imagePath = $row["ImagePath"];
$sender = $row["Sender"];
$senderEmail = $row["SenderEmail"];
$receiver = $row["Receiver"];
$receiverEmail = $row["ReceiverEmail"];
$subject = $row["Subject"];
$text = $row["Text"];
$sendDate = $row["SendDate"];
$dispatched = $row["Dispatched"];
$category = $row['Category'];
}
}
?>

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>

Filtering rows of php generated table using a select dropdown

I have a table that looks like this Table
The table rows are filled from a database. Here is the following code.
<select name="tipo" id="tipoProveedor" data-col="6" class="form-control">
<option value="todos">Todos</option>
<?php
$tipo_values = array(
'B'=>'Bienes',
'S'=>'Servicios',
);
foreach($tipo_values as $value => $display_text)
{
$selected = ($value == $this->input->post('tipo')) ? ' selected="selected"' : "";
echo '<option value="'.$value.'" '.$selected.'>'.$display_text.'</option>';
}
?>
</select>
<table id="table" class="table table-striped">
<thead>
<tr>
<th>Razón Social</th>
<th>Dirección</th>
<th>Contacto</th>
<th>Teléfono Fijo</th>
<th>Teléfono Móvil</th>
<th>Correo Electrónico</th>
<th>Tipo</th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach($listaproveedor as $p){ ?>
<tr class="<?php echo $p['tipo']; ?>">
<td><?php echo $p['razonSocial']; ?></td>
<td><?php echo $p['direccion']; ?></td>
<td><?php echo $p['nombre1']; ?></td>
<td><?php echo $p['telefonoFijo1']; ?></td>
<td><?php echo $p['telefonoMovil1']; ?></td>
<td><?php echo $p['correoElectronico1']; ?></td>
<td><?php echo $p['tipo']; ?></td>
<td>
<a title="Editar" href="<?php echo site_url('proveedor/edit/'.$p['id']); ?>" class="btn btn-info btn-xs"><span class="fa fa-pencil"></span></a>
<a title="Eliminar" href="<?php echo site_url('proveedor/remove/'.$p['id']); ?>" class="btn btn-danger btn-xs"><span class="fa fa-trash"></span></a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
Above the table there is a select dropdown to filter the rows.
It has 3 options
Todos (All)
Bienes
Servicios
The filtering should be applied to the column Tipo (the one circled in red in the picture).
Option "Todos" should show all the rows
Option "Bienes" should show all the rows with the letter B in "Tipo" column.
Option "Servicios" should show all the rows with the letter S in "Tipo" column
EDIT: I edited the html code for the table and added the following script.
Now it works!
$('.familiaOcultar').addClass('collapse');
$('#tipoProveedor').change(function(){
$('.familiaOcultar').collapse('hide');
$('.familiaOcultar.B').hide();
var val = $(this).val();
if (val == "todos"){
$('.B td').show();
$('.S td').show();
$('.familiaOcultar.B').hide();
} else if (val == 'B'){
$('.B td').show();
$('.S td').hide();
$('.familiaOcultar.B').show();
} else {
$('.B td').hide();
$('.S td').show();
$('.familiaOcultar.B').hide();
}
});

Categories

Resources