I have issues with my table, I cannot use Javascript (which I know would be easier). I can only use HTML, PHP and CSS. This is my code that I currently have. The issues I need to resolve are the following:
I am able to add rows, delete and I am so also able to edit them by using the "contenteditable", however my issue is that every time I add a row or remove one, it refreshes the whole page. How can I fix this issue?
Also if there is a way to have an edit button instead of my conteneditable method.
Here is my code:
input {
display: block; /* makes one <input> per line */
width: 150px;
}
<?php
if( isset( $_REQUEST["btnadd"]) == "ADD") {
// add 1 to the row counter
if (isset($_REQUEST['count'])) $count = $_REQUEST['count'] + 1;
// set value for first load
else $count = 1;
}
if( isset( $_REQUEST["btnremove"]) == "REMOVE") {
// decrement the row counter
$count = $_REQUEST['count'] - 1;
// set minimum row number
if ($count < 1) $count = 1;
}
?>
<form name="form1">
<table class="table table-bordered table-striped table-hover text-center" align='center'>
<tr>
<th align="center">Name</th>
<th>Start </th>
<th>Size</th>
<th>First Condition</th>
<th>Second Conditon</th>
<th><input type="submit" name="btnadd" id="btnadd" value="ADD" align='center'></th>
</tr>
<?php
// print $count rows
for ($i=1; $i<=$count; $i++) {
echo ' <tr>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td> <input type="submit" name="btnremove" id="btnremove" value="REMOVE"></td>
</tr>
';
}
?>
</table>
<input type="hidden" name="count" value="<?php echo $count; ?>">
</form>
every time I add a row or remove one, it refreshes the whole page. How can I fix this issue?
Without Javascript you can't.
if there is a way to have an edit button instead of my conteneditable method
It's rather hard to suggest what your code should be since you don't ofer a description of what the code is intended to do, other than the code which doesn't do what you want. I think you mean something like:
<?php
$width=5;
$data=isset($_REQUEST['data']) ? $_REQUEST['data'] : array();
$count=count($data);
if (isset($_REQUEST['delete'])) {
foreach ($_REQUEST['delete'] as $i) {
unset($data[$i]);
}
}
$data=array_values($data);
if( isset( $_REQUEST["btnadd"]) == "ADD") {
// add 1 to the row counter
$data[]=array();
}
...
foreach ($data as $i=>$row) {
print "<tr>\n";
for ($x=0; $x<$width; $x++) {
#print "<td><input type='text' name='data[$i][$x]'></td>\n";
}
print "<td><input type='checkbox' name='delete[]' value='$i'>";
print "</tr>\n";
}
print "<tr>
<td colspan='$width'>
<input type="submit" name="btnadd" id="btnadd" value="Add">
</td>
</tr>\n";
Related
I'm currently working on displaying a table containing a list of alarms, where each row contains a checkbox that determines whether the user has already viewed that alarm or not. Here's
an image:
So far, I managed to check/uncheck the checkbox given its state in the database. What I want to do, and don't exactly know how to, allow the user to check/uncheck a checkbox and immediately update the database with this new state without pressing a submit button. The code that I have right now is the following.
<div class="card-body table-responsive p-0">
<form action=modules/alarms.php method="POST">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>Tipus</th>
<th>Data</th>
<th>Vista</th>
</tr>
</thead>
<tbody>
<?php
foreach ($result->fetchAll() as $row){
if ($row[4] == 1){
$status = "checked";
}else{
$status = "";
}
echo
'<tr data-widget="expandable-table" aria-expanded="false">
<td>'.$row[0].'</td>
<td>'.$row[1].'</td>
<td>'.$row[2].'</td>
<td><input type="checkbox" name="chk1[]" value="viewed" '.$status.'></td>
</tr>
<tr class="expandable-body">
<td colspan="5">
<p>
<video width="416" height="416" controls>
<!-- el 42 es la cabecera C:/... fins videos-->
<source src="'.substr($row[3], 42).'" type="video/mp4">
Your browser does not support the video tag.
</video>
</p>
</td>
</tr>';
}
?>
</tbody>
</table>
And the PHP code I have (I don't have the code that will update values on the database, but I'll figure that out. I want to know how I could retrieve the value of the checkbox of every row and its ID so I can update it on the database.
$ei = $_POST['chk1'];
if ($_POST["chk1"] == "chk1") {
for ($i = 0; $i < sizeof($checkbox1); $i++) {
print_r($checkboxl[$i]);
}
}
To solve this problem, first I had to add some fields to the checkbox input. WIth those changes, the table is generated as it follows:
<tr data-widget="expandable-table" aria-expanded="false">
<td>'.$row[0].'</td>
<td>'.$row[1].'</td>
<td>'.$row[2].'</td>
<td><input type="checkbox" name="id" id="viewed" value="'.$row[0].'" '.$status.'></td>
</tr>
After this is done, simply add a script that will retrieve the values from the clicked checkbox and pass them to the necessary PHP function:
$("input").change(function() {
if ($(this).is(':checked')){
var viewed=1;
}else{
var viewed = 0;
}
var id = $(this).val();
$.ajax({
url:"modules/alarms.php",
method:"POST",
data:{viewed:viewed,id:id,},
success: function(data){
},
});
Please note that the 'url' field is the relative path where our PHP function is implemented.
And now, simply update the database with the checkbox value (I'm using PDO objects) as it follows:
<?php
if(isset($_POST["viewed"])) {
$id = $_POST["id"];
$viewed = $_POST["viewed"];
$sql="UPDATE `alarms` SET `viewed` = '$viewed' WHERE (`id` = '$id')";
if($connection->query($sql) === TRUE){
echo "Success";
} else {
echo "error" . $sql . "<br>".$connection->error;
}}?>
I got a simple php page displaying my data from mysql table. I can edit it and it works perfect. But I want to be able to order the data by clicking on the headers of the column (ASC and DESC).
I know I have to add hyperlink to the headers, but the informations I found didn't help me.
<?php
include_once("config.php");
$result = mysqli_query($mysqli, "SELECT * FROM material ORDER BY date ASC");
?>
<html>
<head>
<title>Material</title>
</head>
<body>
<h2>Material</h2>
<input type="button" value="Home" onclick="document.location='../index.html';">
<input type="button" value="Add data" onclick="document.location='add.html';">
</br>
</br>
<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td><b>Count</b></td>
<td><b>Name</b></td>
<td><b>Place</b></td>
<td><b>Serialnumber</b></td>
<td><b>Last Check</b></td>
<td><b>Next Check</b></td>
<td><b>End of life Date</b></td>
<td><b>Update</b></td>
</tr>
<?php
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['count']."</td>";
echo "<td>".$res['name']."</td>";
echo "<td>".$res['place']."</td>";
echo "<td>".$res['serialnumber']."</td>";
echo "<td>".$res['last_check']."</td>";
echo "<td>".$res['next_check']."</td>";
echo "<td>".$res['date']."</td>";
echo "<td><button>Edit</button></td>";
}
?>
</table>
</body>
</html>
Please save yourself the trouble of using datatables. It's good but I don't personally enjoy using it because the tables are non-responsive. I rather use vanilla JavaScript and bootstrap for maximum responsiveness. I have not added Bootstrap in my response but it can be added easily.
You can achieve a sorting table easily client-side. Sort the data by clicking on table headers :
function sortTable(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById("myTable");
switching = true;
//Set the sorting direction to ascending:
dir = "asc";
/*Make a loop that will continue until
no switching has been done:*/
while (switching) {
//start by saying: no switching is done:
switching = false;
rows = table.rows;
/*Loop through all table rows (except the
first, which contains table headers):*/
for (i = 1; i < (rows.length - 1); i++) {
//start by saying there should be no switching:
shouldSwitch = false;
/*Get the two elements you want to compare,
one from current row and one from the next:*/
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
/*check if the two rows should switch place,
based on the direction, asc or desc:*/
if (dir == "asc") {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
//if so, mark as a switch and break the loop:
shouldSwitch= true;
break;
}
} else if (dir == "desc") {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
/*If a switch has been marked, make the switch
and mark that a switch has been done:*/
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
//Each time a switch is done, increase this count by 1:
switchcount ++;
} else {
/*If no switching has been done AND the direction is "asc",
set the direction to "desc" and run the while loop again.*/
if (switchcount == 0 && dir == "asc") {
dir = "desc";
switching = true;
}
}
}
}
table {
border-spacing: 0;
width: 100%;
border: 1px solid #ddd;
}
th {
cursor: pointer;
}
th, td {
text-align: left;
padding: 16px;
}
tr:nth-child(even) {
background-color: #f2f2f2
}
<table id="myTable">
<tr>
<!--When a header is clicked, run the sortTable function, with a parameter, 0 for sorting by names, 1 for sorting by country:-->
<th onclick="sortTable(0)">Name</th>
<th onclick="sortTable(1)">Country</th>
</tr>
<tr>
<td>Berglunds snabbkop</td>
<td>Sweden</td>
</tr>
<tr>
<td>North/South</td>
<td>UK</td>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Germany</td>
</tr>
<tr>
<td>Koniglich Essen</td>
<td>Germany</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Italy</td>
</tr>
<tr>
<td>Paris specialites</td>
<td>France</td>
</tr>
<tr>
<td>Island Trading</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Canada</td>
</tr>
</table>
I am showing rows from a MySQL database into a HTML table
See my code below:
<table border='1' width='100%'>
<tr>
for ($i = 1; $i <= mysql_num_rows($result667); $i++)
{
$row = mysql_fetch_array($result667);
$InterviewTime=$row['interview_time'];
echo"<td><input type='radio' name='Time' value='$time' required>$time</td> ";
}
if ($i % 4 == 0) {
echo '</tr><tr>'; // it's time no move to next row
}
?>
</table>
The table is currently showing in one long row. How can I end the current row and start a new row <tr> after 4 columns?
<table border='1' width='100%'>
<tr>
<?php
for ($i = 1; $i <= mysql_num_rows($result667); $i++)
{
$row = mysql_fetch_array($result667);
$InterviewTime=$row['interview_time'];
echo"<td><input type='radio' name='Time' value='$time' required>$time</td>";
if ($i % 4 == 0) {
echo '</tr><tr>'; // it's time no move to next row
}
}
?>
</tr>
</table>
As i can see your if statement which checks for if $i % 4 ==0 is out of the for loop. check this code works for you. and i have added a </tr> to close your opened tr tag at the bottom
Try this Code might help you , and try to write HTML code
<table border='1' width='100%'>
<tr>
<?php
for ($i = 1; $i <= mysql_num_rows($result667); $i++)
{
$row = mysql_fetch_array($result667);
$InterviewTime=$row['interview_time'];
echo"?>
<td><input type='radio' name='Time' value='$time' required><?php $time; ?></td> <?php"
}
if ($i % 4 == 0) {
echo '?></tr><tr> <?php'; // it's time no move to next row
}
?>
</tr>
</table>
Just be simple
<table>
<?php for($i=0;$i<$total;$i++){ ?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php } ?>
</table>
Above code will add $total rows. Just write between <td></td> tags
Now I wish to construct a list of tables under nested while loop and if-else condition with PHP, the format of the code is as below:
while(){
if (){
..... // extract the data
while(){
construct a table using the data extracted above
}
}
elseif (){
..... // extract the data
while(){
construct a table using the data extracted above
}
}
}
Specifically, in the inner while loop the code is:
while ( $chat = mysqli_fetch_assoc($chatQ))
{
echo
"<table class='table table-hover' >"
."<td>"
.$conver['sender_name']."\t".$chat['sender']."\t".$chat['send_time']."\t".$chat['content']
."</td>"
."<td>"
."<form id='join' action='group_chat.php' method ='POST' accept-charset='UTF-8'>"
// post the group id
."<input type='hidden' name='group_id' id='group_id' value=".$conver['sender_id']."/>"
."<button class='btn btn-default' type='submit'>Enter</button>"
."</form>"
."</td>"
."</table>";
}
But the result is very ugly:
The problem is that the Enter button is associated with each message. But what I want is that after displaying all the messages, there is a Enter button which can direct to the specific group. But I don't know how to separate the code. Could you please do me a favor? Thanks in advance!
You can do something like this:
<?php
$counter = 0;
echo '
<form>
<table>';
while ( $chat = mysqli_fetch_assoc($chatQ)){
$counter++;
echo '
<tr>
<td>
group_id_'.$counter.'
</td>
<td>
<input name="group_id_'.$counter.'" value="'.$conver['sender_id'].'">
</td>
</tr>';
}
echo '
<tr>
<td colspan="2">
<button type="submit">Enter</button>
</td>
</tr>
</table>
</form>';
?>
Also you dont have to limit yourself to echo everything in order by setting variables. See example below:
<?php
$counter = 0;
$data = NULL;
while ( $chat = mysqli_fetch_assoc($chatQ)){
$counter++;
$data .='
<tr>
<td>
group_id_'.$counter.'
</td>
<td>
<input name="group_id_'.$counter.'" value="'.$conver['sender_id'].'">
</td>
</tr>';
}
echo '
<form>
<table>'.$data.'
<tr>
<td colspan="2">
<button type="submit">Enter</button>
</td>
</tr>
</table>
</form>';
?>
I need to display specific value of key 'pasdiz_alus' from array $form->data into a table cell. And I need to display this table row only if value of key 'pasdiz_alus' is greater than '0'.
The code for this is below, but the problem is that output displays also the value of key 'pasdiz_alus' above my table row and there it is displayed as many times as number of keys of array.
How can I get rid of this display of value of " 'pasdiz_alus' x times of number of keys in array (in my case 29 times - there are 29 keys in the array)"? In this case it is: 5454545454545454......
My code is:
<table style="width: 800px;">
<tbody>
<?php
if ($form->data['pasdiz_alus'] > 0){
echo '<tr><td style="width: 100px;">Bilde šeit</td><td style="width: 500px;"> <strong>Pašdizainēts alus</strong></td>';
foreach($form->data as $key => $value) {
if($key === 'pasdiz_alus')
echo '<td style="width: 100px;">';
echo $form->data['pasdiz_alus'];
echo '</td>';
}
echo '<td style="width: 100px;">Cena šeit</td></tr>';
}
?>
</tbody>
</table>
And this is the output display, in this case the value of 'pasdiz_alus' is 54
The first row is the "wrong" one that I need to get rid off, and the second row is the "right" one.
5454545454545454545454545454545454545454545454545454545454
Bilde šeit Pašdizainēts alus 54 Cena šeit
Thanks for helping!
Brgds, Raivis
The problem in your script is here:
if($key === 'pasdiz_alus') // <-- missing opening "{"
echo '<td style="width: 100px;">'; // <-- inside the "if"
echo $form->data['pasdiz_alus']; // <-- OUTSIDE the "if"
echo '</td>'; // <-- OUTSIDE the "if"
} // <-- this matches the foreach "{"
Why do you cycle all the array keys, instead of directly accessing it?
<table style="width: 800px;">
<tbody>
<?php if ($form->data['pasdiz_alus'] > 0) { ?>
<tr>
<td style="width: 100px;">Bilde šeit</td>
<td style="width: 500px;"><strong>Pašdizainēts alus</strong></td>
<td style="width: 100px;"><?=$form->data['pasdiz_alus']?></td>
<td style="width: 100px;">Cena šeit</td>
</tr>
<?php } ?>
</tbody>
</table>
This should solve your problem, but I'd recommend also to move the if part before even opening the table:
<?php if ($form->data['pasdiz_alus'] > 0) { ?>
<table style="width: 800px;">
<tbody>
<tr>
<td style="width: 100px;">Bilde šeit</td>
<td style="width: 500px;"><strong>Pašdizainēts alus</strong></td>
<td style="width: 100px;"><?=$form->data['pasdiz_alus']?></td>
<td style="width: 100px;">Cena šeit</td>
</tr>
</tbody>
</table>
<?php } ?>
foreach($array as $k => $v)
{
if($k == 'pasdiz_alus' && $v > 0)
{
echo $v;
}
}
you need that addition in your if to make sure its only printed if it is the right key and its greater 0