Hi I have this code which should change the background colour if the date is 2 months near expiry but for some reason it doesn't work properly. I think it only changes if the year is smaller not the actual date. Any help is appreciated.
my code is:
$expires = date("Y/m/d",(mktime(0, 0, 0, date("m")+2, date("d"), date("Y"))));
echo ($expires."<br>");
$dateDue = date('m/d/Y');
$result = $db->prepare("SELECT * FROM tblitemdates ORDER BY name ");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr class="record">
<?php
if ($row["dateDue"] == NULL)
echo '<tr style="background-color:#ffffff">';
else if ($row["dateDue"]< $expires)
echo '<tr style="background-color:#FFCC99">';
else if ($row["st1"] == NULL)
echo '<tr style="background-color:#ffffff">';
else if ($row["st1"]< $expires)
echo '<tr style="background-color:#FFCC99">';
else
echo '<tr style="background-color:#ffffff">'; ?>
regards,
B
Firstly, I find its always better to compare unix timestamps as its a much easier format to work with in terms of comparing dates to each other.
you can get this with your expires variable as follows...
$expires = date("Y/m/d",(mktime(0, 0, 0, date("m")+2, date("d"), date("Y"))));
$expiresUnix = strtotime($expires); // This should give you the unix timestamp of your formatted date.
[ RUN DB FETCH CODE ]
$dateDueUnix = strtotime($row['dateDue']);
$st1Unix = strtotime($row['st1']);
and then in your if else...
<?php
if ($dateDueUnix == NULL)
echo '<tr style="background-color:#ffffff">';
else if ($dateDueUnix< $expiresUnix)
echo '<tr style="background-color:#FFCC99">';
else if ($st1Unix == NULL)
echo '<tr style="background-color:#ffffff">';
else if ($st1Unix < $expiresUnix)
echo '<tr style="background-color:#FFCC99">';
else
echo '<tr style="background-color:#ffffff">'; ?>
Note you may need to change the == NULL to == 0 as I'm not too sure if $st1Unix will do the conversion if no argument is supplied. Hope this gets you somewhere
Remove the extra <tr class="record">
$expires = date("Y/m/d",(mktime(0, 0, 0, date("m")+2, date("d"), date("Y"))));
echo ($expires."<br>");
$dateDue = date('m/d/Y');
$result = $db->prepare("SELECT * FROM tblitemdates ORDER BY name ");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<?php
if ($row["dateDue"] == NULL)
echo '<tr style="background-color:#ffffff">';
else if ($row["dateDue"]< $expires)
echo '<tr style="background-color:#FFCC99">';
else if ($row["st1"] == NULL)
echo '<tr style="background-color:#ffffff">';
else if ($row["st1"]< $expires)
echo '<tr style="background-color:#FFCC99">';
else
echo '<tr style="background-color:#ffffff">'; ?>
Related
Recently I posted this question (Converting day of week to dates for the current month with MYSQL & PHP) and got many helpful replies.
I'd like to build on this and do something more but again am reaching the limits of my knowledge and am looking for some guidance.
Purpose:
I have weekly recurring events/attendance, which I am able to match and display with the date of the current month thanks to above. Also, I have a database of cancellations/date changes to these weekly recurring events/attendance.
I'd like to match these two to display in one table.
Here's the current situation:
As you can see I'm making this for mobile. Currently I show the set schedule on top with the bottom displaying the cancellations (in red) and the one-off reservations to other events (in green).
Here's my code for the first part:
$sql = "SELECT StudentDB.studentid, ClassDB.classID, ClassDB.class_level, ClassDB.class_title, ClassDB.time, ClassDB.teacher, StudentDB.first_name, StudentDB.last_name, StudentDB.payment_amount, ClassDB.day
FROM ClassDB
INNER JOIN RegDB ON ClassDB.classID = RegDB.classid
INNER JOIN StudentDB ON StudentDB.studentID = RegDB.studentid
WHERE StudentDB.studentid = '$studentid'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table class='table table-hover'>";
echo "<tr><th colspan=2 class='text-center'>固定レッスン</th></tr><tr><th>クラス</th><th>開始時間</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
$dayofclass = $row['day'];
echo "<tr><td>".$row["class_level"]." ".$row["class_title"]."</td><td>".$row['day']." ".$row['class_time']." " .$row['time']. "</td></tr>";
}
$n=date('t',strtotime($date)); // find no of days in this month
$yearmonth = date("Y-m-");
$dates=array();
for ($i=1;$i<=$n;$i++){
if ($i<10) {
$i = "0" . $i;
}
$day=date("l",strtotime($yearmonth.$i)); //find weekdays
if($day==$dayofclass){
$dates[]=$yearmonth.$i;
}
}
$arrlength = count($dates);
for($x = 0; $x < $arrlength; $x++) {
$y = $x +1;
echo "<tr><td>Week ".$y."</td><td>".$dates[$x]."</td></tr>";
}
echo "</table>";
}
And code for the second part:
$sql = "SELECT AttendanceDB.*, ClassDB.*
FROM StudentDB
INNER JOIN AttendanceDB ON StudentDB.studentid = AttendanceDB.studentid
INNER JOIN ClassDB ON AttendanceDB.classid = ClassDB.classID
WHERE StudentDB.studentid = '$studentid' AND AttendanceDB.class_time >= '$date'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table class='table table-hover'>";
echo "<tr><th colspan=2 class='text-center'>振替の予定</th></tr><tr><th>クラス</th><th>開始時間</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
$phpdate = strtotime( $row["class_time"] );
$mysqldate = date( 'Y-m-d', $phpdate );
if ($row["furikae"] == 3){
echo "<tr class=success><td>振替(".$row["class_level"]." ".$row["class_title"].")</td><td>".$mysqldate." " .$row['time']. "</td></tr>";
} elseif ($row["furikae"] == 8) {
echo "<tr class=warning><td>承認待ち</td><td>".$mysqldate." " .$row['time']. "</td></tr>";
} elseif ($row["furikae"] == 2) {
echo "<tr class=danger><td>休み</td><td>".$mysqldate." " .$row['time']. "</td></tr>";
}
}
}
The first set of data is being stored in an array. I assume what I need to do is input things into an array in the second stage and then then compare the information in those two arrays, merge them into one, have another array corresponding to the first which tags the data in the first with things like "absent," then output the the new arrays in a master list?
Is this right? I'm having trouble conceptualizing how to make this code work.
Thanks in advance.
Spent some time educating myself and came up with this (query 1):
if ($result->num_rows > 0) {
echo "<table class='table table-hover'>";
echo "<tr><th colspan=2 class='text-center'>固定レッスン</th></tr><tr><th>クラス</th><th>開始時間</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
$dayofclass = $row['day'];
echo "<tr><td>".$row["class_level"]." ".$row["class_title"]."</td><td>".$row['day']." ".$row['class_time']." " .$row['time']. "</td></tr>";
}
$n=date('t',strtotime($date)); // find no of days in this month
$yearmonth = date("Y-m-");
$dates=array();
for ($i=1;$i<=$n;$i++){
if ($i<10) {
$i = "0" . $i;
}
$day=date("l",strtotime($yearmonth.$i)); //find weekdays
if($day==$dayofclass){
$dates[]=$yearmonth.$i;
$datesdata[$yearmonth.$i] = "0";
}
}
echo "</table>";
}
(query 2):
if ($result->num_rows > 0) {
echo "<table class='table table-hover'>";
echo "<tr><th colspan=2 class='text-center'>今月の予定</th></tr><tr><th>クラス</th><th>開始時間</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
$phpdate = strtotime( $row["class_time"] );
$mysqldate = date( 'Y-m-d', $phpdate );
if ($row["furikae"] == 3){
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "1";
} elseif ($row["furikae"] == 8) {
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "3";
} elseif ($row["furikae"] == 2) {
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "2";
}
}
ksort($datesdata);
foreach ($datesdata as $key => $val) {
if ($val == 0){
echo "<tr><td>参加予定</td><td>".$key."</td></tr>";
} elseif ($val == 1) {
echo "<tr class='success'><td>振替参加予定</td><td>".$key."</td></tr>";
} elseif ($val == 2) {
echo "<tr class='danger'><td>休み予定</td><td>".$key."</td></tr>";
} elseif ($val == 3) {
echo "<tr class='warning'><td>キャンセル待ち</td><td>".$key."</td></tr>";
}
}
}
This is probably not the cleanest way to do it, but it works. I put the dates into an array with the key set to the date and the value set to some number referencing attendance (attending, absent, uncertain). Dates from both queries (the regular attendance results and the irregular reservations) are put into the array, then reordered by date using ksort, then output based on the reservation status.
Hi so I am trying to make a function which actually detects the time and depends on date(H); shows what courses and some other info happen right now.
$holidays = false;
//$hour = date('H');
$hour = "11"; //Changed it to test the code
function classes()
{
if ($holdays == false) //Are we on holidays? No
{
if($hour > "9" && $hour < "21") //University working hours? yes
{
if ($hour = "10") //Hour 10? classes below
{
echo "<tr>";
echo '<th class="course-title">...</th>';
echo '<th class="course-category">...</th>';
echo "<th>...</th>";
echo "<th>...</th>";
echo "<th>...</th>";
echo "</tr>";
//Another Class
echo "<tr>";
echo '<th class="course-title">...</th>';
echo '<th class="course-category">...</th>';
echo "<th>...</th>";
echo "<th>...</th>";
echo "<th>...</th>";
echo "</tr>";
//Another Class
echo "<tr>";
echo '<th class="course-title">...</th>';
echo '<th class="course-category">...</th>';
echo "<th>...</th>";
echo "<th>...</th>";
echo "<th>...</th>";
echo "</tr>";
}
else{ //Hour not 10?
echo "No classes right now.";
}
}
else{ //Not working hours?
echo "University closed at this time.</br>";
}
}
else{ //Vacation?
echo "University closed for vacation!";
}
}
So I setted $hour value to 11 so it will pass the first two ifs successfully and I expect that it will echo that there are no classes right now, but it returns that university is closed at this time. I may also mess up the } on the code.
PS: I know i am using to many echos I am not that experienced yet to go with strings
Please take note of your variable scope. To access $holidays inside the function, declare it with global scope.
$holidays = false;
$hour = "11"; //Changed it to test the code
function classes()
{
global $holidays, $hour; // <=== HERE
if ($holidays == false) //Are we on holidays? No
{
// your code here
{
}
In addition, you have other errors that others have pointed out in your code.
if ($hour = "10") should be if ($hour == "10") – Fabio
and
$holidays != $holdays your missing an i – cmorrissey
So, I've been trying to learn PHP. I've got an assignment where I'm supposed to make a list of products with prices, and depending on what day it is, it's supposed to give you a discount. That works so far. However, one thing i'm trying to do is to not echo the result directly in the code; I want to create a function that takes the necessary parameters (Taste, Price, Amount) and returns a formatted string. I haven't been able to do this, despite trying a lot. Here's the code I have. This is the first time i'm using Stack Overflow, so forgive me if i didn't type it in correctly.
<?php
$donuts = array(
array("Strawberry", 10),
array("Chocolate", 20),
array("Vanilla", 30)
);
$weekday = date('N');
$week = date('W');
$hour = date('G');
if ($weekday == 4) {
echo "Hi";
}
?>
<?php
$today = date("l F j, Y");
if ($weekday % 2 == 1 && $week % 2 == 1 && $hour >= 14 && $hour < 17){
echo "Congratulations! You get a 5% discount, and your wares will be delivered tomorrow!";
}
PRINT "$today";
?>
<table border="1">
<tr>
<th>Taste</th>
<th>Price</th>
<th>Discount</th>
<th>Amount</th>
</tr>
<?php
for($i = 0; $i<count($donuts); $i++) {
echo "<tr>";
echo "<td>".$donuts[$i][0]."</td>";
if( $weekday == 1){
echo"<td>".getPricePercent($donuts[$i][1], .5)." kr</td>";
}
if( $weekday == 3){
echo"<td>".getPricePercent($donuts[$i][1], 1.1)." kr</td>";
}
$pris = $donuts[$i][1];
if ($weekday == 5 && $pris > 20){
echo"<td>".($pris-1)." kr</td>";
}
else { echo"<td>".getPrice($donuts[$i][1], .9)." kr</td>";}
?>
<td><input type="number" name="donuts-<?php echo $i; ?>" /></td>
<?php
echo "</tr>";
}
?>
</table>
<?php
function getPrice($price, $percent) {
return ($price);
}
function getPricePercent($price, $percent){
return ($price * $percent);
}
?>
I dont exactly understand what you are trying to print in price and discount section. You can add getDiscount function to make your code clean.
function getDiscount($weekday, $price){
if($weekday == 1){
return .5;
} else if($weekday == 3){
return 1.1;
} else if($weekday == 5 && $price > 20){
return $price-1;
}
}
Then you can do that,
<table border="1">
<tr>
<th>Taste</th>
<th>Price</th>
<th>Discount</th>
<th>Amount</th>
</tr>
<?php
for($i = 0; $i<count($donuts); $i++) {
echo "<tr>";
echo "<td>".$donuts[$i][0]."</td>";
echo"<td>".getPricePercent($donuts[$i][1], getDiscount($weekday,$donuts[$i][1]))." kr</td>";
echo"<td>".getPrice($donuts[$i][1], getDiscount($weekday,$donuts[$i][1]))." kr</td>";
?>
<td><input type="number" name="donuts-<?php echo $i; ?>" /></td>
<?php
echo "</tr>";
}
?>
</table>
Still, i dont understand, are you trying to dynamically update values of price depending on the amount ?
If you want to concat the strings, you can achieve it by using something like this:
echo "Price" . $price . "Amount" . $amount;
Im trying to create a calendar in php and want the selected month to display a picture (e.g. one for january, another for february and so on). I want the pictures to be extracted from a folder called 'months'. How do I do that?
This is my code:
<?php
class Calendar
{
var $events;
function Calendar($date)
{
if(empty($date)) $date = time();
define('NUM_OF_DAYS', date('t',$date));
define('CURRENT_DAY', date('j',$date));
define('CURRENT_MONTH_A', date('F',$date));
define('CURRENT_MONTH_N', date('n',$date));
define('CURRENT_YEAR', date('Y',$date));
define('START_DAY', (int) date('N', mktime(0,0,0,CURRENT_MONTH_N,1, CURRENT_YEAR)) - 1);
define('COLUMNS', 7);
define('PREV_MONTH', $this->prev_month());
define('NEXT_MONTH', $this->next_month());
$this->events = array();
}
function prev_month()
{
return mktime(0,0,0,
(CURRENT_MONTH_N == 1 ? 12 : CURRENT_MONTH_N - 1),
(checkdate((CURRENT_MONTH_N == 1 ? 12 : CURRENT_MONTH_N - 1), CURRENT_DAY, (CURRENT_MONTH_N == 1 ? CURRENT_YEAR - 1 : CURRENT_YEAR)) ? CURRENT_DAY : 1),
(CURRENT_MONTH_N == 1 ? CURRENT_YEAR - 1 : CURRENT_YEAR));
}
function next_month()
{
return mktime(0,0,0,
(CURRENT_MONTH_N == 12 ? 1 : CURRENT_MONTH_N + 1),
(checkdate((CURRENT_MONTH_N == 12 ? 1 : CURRENT_MONTH_N + 1) , CURRENT_DAY ,(CURRENT_MONTH_N == 12 ? CURRENT_YEAR + 1 : CURRENT_YEAR)) ? CURRENT_DAY : 1),
(CURRENT_MONTH_N == 12 ? CURRENT_YEAR + 1 : CURRENT_YEAR));
}
function getEvent($timestamp)
{
$event = NULL;
if(array_key_exists($timestamp, $this->events))
$event = $this->events[$timestamp];
return $event;
}
function addEvent($event, $day = CURRENT_DAY, $month = CURRENT_MONTH_N, $year = CURRENT_YEAR)
{
$timestamp = mktime(0, 0, 0, $month, $day, $year);
if(array_key_exists($timestamp, $this->events))
array_push($this->events[$timestamp], $event);
else
$this->events[$timestamp] = array($event);
}
function makeEvents()
{
if($events = $this->getEvent(mktime(0, 0, 0, CURRENT_MONTH_N, CURRENT_DAY, CURRENT_YEAR)))
foreach($events as $event) echo $event.'<br />';
}
function makeCalendar()
{
echo '<table border="1" cellspacing="4"><tr>';
echo '<td colspan="7" style="text-align:center"> <img src= \"img/months/$month.jpg\" > </td>';
echo '</tr><tr>';
echo '<td width="30"><<</td>';
echo '<td colspan="5" style="text-align:center">'.CURRENT_MONTH_A .' - '. CURRENT_YEAR.'</td>';
echo '<td width="30">>></td>';
echo '</tr><tr>';
echo '<td width="30">Mon</td>';
echo '<td width="30">Tue</td>';
echo '<td width="30">Wed</td>';
echo '<td width="30">Thu</td>';
echo '<td width="30">Fri</td>';
echo '<td width="30">Sat</td>';
echo '<td width="30">Sun</td>';
echo '</tr><tr>';
echo str_repeat('<td> </td>', START_DAY);
$rows = 1;
for($i = 1; $i <= NUM_OF_DAYS; $i++)
{
if($i == CURRENT_DAY)
echo '<td style="background-color: #C0C0C0"><strong>'.$i.'</strong></td>';
else if($event = $this->getEvent(mktime(0, 0, 0, CURRENT_MONTH_N, $i, CURRENT_YEAR)))
echo '<td style="background-color: #99CCFF">'.$i.'</td>';
else
echo '<td>'.$i.'</td>';
if((($i + START_DAY) % COLUMNS) == 0 && $i != NUM_OF_DAYS)
{
echo '</tr><tr>';
$rows++;
}
}
echo str_repeat('<td> </td>', (COLUMNS * $rows) - (NUM_OF_DAYS + START_DAY)).'</tr></table>';
}
}
$epcMonthPic = date("m", mktime(0,0,0,$mo)); //change the "F" for "m" if you want to use numbers
$epcImagePath = "/img/"; // the path to your monthly images (keep the trailing slash)
$epcImageExt = "jpg"; // the extension you'll be using for your images
echo "<img src=\"$epcImagePath$epcMonthPic.$epcImageExt\">";
$month = $_REQUEST["month"];
$cal = new Calendar($_GET['date']);
$cal->makeCalendar();
?>
In the method makeCalendar() fix line:
echo '<td colspan="7" style="text-align:center"> <img src= \"img/months/$month.jpg\" > </td>';
with:
echo '<td colspan="7" style="text-align:center"> <img src="img/months/' . CURRENT_MONTH_N . '.jpg" > </td>';
and make sure that images exists:
img/months/1.jpg
img/months/2.jpg
...
img/months/12.jpg
Trying to create DIV containers out of a mysql results which each hold chunks of 10.
$balloon_count = the amount of records each div should hold.
$ui = the loop counter.
Functionality is simple, don't want to a template engine.
Trying to use the MODULUS operator to simplify the div cuts.
Doesn't work. Any direction is greatly appreciated.
Sample Code
$ui=1;
$balloon_holds = 10;
while($row = mysql_fetch_array($result))
{
if($ui==1||$ui%$balloon_holds != 0)
{
echo '<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>';
echo "<td style=\"font-size:small;vertical-align:text-top;\">";
}
echo '<input disabled type="checkbox" value="$row[id]"'; $this->ischecked($uid,$row[id]); echo "/>".$row['name'].'<br>'."\r\n";
if($ui==10||$ui%$balloon_holds != 0){
echo '</td></tr></table></div>';
}
$ui++;
}
Sample Expected "HTML" Output
<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>
<td style="font-size:small;vertical-align:text-top;">
Record1
Record2
Record3
Record4
Record5
Record6
Record7
Record8
Record9
Record10
</td></tr></table></div>
<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>
<td style="font-size:small;vertical-align:text-top;">
Record11
Record12
Record13
Record14
Record15
Record16
Record17
Record18
Record19
Record20
</td></tr></table></div>
If I understand the question correctly, you want the <div> opener on $ui = 1,11,21,31,... and the </div> closer on 10,20,30,40,...
If this is correct, your modulus operators should be changed as such:
if($ui%$balloon_holds == 1)
{
...
}
...
if($ui%$balloon_holds == 0)
{
...
}
if($ui==1||$ui%$balloon_holds != 0)
This will always be true except for when $ui is a multiple of 10, so you'd be outputting your header/footer blocks for ALL rows except 0, 10, 20, etc... and the one case where $ui is 1.
Most like likely this would be easier to understand:
$row_cnt = 0;
$max_rows = 10;
while($row = ...) {
if ($row_cnt == 0) {
// output header
}
// output row data
if ($row_cnt == 9) {
// output row footer
}
$row_cnt++;
$row_cnt %= $max_rows; // ($row_cnt resets to 0 when it reaches 10)
}
Here is my suggestion, it's also a little more readable / maintainable.
$ui=0;
$balloon_holds = 10;
while ($row = mysql_fetch_array($result))
{
if ($ui % $balloon_holds)
{
if ($ui >= 10)
{
echo '</td></tr></table></div>';
}
echo '<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>';
echo "<td style=\"font-size:small;vertical-align:text-top;\">";
}
echo '<input disabled type="checkbox" value="$row[id]"';
$this->ischecked($uid,$row[id]);
echo "/>".$row['name'].'<br>'."\r\n";
$ui++;
}
if ($ui > 0)
{
echo '</td></tr></table></div>';
}
$ui=0;
$balloon_holds = 10;
while($row = mysql_fetch_array($result))
{
$exit = 0;
if($ui==1||$ui%$balloon_holds != 0)
{
echo '<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>';
echo "<td style=\"font-size:small;vertical-align:text-top;\">";
}
echo '<input disabled type="checkbox" value="$row[id]"'; $this->ischecked($uid,$row[id]); echo "/>".$row['name'].'<br>'."\r\n";
$ui++;
if($ui%$balloon_holds == 0){
echo '</td></tr></table></div>';
$exit = 1;
}
}
if($exit == false){
echo '</td></tr></table></div>';
}