echo '<table style="width:100%"> <tr>';
echo '<td>Order</td>';
echo '<td>Destination</td>';
echo '<td>Location</td>';
echo '<td>Status</td>';
echo '<td>TimeStamp</td>';
echo '</tr>';
if($result) {
while($row = mysqli_fetch_assoc($result)) {
echo '<tr><td>';
echo $row['OrderNumber'] . '';
echo '</td><td>';
echo $row['Destination'] . '';
echo '</td><td>';
echo $row['Location'] . '';
echo '</td><td>';
echo $row['Status'] . '';
echo '</td><td>';
echo $row['TimeStamp'] . '';
echo '</td></tr>';
}
echo '</table>';
}
I want to change the background of the row turned a different color is the time stamp is more than 60 minutes past the current time. any help would be much appreciated. i dont even know where to begin.
Thanks
edit: format of my time stamp "2015-07-17 19:17:31"
Do an if to see if time is over 60 minutes and if so assign it a class with a different background color. Since you didn't clarify I'm going to assume you are using unix timestamp time().
$currTime = time();
if($result) {
while($row = mysqli_fetch_assoc($result)) {
$calc = $currTime - $row['TimeStamp'];
if($calc > 3600){
$rowClass = "oldOrder";
} else {
$rowClass = "normalOrder";
}
echo '<tr class="'.$rowClass.'"><td>';
echo $row['OrderNumber'] . '';
echo '</td><td>';
echo $row['Destination'] . '';
echo '</td><td>';
echo $row['Location'] . '';
echo '</td><td>';
echo $row['Status'] . '';
echo '</td><td>';
echo $row['TimeStamp'] . '';
echo '</td></tr>';
}
Then add CSS to define the two classes
.oldOrder{
background-color: #ccc;
}
.normalOrder{
background-color: #fff;
}
$NowDT = date("Y-m-d H:i:s");
$NowRDT = strtotime($NowDT);
$DateTimeNF = $row['TimeStamp'];
$TimeRF = strtotime($DateTimeNF);
$TimeDifference = $NowRDT - $TimeRF;
$TimeDifferenceHours = $TimeDifference/3600;
If ($TimeDifferenceHours > "1")
{
echo '<tr bgcolor="#E60000"><td>';
}
else
{
echo '<tr><td>';
}
It seems like your timestamp is a String, you need to convert your string to a Unix TimeStamp using strtotime:
$rowTime=strtotime($row['TimeStamp']);
and then substract it from the actual time, as the previous answer, to obtain the difference in seconnds. Divide it by 60 and you get it on minutes.
$currenTime=time();
$diffSeconds=$currenTime-$rowTime;
$diffMinutes=$diffSeconds/60;
and then check if the difference is bigger than 60:
$myCSS="normalRow";
if ($diffMinutes>60) { $myCSS="differentRow"; }
and then use that CSS Style ($myCSS) in your table row:
echo '<tr class="'.$myCSS.'"><td>';
You need to define those two styles in your CSS file or your HTML Header like:
<style>
.normalRow {
background-color: #888;
}
.differentRow {
background-color: #ccc;
}
</style>
And that's it. Hope it helps.
Related
I am trying to display a list of Links in a table according to Distance and Category. I would like each distance to be a Tab and have the appropriate Links in each Tab. I am trying to accomplish this with A PHP Foreach loop and jQuery-UI Tabs.
Here is the code which gets the data and displays it in the table in each Tab.
The index function in the controller for the View and the function that gets the table data:
public function index() {
$data = array();
$db_name = $this->uri->segment(2);
$this->db->db_select($db_name);
$tables = $this->db->get('tableinfo');
$data['distances'] = array();
$data['tables'] = array(
'men' => array(),
'women' => array()
);
foreach($tables->result() as $row) {
if(!in_array($row->distance, $data['distances'])) {
array_push($data['distances'], $row->distance);
}
if(substr($row->displayname, 0, 4) == "Male") {
array_push($data["tables"]['men'], $row->displayname);
} else {
array_push($data["tables"]['women'], $row->displayname);
}
}
$data['dbname'] = $db_name;
$this->parser->parse('templates/header', $data);
$this->parser->parse('select/index', $data);
$this->parser->parse('templates/footer', $data);
}
public function gettable($table) {
$db_name = "championchipco_sowetomarathon2019";
$this->db->db_select($db_name);
redirect("results/index/" . $db_name . "/" . $table);
}
And the View:
<?php
$i = 1;
echo "<div class='row'>";
echo "<div class='col' id='tabs'>";
echo "<ul>";
foreach($distances as $distance) {
echo "<li><a href='#tabs-" . $i . "'>" . $distance . "</a></li>";
}
echo "</ul>";
foreach($distances as $distance) {
echo "<div id='tabs-" . $i . "'>";
echo "<table class='table' cellspacing='10' cellpadding='10'>";
echo "<tr><th style='font-size: 20px;'>Men</th><th style='font-size: 20px;'>Women</th><th style='font-size: 20px;'></tr>";
foreach($tables['men'] as $table) {
if(substr($table, -4) == $distance) {
echo "<tr>";
echo '<td>' . $tables['men'][$i] . '</td>';
echo '<td>' . $tables['women'][$i] . '</td>';
echo "</tr>";
}
}
echo "</table>";
echo "</div>";
$i++;
}
echo "</div>";
echo "</div>";
?>
At the moment, all of the data is being displayed in every Tab, instead of only display the Links for the particular category in a different tab. I can see that the 2nd table of Men and Women is slightly to the left of the top one so I think the loop is causing something to go wrong.
I have tried re-arranging the way the loops display the data in the View but cannot seem to get only the 10KM in the 10KM Tab, 21KM in 21KM Tab, etc.
Get the data of your second foreach by Ajax it will made your need simple
I mentioned to remove below foreach
foreach($distances as $distance) {
echo "<div id='tabs-" . $i . "'>";
echo "<table class='table' cellspacing='10' cellpadding='10'>";
echo "<tr><th style='font-size: 20px;'>Men</th><th style='font-size: 20px;'>Women</th><th style='font-size: 20px;'></tr>";
foreach($tables['men'] as $table) {
if(substr($table, -4) == $distance) {
echo "<tr>";
echo '<td>' . $tables['men'][$i] . '</td>';
echo '<td>' . $tables['women'][$i] . '</td>';
echo "</tr>";
}
}
echo "</table>";
echo "</div>";
$i++;
}
I have a page on my site that shows a member directory. I want the members to be listed 3 per row, before it goes to the next row. The code I have, does this for the very first row - but on the second row, its all on one line and doesnt carry down.
The profiles are showing up like this:
uuu
uuuuuuuuuuuuuuuuuuuuuuuuuuu
When they should be doing this:
uuu
uuu
uuu
uuu
This is what my code looks like:
<table>
<tr>
<td colspan="4"><h1>Member Directory</h1></td></tr>
<tr>
<td>
<?php
while($row = mysql_fetch_array($result)) {
if (empty($row['profile']) === false){
echo '<img src="', $row['profile'], ' "width="125">';
} else {
echo '<img src="../../images/template/avatar.png">';
}
echo '</td><td>';
echo '' . ucfirst($row['username']) . '<br />';
echo "Location: " . $row['location'] . "<br />";
echo '</td><td>';
if ($i++ == 2) echo '</td></tr><tr><td>';
}
?>
</td>
</tr>
</table>
Any help would be greatly appreciated, thanks!
Use
if (++$i % 3 == 0) echo '</td></tr><tr><td>';
Explanation:
First of all ++$i first increments $i, and then uses it in whatever is next, this makes for more readable code.
Second, the % is the modulus, which means it sortof subtracts 3 from $i until it is not possible anymore. E.g. 9 % 3 == 0, and 11 % 3 == 2 and so on. This means we know that we have printed 3 rows whenever $i % 3 equals 0.
try this one
<table>
<tr>
<td colspan="4"><h1>Member Directory</h1></td>
</tr>
<?php
$count=0;
while($row = mysql_fetch_array($result))
{
$count+=1;
if($count%3==1)
{
echo '<tr>';
}
echo '<td>';
if (empty($row['profile']) === false){
echo '<img src="', $row['profile'], ' "width="125">';
} else {
echo '<img src="../../images/template/avatar.png">';
}
echo '</td>';
echo '<td>';
echo '' . ucfirst($row['username']) . '<br />';
echo "Location: " . $row['location'] . "<br />";
echo '</td>';
if($count%3==0)
{
echo '</tr>';
}
}
if($count%3!=0)
{
echo '</tr>';
}
?>
</table>
You didn't reset the value of $i, so it kept increasing; another issue is that if you have only seven items, the last row should have four empty cells. So the loop condition needs to be augmented with a row completion status:
$i = 0;
while (($row = mysql_fetch_array($result)) !== false || $i != 0) {
if ($i == 0) {
echo '<tr>'; // start of new row
}
if ($row !== false) {
echo '<td>';
if (empty($row['profile'])) {
echo '<img src="', $row['profile'], ' "width="125">';
} else {
echo '<img src="../../images/template/avatar.png">';
}
echo '</td><td>';
echo '' . ucfirst($row['username']) . '<br />';
echo "Location: " . $row['location'];
echo '</td>';
} else {
echo '<td></td><td></td>'; // no more data
}
$i = ($i + 1) % 3; // advance
if ($i == 0) {
echo '</tr>'; // end of the row
}
}
I've got an array with images and a date for each one. I'm retrieving the date via array explode. I want to take this date value and compare it to the previous date value for the previous image. My goal here is to display these dates as headers for the sets of images that correspond to each different date.
Here is my code I've been fiddling with to try and make this work (sorry for all my slashes):
$p = 0;
$a = -1;
echo "<table>";
echo "<tr>";
foreach (array_combine($images,$locs) as $image => $loc)
{
$p++;
$a++;
$datesort = explode('>',$image);
echo "<style>";
echo "input[label=t" . $p . "] {";
echo "display: none;";
echo "}";
echo "input[label=t" . $p . "] + label {";
echo "border: 5px solid #FFFFFF;";
$image = "background: url('http://blah.com/" . $loctb[$p] . "');";
echo $image;
echo "height: 61px;";
echo "width: 92px;";
echo "display: inline-block;";
echo "padding: 0 0 0 0px;";
echo "}";
echo "input[label=t" . $p . "]:checked + label {";
echo "border: 5px solid #FF9900;";
echo "background: url('http://blah.com/" . $loctb[$p] . "');";
echo "height: 61px;";
echo "width: 92px;";
echo "display: inline-block;";
echo "padding: 0 0 0 0px;";
echo "}";
echo "</style>";
$lastdate = array("");
$lastdate[$a] = $datesort[1];
echo "<td>";
if ($lastdate[$a] <> $datesort[1]){
echo "<h2>";
echo $datesort[1];
echo "</h2>";
}
echo "<input type=\"checkbox\" label=\"t" . $p . "\" id=\"t" . $p . "\" name =\"boxes[]\" value=\"<img src=http://blah.com/" . $loc . "\" />";
echo "<label for=\"t" . $p . "\"></label>";
echo "</div>";
echo "</td>";
if ($p % 2 == 0)
{
echo "</tr>";
}
}
echo "</table>";
I've been trying to store the current date value into another array variable and then compare that to the new value the next time through my foreach loop, but I'm doing it wrong :-/...
I'd appreciate any help you can offer. Thanks
Here is a quick and dirty way, though you'll want to tune it to your needs (I had this issue but it was worse as I was also filling in the blank dates in between):
$previous_dates = array();
foreach (array_combine($images,$locs) as $image => $loc) {
$datesort = explode('>',$image);
if (end($previous_dates) <> $datesort[1]) {
echo "<h2>";
echo $datesort[1];
echo "</h2>";
}
// ... more stuff ...//
$previous_dates[] = $datesort[1];
}
Really I would reconsider the approach you're taking for the explode within the loop, but first-things-first, see if that gets you where you need to be.
The table below prints of the results of a query called $sqlStr3. It works fine. The query is set to return the top 25 results.
I would like to apply a unique CSS class (called "class1" for purposes of this question) to the top ten results in the query. How can I do this?
Thanks in advance,
John
$result = mysql_query($sqlStr3);
$count = 1;
$arr = array();
echo "<table class=\"samplesrec1edit\">";
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td class="sitename1edit2a">'.$count++.'.</td>';
echo '<td class="sitename1edit1">'.stripslashes($row["username"]).'</td>';
echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
echo '</tr>';
}
echo "</table>";
use this:
$result = mysql_query($sqlStr3);
$count = 1;
$arr = array();
echo "<table class=\"samplesrec1edit\">";
while ($row = mysql_fetch_array($result)) {
if($count < 11){
echo '<tr class="top-10">';
}else{
echo '<tr class="non-top-10">';
}
echo '<td class="sitename1edit2a">'.$count++.'.</td>';
echo '<td class="sitename1edit1">'.stripslashes($row["username"]).'</td>';
echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
echo '</tr>';
}
echo "</table>";
access the td of top10 using this css
.top-10 td{
//definitions
}
and access the td of non top10 lines with
.non-top-10 td{
//definitions
}
... class="...<?php if ($count <= 10) { ?> class1<?php } ?>" ...
And move the increment to the end of the loop.
you can do it in the server side via php like:
echo '<tr '.($count <=10 ? 'myclass' : '').' > ;
or in jquery in front side like:
$("someTableSelector").find("tr:lt(10)").addClass('myclass')
How about assigning each result from TOP10 a unique class called top_result_[n]?
while ($row = mysql_fetch_array($result)) {
echo '<tr'.($count++ <=10 ? ' class="top_result_'.$count.'"' : '').'>';
echo '<td class="sitename1edit2a">'.$count.'.</td>';
echo '<td class="sitename1edit1">'.stripslashes($row["username"]).'</td>';
echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
echo '</tr>';
}
echo "</table>";
I'm not sure wether you want to apply ONE class for all 10 results or UNIQUE class for each from 10 results, as for the first case, the code would be:
while ($row = mysql_fetch_array($result)) {
echo '<tr'.($count++ <=10 ? ' class="top_result"' : '').'>';
echo '<td class="sitename1edit2a">'.$count.'.</td>';
echo '<td class="sitename1edit1">'.stripslashes($row["username"]).'</td>';
echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
echo '</tr>';
}
echo "</table>";
Use CSS:
.samplesrec1edit tr:nth-child(-n+10) {
cssrule:value;
}
This technique only works in newer browsers however. The following link has compatibility charts.
http://reference.sitepoint.com/css/pseudoclass-nthchild
Can someone show me how to change this date stamp and print this in an html table?
I have an input file with this time stamp format:
4-Start=20100901180002
This time format is stored like this in an array.
I print out the array like so to create an html table:
foreach ($data as $row){
$counter ++;
$class = $counter % 2 === 0 ? 'alt1' : 'alt2';
echo '<tr class="' . $class . '">';
foreach ($keys as $column)
if (isset($row[$column])){
echo '<td>' . $row[$column];
} else {
echo '<td>' . '' . '</td>';
}
}
echo '</table>';
How do I change the timestamp in this table to this? 2010-09-01 18:00:02
This is what you are looking for,
http://de2.php.net/manual/en/function.strtotime.php
There is a similar question you can get more inputs from there as well..
How do I format the date and time That is received from database
EDIT:
Yes you can use it an echo as well
echo date("Y-m-d H:i:s",strtotime('20100901180002')) ; // 2010-09-01 18:00:02
You can even use CreateFromFormat as RC said, this is using CreateFromFormat in Procedural style.
$date = date_create_from_format("YmdHis", '20100901180002');
echo date_format($date, 'Y-m-d H:i:s'); // 2010-09-01 18:00:02
Refer to http://php.net/manual/en/datetime.createfromformat.php
This part is strange, the elseif is never reached in my opinion.
if (isset($row[$column])){
echo '<td>' . $row[$column] . '</td>';
} elseif ($column == 'Condition') {
echo '<td> Error </td>';
} else {
echo '<td> </td>';
}
Regarding your format issue:
// PHP > 5.2
$date_s = "" . $row['Start'];
$date = DateTime::createFromFormat("YmdHis", $date_s);
echo $date->format("Y-m-d H:i:s"); // 2010-09-01 18:00:02