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 :)
Related
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.
Hello this table pulls data from a database.
<h2>Weekly appointment list</h2>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Week Day</th>
<th>Customers</th>
<th>Selected service</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<tr>
<td>Monday</td>
<?php
$date = date('Y-m-d', strtotime("this Monday"));
$sql = "SELECT * FROM appointment WHERE weekday = '$date'";
$query = mysqli_query($db, $sql);
$numRows = mysqli_num_rows($query);
if ($numRows > 0) {
while ($row = mysqli_fetch_array($query)) { ?>
<td><?php echo $row['user_name'] ?></td>
<td><?php echo $row['service'] ?></td>
<td><?php echo $row['time'] ?></td>
<?php }
}
?>
</tr>
</tbody>
But the problem is when i have two users from echo $row['user_name'] //user x, user y the table rows break and show something like this: image link. See the the table row is broken. I want to show this way:expected table structure. All customers are shown on the particular day row in customers column. How to fix my code or the way of representation. Thanks in advance.
change your sql query to group concat username,service and time.
$sql ="SELECT group_concat(user_name) as user_name,group_concat(service) as service ,group_concat(time) as time FROM appointment WHERE weekday = '$date'";
This query will return one row with all the user and service information in one row.
you want to combine something like this
With something like this:
$res = mysql_query(/**/);
$rows = array();
while($row = mysql_fetch_assoc($res)){
array_push($rows, $row);
}
Let me know if you struggle.
I want to use the script i placed underneath, but it should show me who's having birthday today. i have added a birthday in my sql table ion this format: 1985-06-03
<html>
<head>
<title>Last 10 Results</title>
</head>
<body>
<table>
<thead>
<tr>
<td>Id</td>
<td>Name</td>
</tr>
</thead>
<tbody>
<?php
$connect = mysql_connect("localhost","root", "root");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("apploymentdevs");
$results = mysql_query("SELECT * FROM demo LIMIT 10");
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['Id']?></td>
<td><?php echo $row['Name']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
DB Table structure:
ID INT11
FirstName Varchar
LastName Varchar
Department Varchar
Birthday Date (yyyy-mm-dd)
Since, you will need to exclude the year, you can use the MONTH and DAY SQL functions like so:
SELECT * FROM table WHERE DAY(birthday) = DAY(CURDATE()) AND MONTH(birthday) = MONTH(CURDATE());
In your query, format each date to be MM-DD.
SELECT *
FROM demo
WHERE DATE_FORMAT(birthday, "%c-%d") = DATE_FORMAT(NOW(), "%c-%d")
LIMIT 10
This will bring back results where the MM-DD of both NOW() and the birthday value are equal.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
<?php
$connect = mysql_connect("localhost","root", "root");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("apploymentdevs");
$today_date = date('d');
$today_month = date('m');
$results = mysql_query("SELECT * FROM `table_name` where DATE('dob_column') = $today_date && MONTH(`dob_column`) = $today_month");
if(mysql_num_rows($results) > 0){
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['Id']?></td>
<td><?php echo $row['Name']?></td>
</tr>
<?php
}
}else{
echo "No one birthday on today enter code here";
}
?>
Can you try something like this:
$results = mysql_query("SELECT * FROM demo WHERE MONTH(`table_column`) = '".date('m')."' AND YEAR(`table_column`) = '".date('Y')."'");
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"
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