<?php for($i=0; $i< mysqli_num_rows($result); $i++){ ?>
<table id="t01" style="width:100%">
<tr>
<th colspan="2"><?php print_r($results[$i]['arr_question']); ?></th>
</tr>
<tr>
<td><?php print_r($results[$i]['a']); ?></td>
<td><?php print_r($results[$i]['b']); ?></td>
</tr>
<tr>
<td><?php print_r($results[$i]['c']); ?></td>
<td><?php print_r($results[$i]['d']); ?></td>
</tr>
<tr>
<td colspan="2"><button type="button" onClick="document.getElementById('').innerHTML= '<?php print_r($results[$i]['answer']); ?>'"> Answer </button>
<p id=''></p>
</td>
</tr>
<tr> <td colspan="2"><?php print_r($results[$i]['description']); ?></td>
</tr>
</table>
</br>
<?php } ?>
In this code document.getElementById('').innerHTML is not working properly, it returns the same value while clicking on button in loop, when i put id equals $i it return the value in same place at every loop, it overrides the data at same place, when i put any static value it returns same. What should i do for getting the different value at different places. Any help will be appreciable.
You cannot have empty ids, assign an id as follows:
<p id='p-<?php echo $i; ?>'></p>
And use it as follows in the query selector:
<button type="button" onClick="document.getElementById('p-<?php echo $i; ?>').innerHTML= '<?php print_r($results[$i]['answer']); ?>'"> Answer </button>
Related
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>
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
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>
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>
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");
}