Simple code for exceeding time [duplicate] - mysql

This question already has an answer here:
SQL Time exceeds
(1 answer)
Closed 9 years ago.
I've written a piece of code to calculate the waiting time for a patient as follows;
$query = "SELECT PatientID, Forename, Surname, Gender, Illness, Priority, Arrival_time, TIME_FORMAT(ABS(TIMEDIFF(CURTIME(), Arrival_time)),'%H hours') as Waiting_Time FROM Patient";
Which displays the following data:
PatientID Forename Surname Gender Illness Priority Waiting Time
625 Max Courts M non-urgent moderate 4 hours
As the example shows; the waiting time is 4 hours;
I need a piece of code to detect and alert when a time is MORE THAN 4 hours; to echo an alert.
Example code I've tried;
if $waitingtime == >4 hours
then echo "patient must be seen!"
EDIT FULL CODE
<?php
$conn = mysqli_connect("localhost","root","") or die ("No connection");
mysqli_select_db($conn, "a&e") or die('Could not select database.');
$query = "SELECT PatientID, Forename, Surname, Gender, Illness, Priority, Arrival_time, TIME_FORMAT(ABS(TIMEDIFF(CURTIME(), Arrival_time)),'%H hours') as Waiting_Time FROM Patient";
$result = mysqli_query($conn, $query) or die("Invalid query");
echo "<table border='1'>
<tr>
<th>PatientID</th>
<th>Forename</th>
<th>Surname</th>
<th>Gender</th>
<th>Illness</th>
<th>Priority</th>
<th>Waiting Time</th>
</tr>";
while ($row = $result->fetch_object()){
echo "<tr>
<td>" . $row->PatientID . "</td>
<td>" . $row->Forename . "</td>
<td>" . $row->Surname . "</td>
<td>" . $row->Gender . "</td>
<td>" . $row->Illness . "</td>
<td>" . $row->Priority . "</td>
<td>" . $row->Waiting_Time . "</td>
</tr>";
}
echo "</table>";
mysqli_close($conn);
?>

I'm using mysql_* for demonstation purposes. You should be using mysqli_* or PDO.
$query = "SELECT PatientID, Forename, Surname, Gender, Illness, Priority, Arrival_time, TIME_FORMAT(ABS(TIMEDIFF(CURTIME(), Arrival_time)),'%H') as Waiting_Time FROM Patient";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if ($row['Waiting_Time '] > 4)
{
echo "patient must be seen!"
}

Related

YII2 get the date after 10 years and 5 years

Here is the table structure.
tbl_staff
firstname varchar(50)
midname varchar(50)
lastname varchar(50)
...
tbl_staff2
first_day_of_service date
...
Here is the scenario.
For the first 10 years of stay of the staff in organization, he/she will be given a loyalty award and then every 5 years after he/she receives the first 10 years.
My initial code will just get the name of staff and the date of first day of service.
public function actionGet_loyalty() {
$query = (new \yii\db\Query())
->select(['firstname', 'midname', 'lastname', 'first_day_service'])
->from('tbl_staff')
->limit(100)
->offset(0)
->orderBy('first_day_service')
->leftJoin('tbl_staff2', 'tbl_staff2.staff_id = tbl_staff.id');
$command = $query->createCommand();
$data = $command->queryAll();
$string = "<table class='table table-striped'><tr><th>No.</th><th>Name</th><th>Date to Receive Loyalty Award</th></tr>";
$i = 1;
foreach ($data as $row) {
$firstname = $row['firstname'];
$midname = $row['midname'];
$lastname = $row['lastname'];
//$string.=$row['first_day_service'];
$string.="<tr>"
. "<td>$i</td>"
. "<td>" . $firstname . " " . $midname[0] . ". " . $lastname . "</td>"
. "<td>" . $row['first_day_service'] . "</td>"
. "</tr>";
$i++;
}
$string.="</table>";
return $string;
}
How to display all the name of staff and the date he/she will receive the award in increasing order.
Or any mysql query that will sort the name according the date?
If you want to do it via SQL query, in MySQL you can add date using DATE_ADD function:
DATE_ADD(date, INTERVAL 5 YEAR)
Check this similar question.
Or via PHP using DateTime modify method:
$dateTime = new DateTime($date);
$dateTime->modify('+ 5 years')->format('Y-m-d H:i:s');

Print out certain rows from a table

The names im using can be changed or removed in the database at times, as it is now i have to change every name in the script when that happends. Can i print it out w/o a IF for example?
<?php
$sql = "SELECT detail,
SUM(IF(depot = 'Huvuddepån', quantity, 0)) AS Huvuddepån,
SUM(IF(depot = 'Uddevalla', quantity, 0)) AS Uddevalla,
SUM(IF(depot = 'Jönköping', quantity, 0)) AS Jönköping,
SUM(IF(depot = 'Polyeten', quantity, 0)) AS Polyeten,
SUM(IF(depot = 'Krackern', quantity, 0)) AS Krackern
FROM quantities
GROUP BY detail";
$result = $conn->query($sql);
echo "<table border='0' cellspacing='3' cellpadding='3'>";
echo "<tr>";
echo "<td width='300'>"."<b>Detalj</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Huvuddepån</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Uddevalla</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Jönköping</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Polyeten</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Krackern</b>"."</td>";
echo "</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>".$row["detail"]."</td>";
echo "<td align='center'>".$row["Huvuddepån"]."</td>";
echo "<td align='center'>".$row["Uddevalla"]."</td>";
echo "<td align='center'>".$row["Jönköping"]."</td>";
echo "<td align='center'>".$row["Polyeten"]."</td>";
echo "<td align='center'>".$row["Krackern"]."</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
echo "</table>";
?>
What about this ? See sqlfiddle
SET #COLUMNS = NULL;
SELECT GROUP_CONCAT(
DISTINCT CONCAT(
'SUM(CASE WHEN depot = "',
depot,
'" THEN quantity ELSE 0 END) AS "',
depot,
'"'
)
) INTO #COLUMNS
FROM quantities;
SET #SQL = CONCAT(
'SELECT
detail,
',#COLUMNS,'
FROM
quantities
GROUP BY
detail'
);
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
It works with the following table :
CREATE TABLE quantities(
detail INT,
depot VARCHAR(20),
quantity INT
);
INSERT INTO quantities VALUES (1,"Huvuddepån",10);
INSERT INTO quantities VALUES (2,'Uddevalla',15);
INSERT INTO quantities VALUES (2,'Jönköping',20);
INSERT INTO quantities VALUES (3,'Polyeten',10);
INSERT INTO quantities VALUES (1,'Krackern',10);
INSERT INTO quantities VALUES (4,"fo'o6",10);
NOTE : Nevertheless, it will most likely break if one of your depot contains quotes ". If your data might contain such quotes, you should consider using REPLACE to replace those by \" in order to escape them.
The php code you should be using is the following one, I escaped the " in the sql because they weren't and I believe the problem came from here :
<?php
$sql = "SET #COLUMNS = NULL;
SELECT GROUP_CONCAT(
DISTINCT CONCAT(
'SUM(CASE WHEN depot = \"',
depot,
'\" THEN quantity ELSE 0 END) AS \"',
depot,
'\"'
)
) INTO #COLUMNS
FROM quantities;
SET #SQL = CONCAT(
'SELECT
detail,
',#COLUMNS,'
FROM
quantities
GROUP BY
detail'
);
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;";
$result = $conn->query($sql);
echo "<table border='0' cellspacing='3' cellpadding='3'>";
echo "<tr>";
echo "<td width='300'>"."<b>Detalj</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Huvuddepån</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Uddevalla</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Jönköping</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Polyeten</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Krackern</b>"."</td>";
echo "</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>".$row["detail"]."</td>";
echo "<td align='center'>".$row["Huvuddepån"]."</td>";
echo "<td align='center'>".$row["Uddevalla"]."</td>";
echo "<td align='center'>".$row["Jönköping"]."</td>";
echo "<td align='center'>".$row["Polyeten"]."</td>";
echo "<td align='center'>".$row["Krackern"]."</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
echo "</table>";
?>

Changing date format from database

I am trying to change the date format before it is displayed with SQL query however the date format is being completely ignored.
my code is
$query = "SELECT * , DATE_FORMAT(formatted, '%d/%m/%Y') from movies;";
then further down this is my table
echo "<table>"
echo "<table border='2'>"
echo "<tr>
<th>id</th>
<th>title</th>
<th>date</th>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['formatted'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
this query is working, however the date format is being ignored and just displaying the date in yyyy-mm-dd I want it in DD-MM-YY.
thanks
Use an alias to name your calculated column
SELECT * , DATE_FORMAT(datetime, '%d/%m/%Y') AS formatted_date
from movies
Use a different name than the existing column to differ between the two. Then use
echo "<td>" . $row['formatted_date'] . "</td>";
to get the formatted one.
You need to mention the alias for the formatted datetime column other wise formatted value will not be called in your code
SELECT * ,
DATE_FORMAT(`datetime`, '%d/%m/%Y') `datetime`
from movies

combine two pdo queries showing all results from the 2nd.

I had one table listing events and attendies. To try Database normalization i split this into two tables.
I have made a database listing all the planned games for a team.
Table 1
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+name + organiser + date + location + cost + notes + id+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
i then have a second database that has everyone how has marked they will be comming
Table 2
++++++++++++++++++++++++++
+id + event + player +
++++++++++++++++++++++++++
The id is unique to each, The id from table 1 is used as the event in table 2.
i have a simple PDO query that pulls the data from table 1 into a HTMl table
if($db->connect_error) {
die("Connection error: ".$db->connect_error);
}
$sql = $db->query('SELECT * FROM event ORDER BY `date` ASC'
) or die($db->error);
echo"";
while($row = mysqli_fetch_assoc($sql))
{
$a = $row['attendees'];//will look up attendies to tick a check box if already aknowledged
$b = htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
if (strpos($a,$b) !== false) {
$c = "checked='checked'";
}else{
$c = "";
}
$r=$row['id'];
echo "<div id='results'>";
echo "<CENTER>";
echo "<table BORDER=6 class='fixed'>";
echo "<TR> ";
echo "<TD COLSPAN=3 ALIGN=CENTER><form action='going.php' method='post' name='form".$r."'>Event ".$row['name']." i'm going
<input type='checkbox' name='going'".$c." onclick='document.form".$r.".submit ();'>
<input type='text' name='Organise' value='".$r."'>
<input type='text' name='name' value='".$b."'>
</form></TD>";
echo "</TR> ";
echo "<TR>";
echo "<td>";
echo "<B><u><font color='#0080FF'>Title </font></b></u>".$row['name']."<br></font>";
echo "<B><u><font color='#0080FF'>Orginiser </font></b></u>".$row['organiser']."<br></font>";
echo "<B><u><font color='#0080FF'>When </font></b></u>".date("D j-M-Y GA",$row['dt'])."<br></font>";
echo "<B><u><font color='#0080FF'>Location </font></b></u>".$row['location']."<br></font>";
echo "<B><u><font color='#0080FF'>Cost </font></b></u>£".$row['cost']."<br></font></TD>";
echo "<TD ROWSPAN=3 valign='top'><B><u><font color='#0080FF'>Attendies </font></b></u>".$row['attendees']."<br></font></TD>";//will change to table 2
echo "<TD ROWSPAN=3 valign='top'><B><u><font color='#0080FF'>notes </font></b></u>".$row['notes']."<br></font></TD>";
echo "</tr>";
echo "</table>";
echo "</CENTER>";
echo "</div>";
}
i have tried and joind these using
$sql = $db->query('SELECT t1.*, t2.event as t2event, t2.player as player
FROM `event` as t1
LEFT JOIN `going` as t2 on t1.id = t2.event
ORDER BY t1.`dt` ASC'
but all i got was a HTML table per event and per player. I'm sure its possible but can't work it out, can i create a html table from quering table 1 and add to attendies all those going from table 2 not just one or creating a result each one. ?

SQL Time exceeds

I currently have a system that outputs a waiting time, given their arrival time and current time; (current time - arrival time). How would I programme the code so that;
if the waiting_time exceeds 3 hours then alert "patient must be seen next!"
Here is the code I have so far to calculate waiting_time.
<?php
$conn = mysqli_connect("localhost","root","") or die ("No connection");
mysqli_select_db($conn, "a&e") or die('Could not select database.');
$query = "SELECT PatientID, Forename, Surname, Gender, Illness, Priority, Arrival_time, TIME_FORMAT(ABS(TIMEDIFF(CURTIME(), Arrival_time)),'%H hours') as Waiting_Time FROM Patient";
$result = mysqli_query($conn, $query) or die("Invalid query");
echo "<table border='1'>
<tr>
<th>PatientID</th>
<th>Forename</th>
<th>Surname</th>
<th>Gender</th>
<th>Illness</th>
<th>Priority</th>
<th>Waiting Time</th>
</tr>";
while ($row = $result->fetch_object()){
echo "<tr>
<td>" . $row->PatientID . "</td>
<td>" . $row->Forename . "</td>
<td>" . $row->Surname . "</td>
<td>" . $row->Gender . "</td>
<td>" . $row->Illness . "</td>
<td>" . $row->Priority . "</td>
<td>" . $row->Waiting_Time . "</td>
</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
$query = "SELECT PatientID, Forename, Surname, Gender, Illness, Priority, Arrival_time,
TIME_FORMAT(ABS(TIMEDIFF(CURTIME(), Arrival_time)),'%H hours') as Waiting_Time
FROM Patient
ORDER BY TIMEDIFF(CURTIME(), Arrival_time) DESC";
Then your results are ordered with the longest wait first.
<?php
while ($row = $result->fetch_object()): ?>
<tr>
<td><?=$row->PatientID?></td>
<td><?=$row->Forename?></td>
<td><?=$row->Surname?></td>
<td><?=$row->Gender?></td>
<td><?=$row->Illness?></td>
<td><?=$row->Priority?></td>
<td><?=$row->Waiting_Time?>
<? if ($row->Waiting_Time >= 3): ?>
<strong>Patient must be seen!</strong>
<? endif; ?>
</td>
</tr>
<? endwhile; ?>