I'm currently trying to refresh my table every 5 seconds, but I'm not being successful.
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Número</th>
<th>Nome do colaborador / secção</th>
</tr>
</thead>
<tbody>
<?php
$req = mysqli_query($con, 'select user_id, user_name from users');
while ($dnn = mysqli_fetch_array($req)) {
echo "<tr>";
echo " <td>" . $dnn['user_id'] . "</td>";
echo " <td>" . $dnn['user_name'] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
What I want is to refresh the page every 5 seconds and of course if someone edits he will be able to see the changes that were made.
What can I do to complete this action?
You need to use $.ajax like
$(function(){
function getdata(){
$.ajax({
url:'getdata.php',
success:function(response){
$('#dataTables-example tbody').html(response);
}
});
}
setInterval(function(){getdata()},5000);
});
getdata.php
<?php
$req = mysqli_query($con, 'select user_id, user_name from users');
while ($dnn = mysqli_fetch_array($req)) {
echo "<tr>
<td>" . $dnn['user_id'] . "</td>
<td>" . $dnn['user_name'] . "</td>
</tr>";
}
?>
Related
I want to generate Bootstrap 5 table .table-striped using data from MySQL, table: [users]
The table in plain HTML looks fine, but as soon as I generate it using PHP all styling disappears
PHP Code:
<?php
$result = mysqli_query($link,"SELECT * FROM users ORDER BY id ASC");
echo "<table class='table table-striped'>
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Rank</th>
<th>Created at</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['rank'] . "</td>";
echo "<td>" . $row['created_at'] . "</td>";
echo "</tr>";
echo " </tbody>";
}
echo "</table>";
mysqli_close($link);
?>
and I have <div class='table-responsive'> before <?php and </div> after ?>
I would be glad if you could explain me where I am making a mistake and I would like the table to have the same styling as in pure HTML
EDIT:
rendered HTML
<div class='table-responsive'>
<table class='table table-striped'>
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Rank</th>
<th>Created at</th>
</tr>
</thead><tbody><tr><td>39</td><td></td><td>julia</td><td>2021-12-20 00:42:14</td></tr> </tbody><tbody><tr><td>40</td><td>test</td><td>user</td><td>2021-12-20 02:35:37</td></tr> </tbody><tbody><tr><td>41</td><td>testt</td><td>user</td><td>2021-12-20 17:22:43</td></tr> </tbody></table></div>
You are building a new tbody for every row. You should move the tbody creation outside the while loop. You also only need 1 echo, rather than echoing every line. I would do:
<?php
$result = mysqli_query($link,"SELECT * FROM users ORDER BY id ASC");
$output = "<div class='table-responsive'>
<table class='table table-striped'>
<thead>
<tr>
<th style='background-color:#ad8c70'>#</th>
<th style='background-color:#ad8c70'>Username</th>
<th style='background-color:#ad8c70'>Rank</th>
<th style='background-color:#ad8c70'>Created at</th>
</tr>
</thead>
<tbdoy>";
while($row = mysqli_fetch_array($result))
{
$output .= "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['username'] . "</td>
<td>" . $row['rank'] . "</td>
<td>" . $row['created_at'] . "</td>
</tr>";
}
mysqli_close($link);
$output .= '</tbody></table>';
echo $output;
?>
I have a PHP page that shows pending claims which can have their status changed when a checkbox is selected and the array updated.
<?php include("partials/header.php"); ?>
<p>Manage expenses here!</p>
<body>
<style type="text/css">
#dis{
display:none;
}
</style>
<div id="dis">
<!-- here message will be displayed -->
</div>
<p>Current Claims</p>
<div class="container">
<form method="post" action="export.php" enctype="multipart/form-data" >
<table cellspacing="0" width="100%" id="example" class="table table-striped table-hover table-responsive">
<thead>
<tr>
<th>ID</th>
<th>Employee ID</th>
<th>Ref</th>
<th>Date</th>
<th>Filed Date</th>
<th>Type</th>
<th>Client</th>
<th>Project</th>
<th>Narrative</th>
<th>Net</th>
<th>VAT</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
//connection details
require_once 'dbconfig.php';
//end connection details
$query = "SELECT * FROM claim WHERE status='pending'";
$result = mysql_query($query);
if($result === false) {
die(mysql_error());
}
while($row = mysql_fetch_array($result)){
$row_id = $row['c_id'];
$employee_id = $row['employee_id'];
$claim_ref = $row['claim_ref'];
$transaction_date = $row['transaction_date'];
$filed_date = $row['filed_date'];
$expense_type_id = $row['expense_type_id'];
$client_code_id = $row['client_code_id'];
$project_code_id = $row['project_code_id'];
$narrative = $row['narrative'];
$net = $row['net'];
$vat = $row['vat'];
$status = $row['status'];
echo
"<tr>
<td>" . $row_id . "</td>
<td>" . $employee_id . "</td>
<td>" . $claim_ref . "</td>
<td>" . $transaction_date . "</td>
<td>" . $filed_date . "</td>
<td>" . $expense_type_id . "</td>
<td>" . $client_code_id . "</td>
<td>" . $project_code_id . "</td>
<td>" . $narrative . "</td>
<td>" . $net . "</td>
<td>" . $vat . "</td>
<td>" . $status . "</td>
<td><input type='checkbox' name='c_id[]' value='" . $row_id . "'/></td>
</tr>";
}
echo "</tbody></table><input type='submit' value='accept'></form>"; //end form
/***** Update Status *****/
/*print_r($_POST);*/
if(gettype($_POST['c_id'])=="array"){
foreach($_POST['c_id'] as $val){
$id_c = $val;
$query2 = "UPDATE claim SET status = 'accepted' where c_id='".$id_c."'";
$result2 = mysql_query($query2);
if($result2 === false) {
die(mysql_error());
}
echo "Status " .$id_c. " is updated. <br>";
}
}
mysql_close();
?>
</tbody>
</table>
</div>
</div>
<br />
</script>
</body>
</html>
Q1. I was wondering whether the rows which are having the status column changed to accepted, can somehow also be exported to a csv file.
Q2. Is there a way to have two buttons, one for accepting and the other one for rejecting as each claim has three possible statuses: pending, accepted, rejected
Update: the claim table is as follows: http://sqlfiddle.com/#!9/5ce91
create table claim (
c_id INT(5) zerofill primary key auto_increment,
claim_ref VARCHAR(7),
employee_id integer NOT NULL REFERENCES employee(emp_id),
transaction_date DATE,
filed_date DATE,
expense_type_id VARCHAR(20) REFERENCES expense_type(expense_type),
client_code_id VARCHAR(7) REFERENCES client(client_code),
project_code_id VARCHAR(10) REFERENCES project(project_code),
narrative VARCHAR(255),
net decimal(6,2),
vat decimal(6,2),
status ENUM('pending', 'rejected', 'accepted'))
ENGINE = InnoDB;
Another bit is that the project ends in about a week so I'd rather not get into updating to PDO or MySQLi although if anyone has a complete solution in one of these it would be just as good, thanks.
I've got a problem with a html table in a php code.
I try this code but I have a problem with href in a tag. In fact when I click on the href it post undefined while DetailsAnnonces.php exist.
Thanks for your response.
<?php
$conn=#mysqli_connect($_SESSION['servername'],$_SESSION['username'],
$_SESSION['password'], $_SESSION['database']) or die(mysql_error());
$verifExistence1 = "SELECT * FROM `annonces`;";
$result = mysqli_query($conn, $verifExistence1);
echo "<table border = 2>";
echo "<table align='center'> <thead>
<tr>
<th>Titre</th>
<th>Description</th>
<th>Image</th>
</tr>
</thead>
<tbody>";
while($row = #mysqli_fetch_assoc($result)){
$idbis=$row['id'];
$titrebis=$row['titre'];
echo "<tr>
<td>" . $row['titre'] . "</td><td>" . $row['description'] . "</td>
<td> $titrebis
</tr>";
}
echo "</table>";
//echo $html_table;
?>
The problem was caused by a javascript left on this page.
The problem is in the $idbis
<?php
echo "
<td>
'".$titrebis."
</td>
</tr>";
?>
I have this problem in displaying the result in td inside table. I want to display all rows coming from the database.
I have code but it results only one row.
<table class="table table-striped table-bordered table-hover" id="dataTables">
<thead>
<tr>
<th>ID</th>
<th>Role</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
$con=mysqli_connect("localhost", "root", "", "trackingsys");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query="SELECT * from role" ;
$result = mysqli_query($con,$query);
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['RoleName'] . "</td>";
echo "<td>" . $row['RoleDesc'] . "</td>";
echo '</tr>';
}
mysqli_close($con); ?>
</tbody>
</table>
I edited this code and put the ending tag </td>. But still it results only one row.
i am new to php and i try to solve this many times but i couldn't.
this is my php code
Can someone help me with this it should be easy for a php expert.
<?php
require_once 'connector.php';
$result = mysql_query('SELECT * FROM highscores ORDER BY score DESC');
$username = mysql_query('SELECT username FROM users WHERE id in(SELECT user_id FROM highscores)');
echo"<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
while ($name = mysql_fetch_array($username) )
{
echo "<tr>
<td>" . $name ['username'] . "</td>";
}
while( $row = mysql_fetch_array($result))
{
echo"
<td>" . $row ['score'] . "</td>
<td>" . $row ['date'] . "</td>
</tr>";
}
echo"
</table>
</body>
</html>
";
the table i want to take
mysql_* is deprecated! use mysqli_*
<?php
require_once 'connector.php';
$SQL = "SELECT u.username, h.score, h.date
FROM users AS u
JOIN highscores AS h ON (h.user_id = u.users_id)";
$result = mysql_query($SQL) or die( mysql_error() );
echo "<html>
<head>
<title>Highscores</title>
</head>
<body>";
if( mysql_num_rows($result) > 0 )
{
echo "<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>";
while ( $row = mysql_fetch_array($result) )
{
echo "<tr>";
printf("<td>%s</td>", $row['username']);
printf("<td>%s</td>", $row['score']);
printf("<td>%s</td>", $row['date']);
echo "</tr>";
}
echo "</table>
</body>
</html>";
}
mysql_free_result($result);
Can you try this,
$result = mysql_query('SELECT hs.score, hs.date, u.username FROM highscores as hs, users as u where hs.user_id=u.id ORDER BY score DESC');
echo "<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
while( $row = mysql_fetch_array($result))
{
echo"<tr>
<td>" . $row ['username'] . "</td>
<td>" . $row ['score'] . "</td>
<td>" . $row ['date'] . "</td>
</tr>";
}
Instead of putting everything in a double quote, you can use string concatenation with (.) operator. For example, Instead of writing like this
echo"<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
You can write like this:
echo "<html>" .
"<head>" .
"<title>Highscores</title>"
"</head>" .
"<body>" .
"<table border='1'>" .
"<tr>" .
"<th>user</th>" .
"<th>score</th>" .
"<th>Date</th>" .
"</tr>" ;
The following code block
echo "<tr>
<td>" . $name ['username'] . "</td>";
Should be written as
echo "<tr>" .
"<td>" . $name ['username'] . "</td>";
In this way the code looks more readable as well.
The second loop needs to be inside the first loop, and the closing </tr> shouldn't be inside the second loop.
<?
while ($name = mysql_fetch_array($username)) {
echo "<tr>";
echo "<td>" . $name["username"] . "</td>";
while ($row = mysql_fetch_array($result)) {
echo "<td>" . $row["score"] . "</td>";
echo "<td>" . $row["date"] . "</td>";
}
echo "</tr>";
}
?>