How to set countdown timer for a column of a table? - php

I want to set a timer for one column of a table, the column contain time and I want to set a timer for this column. Each cell of this column has a specific time, if the time is 1 hour before 20 minutes of that hour it should display a popup message which the time is going to finish.
Below is my table:
column name: Time Difference
<body>
<div class="card-body">
<table class="table">
<thead>
<tr>
<th>IdCard</th>
<th>Name</th>
<th>Company</th>
<th>Floors</th>
<th>Arrival</th>
<th>Departure</th>
<th>Time Difference</th>
</tr>
</thead>
<?php
include('includes/dbconnection.php');
$sql="SELECT *,
TIME_FORMAT(arrival, '%h:%i %p') AS arrival,
TIME_FORMAT(departure, '%h:%i %p') AS departure,
TIME_FORMAT(timediff(departure, arrival), '%H:%i') AS difftime
FROM master where Status = 'Pending'";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0) {
foreach($results as $row) {
?>
<tr>
<!-- <td><?php echo htmlentities($cnt);?></td>-->
<td><?php echo htmlentities($row->idcard);?></td>
<td><?php echo htmlentities($row->name);?></td>
<td><?php echo htmlentities($row->company);?></td>
<td><?php echo htmlentities($row->floors);?></td>
<td><?php echo htmlentities($row->arrival);?></td>
<td><?php echo htmlentities($row->departure);?></td>
<td><span style="color: red;"><?php echo htmlentities($row->difftime);?></span></td>
</tr>
<?php
$cnt=$cnt+1;
}
}
?>
</table>
</div>
</body>
Table Image

Eliana,
See these answers how to refresh PHP page by browser:
Refresh a page using PHP
PHP-script refresh it self and restart execution-time
After the page refresh your current code will then display the new time difference in the last column 'Time Difference'.
If you still want to show pop up then you need to use some Javascript to display the pop after page is loaded, e.g.:
<body onload="showArrivingSoon()">
<!-- Your code ... -->
<script>
<?php
// Iterate through rows to produce string $arrivingSoonAsStringDelimitedByNewLine ...
?>
// Here the prepared string is printed to the page and initialize JS variable
var arrivingSoon = '<?php echo $arrivingSoonAsStringDelimitedByNewLine ?>';
function showArrivingSoon(arrivingSoon)
{
alert('Arriving soon:\n' + arrivingSoon);
}
</script>
</body>
The iteration, calculations and checks could be done in Javascript as well. To do that it's easier first to pass data rows as JSON from PHP to Javascript variable, then iterate.

Related

How to make a button inside a table execute a mysql query

I have a simple html table that displays data from a mysql table containing a list of available tournaments.
<table>
<tr>
<th>Tournament Name</th>
<th>Platform</th>
<th>Date</th>
<th>Venue</th>
<th>Judge</th>
<th>Description</th>
<th>Limit</th>
<th>Register</th>
</tr>
<?php while($row = mysqli_fetch_array($result)):?>
<tr>
<td><?php echo $row['nTournament'];?></td>
<td><?php echo $row['pTournament'];?></td>
<td><?php echo $row['dTournament'];?></td>
<td><?php echo $row['vTournament'];?></td>
<td><?php echo $row['jTournament'];?></td>
<td><?php echo $row['dTournament'];?></td>
<td><?php echo $row['lTournament']; ?></td>
<td><form method="post" action="tournamentlist.php"><button type="submit" name="btn_register"></button></form></td>
</tr>
<?php endwhile; ?>
</table>
What I want to do is that once the current user clicks that button, the button executes a query which takes the current user's id and the tournament id of the selected row to add them to another mysql table. Something like a tournament inscription.
The query/function I want to run is:
if (isset($_POST['btn_register'])) {
singup();
}
function singup() {
global $db;
$idUser = $_POST['idUser'];
$idTournament= $_POST['idTournament'];
$query = "INSERT INTO registeredCompetitor(idUser, idTournament) VALUES ('$idUser', '$idTournament')";
mysqli_query($db, $query);
header('location: tournamentlist.php');
}
The query works just fine on it's own but I don't know if the function itself works because of the button.
Is it possible to do such thing without the use of a form? If not, What other ways are there to do something like this?
EDIT1: Button now is ready but nor the function or the query executes once it's clicked.
You can create a form on each row that has 2 hidden fields in it. Not sure what your DB fields are called though so you'll have to change user_id and tournament_id to something appropriate.
...
<td>
<form action="post" action="the_name_of_your_php_script.php">
<input type="hidden" name="idUser" value="<?php echo $row['user_id']; ?>">
<input type="hidden" name="idTournament" value="<?php echo $row['tournament_id']; ?>">
<button type="submit" name="btn_register"></button>
</form>
</td>
...
Can't wrap it because <form> is not allowed as a child of <td>
All the documentation I see online permits forms inside of <td>. The other alternative is to use a link instead of a form.
You can use javascript to execute this functionality.
<table>
<tr>
<th>Tournament Name</th>
<th>Platform</th>
<th>Date</th>
<th>Venue</th>
<th>Judge</th>
<th>Description</th>
<th>Limit</th>
<th>Register</th>
</tr>
<?php while($row = mysqli_fetch_array($result)):?>
<tr>
<td><?php echo $row['nTournament'];?></td>
<td><?php echo $row['pTournament'];?></td>
<td><?php echo $row['dTournament'];?></td>
<td><?php echo $row['vTournament'];?></td>
<td><?php echo $row['jTournament'];?></td>
<td><?php echo $row['dTournament'];?></td>
<td><?php echo $row['lTournament']; ?></td>
<td><button type="submit" onclick="sendRequest('<?php echo $idUser ?>','<?php echo $idTournament ?>')" name="btn_register"></button></td>
</tr>
<?php endwhile; ?>
</table>
Javascript code
function sendRequest(idUser,idTournament ){
// i am using jquery
$.post(other_page_url,{idUser:idUser,idTournament :idTournament},function(data)
{
window.location = 'tournamentlist.php';
} )}
I hope it's gonna help you
You don't need to wrap this in a <form> inside the table, you can do it all via Javascript / Jquery if you wish. On the html side, add the data you need to the button in data sets:
<td><button type="submit" name="btn_register" id="btn_register" data-user-id="<?php echo $userId ?>" data-tournament-id="<?php echo $row['idTournament']; ?>"></button></td>
Then in your script, you can call the data and send to post via ajax and either load the new page after INSERT, or load as a modal, or whatever you like. You can do something like below if loading a modal (change the load part to refresh the page if you want to go directly):
$('#btn_register').on('click', function () {
var t_id = $(this).data('tournament-id');
var u_id = $(this).data('user-id');
$.ajax({
url: "YourURL",
type: "POST",
data:{
t_id:t_id,
u_id:u_id
},
success: function (h) {
$(".modal-title").text("Table with new stuff");
$(".modal-body").html(h);
$('#modal').modal();
// lots of options here - you could even reload above if desired
}
})
});

Ajax Don't Show DB Table When Radio Button Select

I'm still new in web programming. I got problem when I use ajax and jQuery for get mysql data when I choose radio button then submit that value. For the query I'm using PDO. Here's my code :
1. Choose the value from radio button
<form method="get" name="urut">
<input type="radio" name="urut" value="ASC" id="urut"/>ASC
<input type="radio" name="urut" value="DESC" id="urut"/>DESC
<input type="submit" value="Filter" id="masukan"/>
</form>
jQuery code to get ASC or DESC value from the radio button when click the input submit
<script type="text/javascript">
$(document).ready(function(){
$('#masukan').click(function(){
$('#table_div').text("");
$('#info').text("");
$.ajax({
url : "php/by_name.php",
type : "get",
data : {"urut": $("#urut").val()},
success : function(data){
$('#info').html('<b>Daftar Siswa Berdasarkan Nama</b>');
$('#table_div').html(data);
},
error : function(xhr, teksStatus, kesalahan){
$('#info').html('<b>Terjadi Kesalahan</b>');
}
});
});
});
</script>
This page give data that choose by user when submit the value is DESC or ASC in html table
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="get">
<table>
<?php
# code...
if(isset($_GET['urut'])){
$urut = trim($_GET['urut']);
?>
<thead>
<th>N I S</th>
<th>N I S N</th>
<th>NAMA SISWA</th>
<th>TEMPAT LAHIR</th>
<th>TANGGAL LAHIR</th>
<th>AKSI</th>
</thead>
<tbody>
<?php
// echo $urut;
include "db_connect.php";
if($urut == "ASC"){
try{
$kueri = $dt_bas->prepare("SELECT nis, nisn, nm_dpn, tmp_lhr, dob_siswa FROM siswa ORDER BY nm_dpn ASC");
// $kueri->bindParam(":pilih", $urut);
$kueri->execute();
}catch(PDOException $e){
}
while ($row = $kueri->fetch(PDO::FETCH_ASSOC)) {
?>
<!--- Tabel Row Start ASC------------------>
<tr>
<td><?php echo $row["nis"];?></td>
<td><?php echo $row["nisn"];?></td>
<td><?php echo $row["nm_dpn"];?></td>
<td><?php echo $row["tmp_lhr"];?></td>
<td><?php echo $row["dob_siswa"];?></td>
<td>
<a href="#">Ubah<a/>
Detail
</td>
</tr>
<?php
} // end of while where $urut == ASC
}elseif($urut == "DESC"){
try{
$kueri = $dt_bas->prepare("SELECT nis, nisn, nm_dpn, tmp_lhr, dob_siswa FROM siswa ORDER BY nm_dpn DESC");
// $kueri->bindParam(":pilih", $urut);
$kueri->execute();
}catch(PDOException $e){
}
while ($row = $kueri->fetch(PDO::FETCH_ASSOC)) {
?>
<!--- Tabel Row Start ASC------------------>
<tr>
<td><?php echo $row["nis"];?></td>
<td><?php echo $row["nisn"];?></td>
<td><?php echo $row["nm_dpn"];?></td>
<td><?php echo $row["tmp_lhr"];?></td>
<td><?php echo $row["dob_siswa"];?></td>
<td>
<a href="#">Ubah<a/>
Detail
</td>
</tr>
<?php
} // end of while where $urut == DESC
} // end of child if
} // end of parent if
?>
</tbody>
</table>
</form>
</body>
</html>
When, I refresh the page on my web browser, I get error message but It only work once for both value I input and it's just temporary, but when I try it for 2 or more times I get nothing even the error message.
Here my console error
enter image description here
I get something weird also in this error, when I submit DESC in my web browser url I can see DESC, but in my console error it show me ASC even I try to submit DESC for many times. Here the picture
enter image description here
Thank's for your time, everyone.
You are using same id for both radio input which is why you are not getting correct value. Change one of radio button ID and use jQuery to get selected input button value and then use.
Use separate id s for radio buttons .That will do your need..

selected row of a table and it's value with PHP

I have inserted all the payments done from my database into a table and all their information they I was meant to show to the visitor. However, I'd like to add this feature that by the time that the visitor select each row they can visit all the products they've bought on that day. In other words I want a modal to pops out and show the invoice for that purchase (Meaning that all the products that were bought at the same day). However how do I get which date has been selected?
<?php
$myQuery = mysql_query("SELECT * FROM `payment` WHERE UserID = $uid;");
?>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Brand</th>
<th>Price</th>
<th>Quantity</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_assoc($myQuery)){
?>
<tr data-toggle="modal" data-id="1" data-target="#orderModal">
<td><?php echo $row["ProductID"] ?></td>
<td><?php echo $row["ProductName"] ?></td>
<td><?php echo $row["Brand"] ?></td>
<td><?php echo $row["Price"] ?></td>
<td><?php echo $row["Quantity"] ?></td>
<td><?php echo $row["Date"] ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
There are several ways to go about it. First, you can use PHP to select the date:
$date = date('Y-m-d');
$myQuery = mysql_query("SELECT * FROM `payment` WHERE UserID = $uid AND Date = '$date';");
Or you can use MySQL's CURDATE() method:
$myQuery = mysql_query("SELECT * FROM `payment` WHERE UserID = $uid AND Date = CURDATE();");
This will work if your date format is in Y-m-d (2016-06-03) format. If it's in another format, you're going to have to do a bit of DATE_FORMAT manipulation. This will only work for "today". Otherwise, go with the first method and pass in the desired date.
As an aside, mysql_* functions are deprecated, and removed as of PHP7. I'd really recommend using PDO or mysqli, and using it to bind your parameters so you don't have to worry about mysql injection.
Use Jquery DATEPICKER to choose date and submit form on your target file where query is written
Get the date on that page like this:
$date = date('Y-m-d');
if(iseet($_POST['date']))
{
$date = date("Y-m-d", strtotime($_POST['date']));
}
$myQuery = mysql_query("SELECT * FROM `payment` WHERE UserID = $uid AND Date = '$date' ");
Here is the code for datepicker
$(function() {
$( "#datepicker" ).datepicker();
});
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<form action="yourfile.php" method ="post">
<p>Date: <input type="text" name="date" id="datepicker"></p>
<input type="submit" value="Submit">
</form>
Use a date selector for selecting the date and after that make a ajax call to you php function with that selected date and then fire a query like SELECT * FROMpaymentWHERE UserID = $uid and Date = $date; and the popup a model with returned data from ajax call.
mysql_query("SELECT * FROM `payment` WHERE UserID = $uid AND Date between '$date1' and '$date2'");
$date1 is the date of purchase with hour, minutes and seconds set to 0s
$date2 is date of purchase with hours set to 23, minutes to 59 and seconds to 59
if $date is 2015-05-05 16:34:22, $date1 is 2015-05-05 00:00:00 and $date2 is 2015-05-05 23:59:59 , same day :)

PHP Script - Activity Description not reflecting record values

I had a need for a system that could track billable tasks for tickets that we processed. I have a script that is almost complete, but I cannot seem to figure out why the tickets_tasks_activity field is not pulling the right enum_short_description when displaying. When I add a task the values are added correctly, its only not displaying them correctly. It seems that I can add one activity... and then every additional activity has the same description even though the values are different in the database. Could someone please help me out.
Here is the script to pull the information from the database.
* Get the tasks associated with a ticket.
*
* #company_id The ID of the company that is being queried.
* #ticket_id The ID of the ticket that is being queried.
*/
function get_ticket_tasks($company_id, $ticket_id)
{
$sql = "SELECT u.user_first_name,
u.user_last_name,
u.user_id,
tn.tickets_tasks_posted_date,
tn.tickets_tasks_task,
tn.tickets_tasks_activity,
(SELECT enum_short_description FROM tbl_enum WHERE enum_id = tn.tickets_tasks_activity) tickets_tasks_activity_description
FROM tbl_tickets_tasks tn,
tbl_user u
WHERE tn.tickets_tasks_company_id = {$company_id}
AND tn.tickets_tasks_ticket_id = {$ticket_id}
AND tn.tickets_tasks_posted_user_id = u.user_id;";
$query = $this->db->query($sql);
// If nothing was returned invalid query.
if($query->num_rows() == 0)
return false;
return $query->result_array();
}
Here is the code for the View Page
<div class="tab-pane" id="tasks">
<div class="row">
<div class="col-lg-12">
<?php
if($tasks)
{ ?>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Service Provided</th>
<th>Value</th>
<th>Who?</th>
<th>When?</th>
</tr>
</thead>
<tbody>
<?php
for($i = 0; $i < count($tasks); ++$i)
{ ?>
<tr>
<td><?php echo $tasks[0]['tickets_tasks_activity_description']; ?></td>
<td><?php echo nl2br($tasks[$i]['tickets_tasks_task']); ?></td>
<td><?php echo $tasks[$i]['user_first_name'].' '.$tasks[$i]['user_last_name']; ?></td>
<td><?php echo date("m/d/Y", strtotime($tasks[$i]['tickets_tasks_posted_date'])); ?></td>
</tr>
<?php
} ?>
</tbody>
</table>
<?php
}
else
{ ?>
<p>This ticket does not have any tasks.</p>
<?php
} ?>
</div>
</div>
</div>
Here you have (at the beginning of the loop):
<td><?php echo $tasks[0]['tickets_tasks_activity_description']; ?></td>
you're printing always $task[0], you should print $task[$i]
PS: You should probably state your JOIN in the FROM section of your SQL clause (instead to put it on the WERE section), at first I though you had none :-)

jquery how to get value in a table

I have the following code that generates a table:
<table class="table table-bordered table-striped" id="assignedvs">
<thead>
<tr>
<th>VlId</th>
<th>Name</th>
<th>Status</th>
<th>Voice</th>
<th>Jumbo</th>
<th>Mode</th>
</tr>
</thead>
<tbody>
<?php foreach ($vln as $vlndetail): ?>
<tr>
<td id='vlid'><?php echo $vlndetail['VlId'] ?></td>
<td><?php echo $vlndetail['Name'] ?></td>
<td><?php echo $vlndetail['Status'] ?></td>
<td><?php echo $vlndetail['Voice'] ?></td>
<td><?php echo $vlndetail['Jumbo'] ?></td>
<td><?php echo $vlandetail['Mode'] ?></td>
</tr>
<?php endforeach ?>
I need to find the row where the VlId matches what the user has specified in a text box. Once I've found this record, I want to grab value in the mode column for the particular row.
here's what i've written so far:
$('#delete').live('click', function() {
//get a count of all records. only allowed to delete if you have more than one + the header.
var reccount = $('#assignedvs tr').length;
if (reccount > 2)
{
//loop through the table
$('#assignedvs tr').each(function() {
var temp = $(this).find(".vlid").html();
console.log(temp);
if (temp == $('#uservalue').val){
//grab mode column
}
});
}
else
{
alert("error: must have at least 1 record.");
}
});
problem - the code i have to reference the vlid column is incorrect. it always prints a null to the console.
Can you tell me what I've done wrong?
thanks.
EDIT 1
I changed my id to a class and changed my jquery back to the original code I had. it's working - except for the fact that I think it's including the header . I have 4 rows + header. When i check the console results, the first print out is always NULL and then the correct value for the 4 records. how do it get it to ignore the header?
That's because you are finding by className, not by id. To find by id, use the following instead:
$(this).find("#vlid").html();
However, since ids should be unique across the entire document, a better solution would be to maintain your current code, and instead of using <td id='vlid'>, use <td class='vlid'>.
Also note that val() is a function. Thus, to get the value of a given input, you should use $('#uservalue').val().
EDIT: To exclude the header, use the $('#assignedvs tbody tr') selector. This way, you only get rows that are descendants of tbody, thus ignoring the header rows, which descend from thead.
couple of changes:
<?php echo $vlndetail['VlId']; //missing semi-colon ?>
var temp = $(this).find("#vlid").html(); vlid is an id
You can easily do this via datatables at http://datatables.net
var temp = $(this).find("#vlid").html(); // .vlid (by class) changed to #vlid (by id)
An even easier solution.
Assign an ID to each row with the vlId (possibly prepend tr_ to the ID to avoid duplication).
Assign a class with the column name to each datacell.
Like so:
<?php foreach ($vln as $vlndetail): ?>
<tr id='tr_<?php echo $vlndetail['VlId'] // set the ID ?>'>
<td class='vlid'><?php echo $vlndetail['VlId'] ?></td>
<td class='Name'><?php echo $vlndetail['Name'] ?></td>
<td class='Status'><?php echo $vlndetail['Status'] ?></td>
<td class='Voice'><?php echo $vlndetail['Voice'] ?></td>
<td class='Jumbo'><?php echo $vlndetail['Jumbo'] ?></td>
<td class='Mode'><?php echo $vlandetail['Mode'] ?></td>
</tr>
<?php endforeach ?>
Then to get the Name of the selected vlID just do this JQUERY:
var rowID = "#tr_" + $('#uservalue').val(); // this is optional. I prefer to cache values for increased code readability.
$(rowID + " td.Mode").dostuffhere(); // this selector returns the Mode cell for the row indicated by the user
This will grab the Mode column of that specific row.

Categories