How to get the data from database on current date automatic - php

i want to get the data from database on current date. when i open page then i see get the data of today current date. if i select the another date then i get the another date data from database here is live example of my concept http://joomusic.info/joosilver.php if you visit here then you understand my concept
<link rel="stylesheet" type="text/css" href="tcal.css" />
<script type="text/javascript" src="tcal.js"></script>
<script src="jquery.js"></script>
<script>
$(document).ready(function()
{
//ajaxTime.php is called every second to get time from server
var refreshId = setInterval(function()
{
$('#timeval').load('ajaxTime.php?randval='+ Math.random());
}, 1000);
//stop the clock when this button is clicked
$("#stop").click(function()
{
clearInterval(refreshId);
});
});
</script>
<form action="ko.php" method="get">
From : <input type="text" name="d1" class="tcal" value="<?php echo date("m/d/Y"); ?>" />
<input type="submit" value="SHOW">
</form>
<table id="resultTable" data-responsive="table" style="text-align: center; width: 400px;" border="1" cellspacing="0" cellpadding="4">
<thead>
<tr>
<tr bgcolor="#F1EDC2">
<td><font color='#2F4F4F'><h2> Draw Time</h2></font></td>
<td><font color='#2F4F4F'><h2> Wining Number</h2></font></td>
</tr>
</thead>
<tbody>
<?php
include('connect.php');
if (isset($_GET["d1"])) { $d1 = $_GET["d1"]; } else { $d1=('Y-m-d H:i:s'); };
$result = $db->prepare("SELECT * FROM birthday WHERE date = :a");
$result->bindParam(':a', $d1);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr class="record">
<tr bgcolor="#EEF3E2">
<td><font size=5><font color='#008B00'><?php echo $row['dt']; ?></font></td>
<td><font size=5><font color='#008B00'><?php echo $row['wn']; ?></font></td>
</tr>
<?php
}
?>
</tbody>
</table>
if i change this line $result = $db->prepare("SELECT * FROM birthday WHERE date = :a"); into $result = $db->prepare("SELECT * FROM birthday WHERE date <= :a"); then i get all dates data ... but i want only current date data automatic here is live example of my concept http://joomusic.info/joosilver.php i want like this 100%

You didn't mention the data type of your date column.
But date, timestamp, and datetime items are just a little tricky to use in WHERE statements. If you want to find all the rows on a particular date the best way to do it is with:
WHERE whatever
AND `date` >= DATE(:a)
AND `date` < DATE(:a) + INTERVAL 1 DAY
This gets all the date values starting at midnight on the day I mentioned, up until but not including midnight on the day after the day I mentioned.
You could also do this (or some equivalent)
WHERE whatever
AND DATE(`date`) = DATE(:a) /* slow! */
but it will prevent the use of an index to satisfy your query, making it slow.
Here's an essay I wrote offering some background.
http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/

three points to consider:
1. if you just want the current date result then just use "=".
2.here we need to see what is the datatype of you date column in the birthday table.
if the datatype is datetime then add hh:mm:ss to the date, or if the datatype is just date then just use date.
3. if the date column is int and storing them in timestamp then convert date to microtime and use it.
SELECT * FROM yourTable WHERE CURDATE() >= STR_TO_DATE(date, '%m-%d-%Y') ORDER BY STR_TO_DATE(date, '%m-%d-%Y') DESC LIMIT 1

Related

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

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.

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 :)

How to split a ranking (table) after 20 entries and see the next at the next site?

I want to stop the row after 20 entries and return at the same point at the next side. To make it short I want only 20 entries per site.
Here is my code:
<html>
<head>
<title>server ranking</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="layout.css">
</head>
<body>
<?PHP
$verbindung = mysql_connect("localhost", "root", "") or die("Fehler bei der datenbank");
mysql_select_db("users") or die ("datenbank existiert nicht")
?>
<?PHP
$abfrage = "SELECT * FROM `ranking` ORDER BY `points` DESC ";
$ergebnis = mysql_query($abfrage);
$zaehler = 0;
?>
<table rules="all" style="90%;">
<caption>ViceVice Server Ranking</caption>
<tr style="text-align:center; vertical-align:middle;">
<td>Rang</td>
<td>Benutzername</td>
<td>Punkte</td>
</tr>
<?PHP
while($row = mysql_fetch_object($ergebnis))
{
$zaehler = $zaehler + 1;
?>
<tr>
<td style="text-align:center; vertical-align:middle; width: 50;">
<?PHP
echo $zaehler;
?>
</td>
<td style="text-align:center; vertical-align:middle; width: 50;">
<img src="https://minotar.net/helm/<?php echo $row->user; ?>/25.png">
</td>
<td style="text-align:left; vertical-align:middle; width: 200;">
<?PHP
echo $row->user;
?>
</td>
<td style="text-align:left; vertical-align:middle; width: 100;">
<?PHP
echo $row->points;
?>
</td>
</tr>
<?PHP
}
?>
</table>
</body>
</html>
I would thank you for any help ;D
This is not meant as a definitive answer, because there are many ways to accomplish what the OP is asking. It is, however, too long to post as a comment. This method uses simple URL parameters to specify the pagination (start shows the record to start at, show specifies how many records to show), and MySQL's LIMIT clause to limit the database output. Obviously you must check that the values in the $_GET parameters are reasonable. If the user changes the URL to read page.php?start=hello&show=world your query will fail... I have left out the data sanitation for brevity.
page.php:
<?php
$sql = "SELECT id, description FROM mytable ORDER BY id";
// if there is the "start" url parameter, use it. Else, start from zero.
$start = isset($_GET['start']) ? $_GET['start'] : 0;
// if there is the "show" url parameter, user it. Else, use default 20 records.
$show = isset($_GET['show']) ? $_GET['show'] : 20;
$sql .= " LIMIT $start,$show";
// doDatabaseQuery is a fictitious function which we assume
// returns an array of objects. Comment this line if using test data, below
$records = doDatabaseQuery($sql);
// if you want to test this with some fake data, uncomment this code section
// which creates 200 fake records, and puts them into an
// array then array_slice acts like the "LIMIT" clause in the mysql query
/*
for($x=1;$x<=200;$x++) {
$o = new stdClass();
$o->id = $x;
$o->description = "Description for object $x";
$records[] = $o;
}
$records = array_slice($records, $start, $show);
*/
// set the "start" parameter value in the "Back" link
// if it's less than zero, we set it to zero.
$previous_start = $start - $show;
if($previous_start < 0) $previous_start = 0;
// set the "start" parameter value in the "Next" link
// if we wanted to do something more advanced, we would check
// this value is not greater than the number of records in the table
$next_start = $start + $show;
include "page.html";
?>
page.html:
<html>
<head>
<title>Test Page</title>
</head>
<body>
<h1>Test Pagination Page</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php for($i=0; $i < count($records); $i++): ?>
<tr>
<td><?php echo $records[$i]->id; ?></td>
<td><?php echo $records[$i]->description; ?></td>
</tr>
<?php endfor; ?>
</tbody>
<tfoot>
<tr>
<td>Back</td>
<td>Next</td>
</tr>
</tfoot>
</table>
</body>
</html>
Advantages: Allows a very basic pagination, but can be easily adapted to provide "Last Page" and "First Page" functionality.
Disadvantages: Allows the user to manipulate the URL by hand; imagine a table with 2000000 records - the user could set the show parameter to 2000000 and have your server do lots of work to pull out all the records from the table... To get around this, you can limit the $show variable in the code to a maximum value, or remove it from the URL completely and keep it at a fixed value.

How set time in php to get data using date

here is the script code of my page.
i select any date and i get the data by selecting date from the database.
i want to initially set the date to today when i open my page.
then i get the data of today and of current time.
but how ???
i give you an example: this is example link.
here you can see timestamp box to show results of current date and time
i want to do it exactly like this.
but how?
<link rel="stylesheet" type="text/css" href="tcal.css" />
<script type="text/javascript" src="tcal.js"></script>
<form action="index.php" method="get">
From : <input type="text" name="d1" class="tcal" value="" />
<input type="submit" value="Search">
</form>
<table id="resultTable" data-responsive="table" style="text-align: left; width: 400px;" border="1" cellspacing="0" cellpadding="4">
<thead>
<tr>
<th> Birtday </th>
<th> Name </th>
<th> Gender </th>
</tr>
</thead>
<tbody>
<?php
include('connect.php');
if (isset($_GET["d1"])) { $d1 = $_GET["d1"]; } else { $d1="0000-00-00"; };
$result = $db->prepare("SELECT * FROM birthday WHERE date = :a");
$result->bindParam(':a', $d1);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr class="record">
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['gender']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
replace
if (isset($_GET["d1"])) { $d1 = $_GET["d1"]; } else { $d1="0000-00-00"; };
with
if (isset($_GET["d1"])) { $d1 = $_GET["d1"]; } else { $d1=date('"Y-m-d"); };
or even shorter
$d1 = isset($_GET["d1"]) ? $_GET["d1"] : date('"Y-m-d");
to set the current date as the initial date.
Your other question is "How do i get a calendar to select a date" right? If so, just google "How do i get a calendar to select a date with php". Or search for PHP Calendar select.
This is not a free "write my code for me" website by the way. Sorry.
To get current time and date, you can use the date function. For example:
$d1 = date('Y-m-d H:i:s');
It will give you something like:
"2014-03-14 08:52:30"

Displaying MySQL Data in PHP in a hidden div

I have this HTML code for my div:
<div id="ticketupdates" style="display:none;">
</div>
then within the div i have this PHP Code:
<?php
$sql2="
SELECT
ticket_seq,
CONCAT(CONCAT(notes),'<br><br>',filename,'') as displaydata,
datetime as timestamp,
updatedby,
CONCAT('<strong>Time Start: </strong>',timestart,' - <strong>Time End: </strong>',timeend) as timestartend
from ticket_updates where ticket_seq = '".$result["ticketnumber"]."'
UNION
SELECT
ticket_seq,
CONCAT('<strong>',ticketchange,'</strong><br>' ,description) as displaydata,
datetime as timestamp,
changed_by as updatedby,
blankfield
from ticket_changes where ticket_seq = '".$result["ticketnumber"]."'
ORDER by timestamp ASC ";
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
while($result2=mysql_fetch_array($rs2))
{
$timestartend2 = ($result2["timestartend"] > '') ? '<br><br>'.$result2["timestartend"] : '';
echo '<tr>
<td colspan="2" bgcolor="#666666" align="left"><font color="#FFFFFF"><strong>'.$result2["updatedby"].'</strong></font></td>
<td bgcolor="#666666" align="right"><font color="#FFFFFF"><strong>'.$result2["timestamp"].'</strong></font></td>
</tr>
<tr>
<td colspan="3">'.nl2br($result2["displaydata"]).''.$timestartend2.'</td>
</tr>
<tr>
<td colspan="3"></td>
</tr>';
}
?>
obviously the div is hidden as its got display:none on the style of it but it's still displaying the PHP results.
If i just put some plain text inside the div like this:
<div id="ticketupdates" style="display:none;">
hello
</div>
it doesn't display. I am doing this as I have a link that expands the div to show the content.
Any ideas why it's not working with the PHP Code?
Did you open and close that table that your putting rows and columns too? If you did not open it, you might get some unexpected results.
The name of the ID '#ticketupdates' sounds like this could be loaded by an AJAX request?
Try hiding it after the request with Javascript, or with jQuery;
$('#ticketupdates').hide();
And for the open doors; please try to not use mysql_* functions, and try to seperate the PHP and styling from the HTML (especially from SQL).

Categories