echo timestamp in proper day field - php

In mysql I have unix epoch time inserted and it needs to be displayed in calendar like table.
Every timedate should be echoed in row that represents that day.
Code that I have is this and it echoes 31 days in one column and all timestamps in one row. How to put them in correct day/row? Thank you.
<table>
<tr>
<td><strong>day</strong></td>
<td><strong>timestamp</strong></td>
</tr>
<?php
$sel = "select time from rv where id=$id";
$rez = mysql_query($sel) or die(mysql_error());
$n = mysql_num_rows($rez);
for($day=1; $day<32; $day++){
echo "<tr>";
echo "<td>$day.</td>";
while ($re = mysql_fetch_array($rez)){
$time_b = $re["time"];
$time_a = new DateTime("#$time_b");
echo "<td>";
echo $time_a->format('h:i:s');
echo "</td>";
}
echo "</tr>";
}
?>
</table>
Update:
Here is what I get now. And I need every date to be displayed in correct day/row
http://s3.postimg.org/nvmlf1mtv/test.jpg

Supposing you want your days from the database sorted from 1-31, your working the wrong way rond.
You first want to sort the values from the database, store by day and then display them.
Warning: I'm using your code for this answer, but DON'T USE mysql, use mysqli.
//make an array with all 31 days
$mydays=array_fill(1,31,array());
//loop the database results and fill the array
while ($re = mysql_fetch_array($rez)){
$day=date('d',$re["time"]);
//store the value in the $mydays array
$mydays[$day][]=$re["time"];
}
//now loop the days and display them
for($i=1;$i<32;$i++){
if(count($mydays[$i])<1)continue; //the is no data in this day
// else loop the data inside this day
foreach($mydays[$i] as $thisday){
echo "<tr>";
echo "<td>$i</td>";
echo "<td>";
echo date('h:i:s',$thisday);
echo "</td>";
echo "</tr>"
}
}

Related

Change cell color based on amount of time passed

So, I'm hitting a brick wall. What I'm trying to do is change the color of my "waiting" cell in my table based off the amount of time that has passed. When queried my "waiting" cell displays a time stamp in the format of 00:00:00. To make my coding more consistent and easier for the next guy who may come along and replace me. I'm querying a view that I created within my mysql database that updates my timestamp. So, all my html/php code has to do is query the view to pull in the information I want displayed.
I've scoured the internet and found several bits and pieces of code that it supposed to be able to change a cells color based on value. I've had a little bit of luck but have not gained the desired results. I either end up with cells that stay the same color and not change or find there is something wrong with the code that prevents the whole page from loading.
include 'config/database.php';
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$records_per_page = 5;
$from_record_num = ($records_per_page * $page) - $records_per_page;
$query = "SELECT ptfin, ptname, ptdob, pore, notes, labstat,
rtstat,radstat,waiting
FROM dashboard_view
WHERE ( labstat != '' AND labstat IS NOT NULL)
OR (radstat != '' AND radstat IS NOT NULL)
OR (rtstat != '' AND rtstat IS NOT NULL)
ORDER BY waiting DESC
LIMIT :from_record_num, :records_per_page";
$stmt = $con->prepare($query);
$stmt->bindParam(":from_record_num", $from_record_num, PDO::PARAM_INT);
$stmt->bindParam(":records_per_page", $records_per_page, PDO::PARAM_INT);
$stmt->execute();
$num = $stmt->rowCount();
$curDate = time();
$mysqlTimestamp = $row['waiting']; //This is the piece of code that is giving me issues.
$dif = strtotime($curdate) - strtotime($mysqlTimestamp);
if($num>0)
if($dif < 10000) {
$tdStyle='background-color:green;';
} else {
$tdStyle='background-color:red;';
}
echo "<table class='table table-hover table-responsive table-bordered'>";
echo "<tr>";
echo "<th>FIN#</th>";
echo "<th>Name</th>";
echo "<th>PorE</th>";
echo "<th>Notes</th>";
echo "<th>Modalities</th>";
echo "<th>Waiting</th>";
echo "</tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td width=15>{$ptfin}</td>";
echo "<td width=100>{$ptname}</td>";
echo "<td width=5>{$pore}</td>";
echo "<td width=200>{$notes}</td>";
echo "<td width=170><img src=".$labstat." ><img src=".$rtstat." ><img src=".$radstat." ></td>";
echo "<td style=\"$tdStyle\" width=50>{$waiting}</td>";
//echo "<td width=5>{$dif}</td>"; code for testing visual output of timestamp mathmatics
echo "</tr>";
}
echo "</table>";
What I would like to have happen is if the timestamp is less than 10 minutes the cell displays green. If the timestamp falls into the range of 10 to 15 minutes to the cell displays yellow. If the timestamp is greater than 15 minutes the cell displays red.

Echo specific row from mysql database, within set of range

I tried to echo rows from mysql database. So in my code user can enter a value, and then program echoes table around the entered value. That part of the code works fine, but when I tried to echo entered values and corresponding row to the value, it somehow gave me first row. While I needed exact row, which I previously entered.
Here is my code:
$density = $_POST["density"];
$t = $_POST["t"];
$query = $pdo->prepare ("SELECT * FROM mytable WHERE "
."(Density >= '$density'-0.05 AND Density <='$density' +0.05) AND "
."(Temp >='$t' -2 AND Temp <='$t' +2) ");
if (!$query->rowCount() == 0) {
$query -> execute();
while ($results = $query->fetch(PDO::FETCH_ASSOC)){
echo "<table>";
echo "<tr><td>";
echo $results['Temperature'];
echo "</td><td">";
echo $results['Density'];
echo "</td><td>";
echo $results['DensityValues'] + $delden;
echo "</td></tr>";
echo "</table>";
}
}
So currently it prints the first row which is: Temp=>10, Density=>600, DensityValues=>658;
While I need to get values I have enterd and coresponding density value: Temp=>12, Density=>650, DensityValues=>678;

Organise seats into tablerows in html output with PHP

I'm putting together something that's mean to allow for a user to book seats for a cinema showing. The row and seat numbers for every showing are stored in the database. I'm currently extracting them in the following method so that users can click on a seat button to select that seat for their booking:
echo "<form>";
echo "<table>";
while($row = mysqli_fetch_assoc($result)){
$rownum = $row['row'];
$seat = $row['seat'];
echo "<tr><td><button type=\"submit\" name=\"seatsel\" value=\"$rownum$seat\">$rownum$seat</button></td></tr>";
}
echo "</table>";
Right now this just outputs html showing all of the buttons as a single row in the table. I'd like the output to show seating across a single table row for every one of the table rows in the cinema screen. I'm not sure how to do this exactly given that each row is of differing lengths. E.g row A has twelve seats while row C has eight.
What would be the best way of accomplishing this?
You could easily update your code so that you will get a new table row every time the mysql row has another value. One thing to note is that you might want to add (depending on whether you're already sorting your results) the following ORDER BY row,seat.
echo "<form>";
echo "<table>";
echo "<tr>";
while($row = mysqli_fetch_assoc($result)){
if (!isset($oldrownumber)) $oldrownumber = $row['row'];
else if ($oldrownumber != $row['row']) {
echo "</tr><tr>";
$oldrownumber = $row['row'];
}
$rownum = $row['row'];
$seat = $row['seat'];
echo "<td><button type=\"submit\" name=\"seatsel\" value=\"$rownum$seat\">$rownum$seat</button></td>";
}
echo "</tr>";
echo "</table>";

How to do an HTML 'upward' rowspan

Here is the problem:
I need to know if there is a way to do an upward rowspan on a <th> element on a form.
I am reading some rows inside a DB that I need to put inside an html table.
I am doing something like:
echo "<table>";
while($result = $resultSet->fetch())
{
echo "<tr>";
echo "<td>$result['Name']</td>";
echo "<td>$result['Job']</td>";
echo "</tr>";
}
ehco "</table>";
First steps are easy until I needed to 'span' together two adjacent cells that contains the same value.
For exemple if someone have the same name but had two jobs I would like them to have something like :
echo "<tr>";
echo "<td rowspan='2'>$result['Name']</td>";
echo "<td>$result['Job']</td>";
echo "</tr>";
But I can't predict how many jobs someone have (except they all have at least one and that they are all in order).
exemple of record in MySQL table
Name/**/Jobs
Paul/**/Jobs1
Simon/**/Jobs23
Simon/**/Jobs45
Roger/**/Jobs67
(All simon's jobs are 'behind one another', they are grouped. If Paul had a second job it would be the second record in the table 'pushing' simons jobs down).
So I need to change the rowspan value of the first element to fit how many jobs that person have.
This is why I would need to know if it is possible to do a upward span because i could just display every on of them and count how many jobs each person have and do a rowspan that would merge the table cell upward.
It would certainly be easier then fetching every row in the DB then looping throught them all and checking how many jobs a person have to 'span' the cells now and then display them all and then skipping them. and so on.
Thanks
A situation like this calls for pre-processing. Like so:
$people = array();
while($result = $resultSet->fetch())
{
if( !isset($people[$result['Name']])) $people[$result['Name']] = array();
$people[$result['Name']][] = $result['Job'];
}
echo "<table>";
foreach($people as $name=>$jobs)
{
echo "<tr>";
echo "<td rowspan=\"".count($jobs)."\">".$name."</td>";
echo "<td>".array_shift($jobs)."</td>";
echo "</tr>";
foreach( $jobs as $otherjob)
{
echo "<tr><td>".$otherjob."</td></tr>";
}
}
echo "</table>";
Done!
What you're looking for doesn't exist in HTML. But you can do it like this:
$currName = $result['Name'];
echo "<tr>";
if($currName==$prevName)
{
echo "<td> </td>";
}
else
{
echo "<td>$result['Name']</td>";
}
echo "<td>$result['Job']</td>";
echo "</tr>";
$prevName = $currName;
Rather than using rowspan, just nest a table in the second column with all of the jobs for that person.
echo "<tr>";
echo "<td>$result['Name']</td>";
echo "<td>";
echo "<table>"
foreach($jobs as $job): //$jobs holds the job values for the current name
echo "<tr><td>";
echo $job;
echo "</td></tr>";
endforeach;
echo "</table>";
echo "</td>";
echo "</tr>";
Note: you will need to preprocess the result set to get a 2D array for this method (see answer from Niet the Dark Absol).

Display results from MySQL query into table using PHP

This is for a timetable and what it does is displays the previous day's timetable and who has booked each slot (this is for a radio station.)
At the moment it displays who has booked each slot in chronological order however I would like it to say the time next to each result. The query outputs 24 rows (which is from Midnight to 23:00) and next to each slot I would like it to say (00:00, 01:00, 02:00, 03:00... 21:00, 22:00 so on so forth.)
This is my current code:
<?php
include("../config.php");
#// Timetable Clearup Variabls
$yesterday = strtotime('yesterday');
$yesterdow = date('l',$yesterday);
echo "<table width=\"580px\" class=\"board\" border=\>";
$order = "SELECT * FROM timetable WHERE day = '$yesterdow'";
$result = mysql_query($order);
// Error checking
if (!$result) {
// output error, take other action
}
else {
while ($row=mysql_fetch_array($result)){
// Append all results onto an array
$rowset[] = $row;
}
}
foreach ($rowset as $row) {
echo "<tr><td>" . htmlspecialchars($row['username']) . "</td></tr>";
}
?>
Can you help?
I think we're all looking too hard at a VERY simple problem. You are already using SELECT * in your query, so you're already fetching all three columns from your table. So now, all you need to do is add another cell to each row of your table.
echo "<tr><td>" . htmlspecialchars($row['username']) . "</td><td>" . htmlspecialchars($row['time']) . "</td></tr>";
And to make sure you are fetching your rows in the correct order, you should add an ORDER BY to your query:
SELECT * FROM timetable WHERE day = '$yesterdow' ORDER BY time
If you don't specify an ORDER BY clause, you have no guarantee that you will get the results in any particular order.
And one last thing, you are looping through the rows twice, unnecessarily. Get rid of the foreach loop and put the echo directly inside the while loop.
try this:
foreach ($rowset as $row) {
echo "<tr><td>" . htmlspecialchars($row['username']) . htmlspecialchars($row['time'])"</td></tr>";

Categories