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.
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 need to display:
/images/image1.jmp if the number is positive
or
/images/image2.jpg if the number is negative
or
/images/image3 if the number is 0.
$stmt = sqlsrv_query($conn,$sql);
echo "<table border='1'><tr><th>Offense</th><th>Previous Date Range</th><th>Current Date Range</th><th>Difference</th><th>Percentage Difference</><th>Up or down image in this column</></tr>";
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>" . $row['Offense']. "</td>";
echo "<td>" . $row['PreviousDateRange']."</td>";
echo "<td>" . $row['DateRange']."</td>";
echo "<td>" . $row['difference1']."</td>";
echo "<td>" . $row['percentchange']."</td>";
echo "<td>" . $row['']. "</td>";
}
echo "</table>";
?>
I found this code and have tried different ways if incorporating it in the echo "<td>" . $row['']. "</td> but not having any luck.
I get the code but can not manipulate it to fit what it needs to do. I'm sure it's a simple solution. Just frustrated.
switch ($myNumber) {
case 0:
echo "Zero is not a valid value.";
break;
case $myNumber < 0:
echo "Negative numbers are not allowed.";
break;
default:
echo "Great! Ready to make calculations.";
break;
}
Thanks for the help guys. Got the answer that worked.
I think this is a more readable solution.
I use an array to hold the links and use a calculation to see if it is negative or positive.
$img = ["-1" => "/images/image2.jpg",
"0" => "/images/image3.jpg",
"1" => "/images/image1.jpg"];
$number = 0;
Echo ($number == 0 ? $img[$number] : $img[$number/abs($number)]);
https://3v4l.org/JstZl
If the number is positive the calculation will be 15/15 => 1.
If the number is negative -10/10 => -1.
Edit:
$stmt = sqlsrv_query($conn,$sql);
echo "<table border='1'><tr><th>Offense</th><th>Previous Date Range</th><th>Current Date Range</th><th>Difference</th><th>Percentage Difference</><th>Up or down image in this column</></tr>";
$img = ["-1" => "/images/image2.jpg",
"0" => "/images/image3.jpg",
"1" => "/images/image1.jpg"];
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>" . $row['Offense']. "</td>";
echo "<td>" . $row['PreviousDateRange']."</td>";
echo "<td>" . $row['DateRange']."</td>";
echo "<td>" . $row['difference1']."</td>";
echo "<td>" . $row['percentchange']."</td>";
Echo '<td><img src="' . ($row['percentchange'] == 0 ? $img[$row['percentchange']] : $img[$row['percentchange']/abs($row['percentchange'])]) . '"></td>';
}
echo "</table>";
You didn't show us where $myNumber comes from so if the variable name is different just modify it in the code below.
echo '<td>/images/image' . ($myNumber == 0 ? '3' : ($myNumber >= 1 ? '1' : '2')) . '.jpg</td>';
Try:
if($myNumber < 0) {
//show /images/image2.jpg
}
if($myNumber == 0) {
//show /images/image3
}
if($myNumber > 0) {
//show /images/image1.jmp
}
In your code:
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>" . $row['Offense']. "</td>";
echo "<td>" . $row['PreviousDateRange']."</td>";
echo "<td>" . $row['DateRange']."</td>";
echo "<td>" . $row['difference1']."</td>";
echo "<td>" . $row['percentchange']."</td>";
echo "<td>";
if($row['percentchange'] < 0) {
//show /images/image2.jpg
}
if($row['percentchange'] == 0) {
//show /images/image3
}
if($row['percentchange'] > 0) {
//show /images/image1.jmp
}
echo "</td>";
}
This is assuming, based off of your other comments, that $row['percentchange'] is the number in particular that you care about checking against.
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.
I'm trying to re-write this code so that it goes with the columns that are going to be user-defined. With this, my challenge consists of
a) Needs to select random starting item from array
b) Select the next random color from the original array that is not equivalent to the most recent items selected based the number: $intNotesColumn + 1
I was thinking a do-while statement nested inside another was appropriate for this but am unsure how to go about this. Here is my code so far:
$metroUIcolors = array( "#A30061", "#8200CC", "008987", "#A05000", "#B85A93", "#C07807", "#E51400", "#297A29" );
$metroUIcolorsLength = count($metroUIcolors);
$intNotesColumn = 3; // This will be user-defined later
// Now I query the SQL database to get my base-code
if ($result->num_rows > 0) {
// output data of each row
echo '<table border=0 valign=top>'
. '<tr>'
. '<td colspan=' . $intNotesColumn . '>' . '<h1>header</h1>' . '</td>'
. '</tr>'
. '<tr>';
$counterRank = 1;
while($row = $result->fetch_assoc()) {
echo "<td bgcolor=" . $metroUIcolors[rand(0, $metroUIcolorsLength - 1)]. ">"
. "<h2>" . $row["username"] . '</h2><br />'
. "<p class='notes'>" . $row["notes"] . "</p>"
. "<p class='footnotes'>"
. "<br />Last Reset: " . $row["lastReset"]
. '<br />Last Update: ' . $row['lastUpdate']
. '<br />SessionID: ' . $row["sessionID"]
. "<br />Counter = " . $counterRank . "</td>". '</p>';
if ($counterRank % $intNotesColumn == 0)
{
echo '</tr><tr>';
}
$counterRank++;
}
echo '</tr></table>';
} else{
echo "No Notes Found in databases";
}
Then, why don't you do it order picking one color at a time. You could use shuffle() so that there will be a different starting color everytime.
<?php
$counterRank = 1;
// shuffle the array
shuffle($metroUIcolors);
while($row = $result->fetch_assoc()) {
$rand_color = $counterRank % $metroUIcolorsLength;
echo "<td bgcolor=" . $metroUIcolors[$rand_color]. ">";
// everything else
$counterRank++;
}
?>
If you insist on doing the way you said, you may create an array $colorCount which has color codes as keys and count as values.
<?php
$counterRank = 1;
$colorCount = array_fill_keys($metroUIcolors, 0);
while($row = $result->fetch_assoc()) {
do {
$rand_color = $metroUIcolors[rand(0, $metroUIcolorsLength - 1)];
} while ($colorCount[$rand_color] > 5);
echo "<td bgcolor=" . $rand_color. ">";
// everything else
$counterRank++;
$colorCount[$rand_color] += 1;
}
?>
I'm trying to have a "1" printed if there is a value in cDeviceRegistrationId column in the database. Here is the code:
$result is an SQL query
while($row = mysql_fetch_assoc($result))
{
if ($row['cDeviceRegistrationId'] > 0) {
$a = 1;
}
echo "<tr class='forum'>";
echo "<td class='forum'>" . $row['intUserID'] . "</td>";
echo "<td class='forum'>" . $row['cUsername'] . "</td>";
echo "<td class='forum'>" . $row['cEmail'] . "</td>";
echo "<td class='forum'>$a</td>";
echo "<td class='forum'>" . $row['uCreateDate'] . "</td>";
echo "</tr>";
}
The value of $a is not overwritten if it does not meet the condition, meaning other iterations may get the value of 1 incorrectly. Here is a fix (replace your if statement):
$a = ($row['cDeviceRegistrationId'] > 0) ? 1 : '';
How about:
if( !is_null($row['cDeviceRegistrationId']) ){
$a = 1;
}