Upon running the below SQL statement (which works perfectly) I've found that all tables in my database are flushed white.
Any idea as to why this happening?
// check database for necessary updates
$update = mysql_query("SELECT * FROM rent WHERE colour='3C0'");
while($row_update = mysql_fetch_array( $update )) {
$datetime_lower = DateTime::createFromFormat('d/m/Y', $min);
$datetime_upper = DateTime::createFromFormat('d/m/Y', $max);
$datetime_compare = DateTime::createFromFormat('d/m/Y g:i a', $row_update['pDate']);
if ($datetime_lower < $datetime_compare && $datetime_upper > $datetime_compare) {
// date is between do nothing
mysql_close($update);
} else {
// date is not between so update
echo "date is not between";
$update_result = mysql_query("UPDATE rent SET colour='F0F0F0' WHERE substr(pDate, 0, 10) NOT BETWEEN $min AND $max && colour='3C0'");
mysql_close($update_result);
}
}
I've included a few pictures.
This is how it is meant to look like (above code omitted):
http://i51.tinypic.com/143gpef.jpg
This is how it currently looks like (above code present):
http://i54.tinypic.com/2lwm4xg.jpg
Your while loop appears to go through all the results from your table. It appears that on each iteration of the loop, you check the date first in PHP, then you check the date again in your update query, and update all matching rows to F0F0F0.
I don't know why your code is changing the colour to white instead of #F0F0F0, since there is no white or fff in your code, so all I can do is suggest something to make your code more efficient.
Instead of updating all rows on each iteration of the while loop, if you have an id column (Primary Key with Auto-increment) in your rent table, you can use this value in your while loop instead of having to test the date a second time.
$update = mysql_query("SELECT * FROM rent WHERE colour='3C0'");
while($row_update = mysql_fetch_array( $update )) {
$datetime_lower = DateTime::createFromFormat('d/m/Y', $min);
$datetime_upper = DateTime::createFromFormat('d/m/Y', $max);
$datetime_compare = DateTime::createFromFormat('d/m/Y g:i a', $row_update['pDate']);
if ($datetime_lower < $datetime_compare && $datetime_upper > $datetime_compare) {
// date is between do nothing
mysql_close($update);
} else {
// date is not between so update
echo "date is not between";
$update_result = mysql_query("UPDATE rent SET colour='F0F0F0' WHERE id=" . $row_update['id'] . " && colour='3C0'");
mysql_close($update_result);
}
}
Related
I have a small PHP page which takes data from MySQL and displays it via PHP in a monthly calendar. I'm having trouble arranging the data properly within an array to get the desired output.
First, I will describe what I would like to happen:
students come to classes on regular days of the week
they can also make or cancel reservations
the calendar also displays days when the school is not open
In order to display this data on the calendar, I use MySQL to output data from a variety of sources, and then input that into an array with PHP, which I sort by date and output.
My issue is, I would like to be able to handle more than one row of data per day, but because I am using the date as the key, I am limited on only displaying one result per day. If I use a loop to append the date with a counter in the key, I get overlapping results in situations where someone made a reservation and then cancelled that reservation on the same day.
As for my code...
First, I check to see if the student is registered in a weekly class, then input that class into the array.
$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) {
while($row = $result->fetch_assoc()) { // DISPLAY REGULAR CLASS DATA
$dayofclass = $row['day'];
$class_level = $row['class_level'];
$class_title = $row["class_title"];
$day = $row["day"];
$class_time = $row["class_time"];
$time = $row["time"];
// check which dates match the days of the week and store in an array
for ($i=1;$i<=$n;$i++){
if ($i<10) {
$i = "0" . $i;
}
$day=date("l",strtotime($yearmonth.$i)); //find weekdays
if($day==$dayofclass){
$time = date("H:i",strtotime($row['time']));
$dates[]=$yearmonth.$i;
$datesdata[$yearmonth.$i] = "0";
$timedata[$yearmonth.$i] = $time;
$classiddate[$yearmonth.$i] = $row['classID'];
}
}
}
echo "</table>";
$conn->close();
}
After that, I check for specific reservations (cancelations, irregular reservations, waitlists) and input them into the array:
$lowerlimit = $yearmonth . "01";
$upperlimit = $yearmonth . "31";
$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 >= '$lowerlimit'
AND AttendanceDB.class_time <= '$upperlimit'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$loopcount = 0;
// store furikae data in the array
while($row = $result->fetch_assoc()) {
$phpdate = strtotime( $row["class_time"] );
$time = date("H:i",strtotime($row['time']));
$mysqldate = date( 'Y-m-d', $phpdate );
$loopcount++;
$mysqldate = $mysqldate . "+" . $loopcount;
// $loopcount++;
// $mysqldate = $mysqldate . "+" . $loopcount;
$previousdate = $mysqldate;
$previousfurikae = $row['furikae'];
if ($row["furikae"] == 3){
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "1";
$timedata[$mysqldate] = $time;
$classiddate[$mysqldate] = $row['classID'];
} elseif ($row["furikae"] == 8 OR $row["furikae"] == 7) {
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "3";
$timedata[$mysqldate] = $time;
} elseif ($row["furikae"] == 2) {
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "2";
$timedata[$mysqldate] = $time;
}
}
}
$conn->close();
Then finally I check the school calendar and input the days off into the array:
$sql = "SELECT *
FROM SchoolScheduleDB
WHERE date >= '$lowerlimit'
AND date <= '$upperlimit'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// store furikae data in the array
while($row = $result->fetch_assoc()) {
$phpdate = strtotime( $row["date"] );
// $time = date("H:i",strtotime($row['time']));
// $mysqldate = date( 'Y-m-d', $phpdate ) . " " . $time;
$mysqldate = date( 'Y-m-d', $phpdate );
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "666";
}
}
$conn->close();
The way I intended it to work was that:
First the regular classes would be input
Then any reservations would overwrite the original plans
And finally the school calendar would overwrite everything
Currently, this functions as it should, but it is limited to displaying 1 result per day, but I would like to be able to display more than 1 result per day for students who come to multiple classes.
Thank you for your help. If I made any mistakes in my question or my question is unclear I will do my best to revise it.
You can make a Sub-Array for each date by using edged brackets:
$data[20180528][] = 'aa';
$data[20180528][] = 'bb';
$data[20180529][] = 'cc';
$data[20180529][] = 'dd';
$data[20180529][] = 'ee';
will give you an Array like this:
20180528 => aa
=> bb
20180529 => cc
=> dd
=> ee
This is setup to show a button between 2 dates set. And to show a "Sign up starts at xxx" before the set start up date, and "Sign up closed the xxxx" after the set end date...
Somehow nothing shows for the "active periode" / the dates in betweeen...
$DateToday = date('Ymd');
$DateStart = get_field('pamelding_fra');
$DateEnd = get_field('pamelding_slutt');
$DateStartOut = new DateTime($DateStart);
$DateEndOut = new DateTime($DateEnd);
if ($DateStart >= $DateToday){
$ClassStatus = "<div class=\"OpenClassButton\"><span class=\"ClassFullWarning\">Påmeldingen åpner " . $DateStartOut->format('j M Y') . "</span></div>";
$ClassButton = $ClassStatus;
}elseif ($DateEnd <= $DateToday){
$ClassStatus = "<div class=\"OpenClassButton\"><span class=\"ClassFullWarning\">Påmeldingen stengte " . $DateEndOut->format('j M Y') . "</span></div>";
$ClassButton = $ClassStatus;
}elseif ($DateStart <= $DateToday && $DateEnd >= $DateToday){
//Do some stuff - show button, this is the active time.
}
Might not be best-practice and I might make stuff difficult for me, suggestions appriciated.
Don't compare dates using date strings. If they are in different formats (like Ymd, mdY etc) you will get unwanted results. Use unix timestamps instead.
$today = time(); // Get today's timestamp
if ($today < strtotime($DateStart)) {
// Do stuff before
} elseif ($today > strtotime($DateEnd)) {
// Do stuff after
} else {
// Active. No need for any conditions here,
// since we only have three states.
}
I want to make an advertisment manager. When an ad reaches its expiry date it becomes non-active. But when I tried to make it and tried it, all of the ads change into non-active even though they haven't reached their expiry dates.
Here's my code:
$query_banner = mysql_query("SELECT * FROM ad_tbl ORDER BY ID DESC LIMIT $from,$max_show") or die(mysql_error());
while($show=mysql_fetch_array($query_banner))
{
$no++;
if(($no%2)==0)
$color = '#f2f2f2';
else
$color = '#f9f9f9';
$expired_date = $show['expiry_date'];
$today_date = date("m/d/Y");
$expired = strtotime($expiry_date);
$today = strtotime($today_date);
if($expired > $today)
{
$valid = "yes";
}
else
{
$valid = "no";
$query_expired = mysql_query("UPDATE ad_tbl SET status='Non-Active' WHERE expiry_date <= $today") or die(mysql_error());
}
}
Try:
$expiredDateTime = new DateTime($expiry_date);
$expired = $expiredDateTime->format('U');
$today = date('U'); // This will default give you timestamp for "now", saving you a step
If that works then I fear your m/d/y format could be the root of the issue. To check you should echo your strtotime values out and stick them into a converter, see what it says.
hey stackoverflowers. im having a few problems with the code i have written to check my database on refresh if all record dates on the database are between the specified $min and $max values. if not then the record will be updated. this is what i have done so far.
// check database for necessary updates
$update = mysql_query("SELECT * FROM rent");
while($row = mysql_fetch_array( $update )) {
$datetime_lower = DateTime::createFromFormat('d/m/Y', $min);
$datetime_upper = DateTime::createFromFormat('d/m/Y', $max);
$datetime_compare = DateTime::createFromFormat('d/m/Y g:i a', $row_update['pDate']);
$diff_lower = $datetime_lower->diff($datetime_compare);
$diff_upper = $datetime_upper->diff($datetime_compare);
if ($datetime_lower < $datetime_compare && $datetime_upper > $datetime_compare) {
// date is between min and max, do nothing
} else {
// date is not between min and max, update cell colour
$result = mysql_query("UPDATE rent SET colour='F0F0F0' WHERE $datetime_lower < $pDate && $datetime_upper > $pDate") or die(mysql_error());
}
}
the logic behind it seems rather rational but whenever i try to run the code i get the error message:
Warning: DateTime::diff() expects parameter 1 to be DateTime, boolean given in C:\xampp\htdocs\keypad\main.php on line 41
Warning: DateTime::diff() expects parameter 1 to be DateTime, boolean given in C:\xampp\htdocs\keypad\main.php on line 42
any idea whats going on?
fetching into $row but using $row_update
UPDATE rent SET colour='F0F0F0' will update ALL records if one tupel is not between $min and $max
Why not UPDATE rent SET colour='F0F0F0' WHERE pDate NOT BETWEEN $min AND $max MySQL Between
I think your approach is not optimized, instead of compare the different from php->mysql->php, you can issue a query to directly update to mysql, like
update rent set colour='F0F0F0' WHERE pDate>='$min' and pDate<='$max';
I have an events calender that displays events in the order of start date. Everything works great but there is one issue. Events that occur on today's date don't display. I believe that my "if" statement to remove events after they have passed is the issue.
<? while($row = mysql_fetch_assoc($result)) {
$id = $row['id'];
$title = $row['title'];
$text = $row['text'];
$image_url = $row['image_url'];
$start_date = date('F d, Y', strtotime($row['start_date']));
$start_hour = $row['start_hour'];
$start_minute = $row['start_minute'];
$start_am_pm = $row['start_am_pm'];
$end_date = date('F d, Y', strtotime($row['end_date']));
$end_hour = $row['end_hour'];
$end_minute = $row['end_minute'];
$end_am_pm = $row['end_am_pm'];
$tba = $row['tba'];
if(strtotime($row['end_date']) > date('U')) {
?>
First of all, I just want to point out that:
date('U') == time()
So you can use time instead of date.
Now for your problem. If the end_date of your event is set to today, it's probably at the beginning of the day (i.e.: 2010-11-18 00:00:00). That's probably why your conditional does not work, because now is past midnight, the current date/time is greater than the end_date.
Try this:
if (strtotime($row['end_date']) == strtotime('TODAY')) {
// event is today
}
I'm guessing that $row['end_date'] only returns the date not the time. So strtotime($row['end_date']) will = 12:00AM and date('U') will equal return the current time.
So if you ran this at 12:01AM on the date of $row['end_date'], you'd have the comparison of
if("12:00AM today" > "12:01AM today") {
Try
if (strtotime($row['end_date']) >= strtotime(date('Y-m-d'))) {