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
Related
I'm sorry for the terribly long question. I just want to make sure that this problem is fully addressed:
so through an html form, I asked user to give four information five times: deliveryby, itemNo, inputTime, inputTime2.
Each of this information is stored inside a session array.
All of this 5 items will be displayed and for each of the item, user can either click on 'retrieve' button or 'delete' button.
so for each of these 5 items, I need a function that can track the number of days between each inputTime2 and inputTime.
if the days is more than 2 days, 'delete' button can be clicked and the item will be cleared from session.
but if it's less than 2 days, 'retrieve' button can be clicked and the item will also be cleared from session.
Both retrieve and delete buttons will delete the items from session.
the only difference is the day's duration that both buttons need to scan.
Box-function.php : where the date function is. this page is attached to the 'delete' button. I'm not sure whether I'm doing it right.
<?php
session_start();
$x = $_GET['id'];
//echo $x;
$date1 = array();
$date2 = array();
$datediff = array();
$days = array();
//to assign the session date array and find out how many days are between the two dates
for($x=0; $x<5; $x++){
$date1 = [strtotime($_SESSION['InputTime'][$x])][$x];
$date2 = [strtotime($_SESSION['InputTime2'][$x])][$x];
$datediff = [$date2 - $date1][$x];
$days = [round($datediff / (60*60*24))][$x];
}
for($x=0; $x<5; $x++){
if ($days[$x] == 2 && $days[$x] > 2){
//if( date("Y-m-d", strtotime("+2 day", strtotime($_SESSION['InputTime'][$x])))){
$_SESSION['DeliveryBy'][$x] = '';
$_SESSION['ItemNo'][$x] = '';
$_SESSION['InputTime'][$x] = '';
header('Location: User-display1.php');
}
}
?>>
User-display1.php:
for($x=0; $x<5; $x++) {
if($_SESSION['DeliveryBy'][$x]!=""){
echo "<tr>";
echo "<td>"; echo $x+1; echo "</td>";
echo "<td>"; echo $_SESSION['DeliveryBy'][$x]; echo "</td>";
echo "<td>"; echo $_SESSION['InputTime'][$x]; echo "</td>";
echo "<td>"; ?> retrieve</button>
<?php echo "</td>";
echo "<td>"; ?> <button type="button" class="btn btn-danger">Delete</button> <?php echo "</td>";
echo "</tr>";
}
else{
echo "<tr>";
echo "<td>"; echo $x+1; echo "</td>";
echo "<td>"; echo ' '; echo "</td>";
echo "<td>"; echo ' '; echo "</td>";
echo "<td>"; ?> retrieve</button>
<?php echo "</td>";
echo "<td>"; ?> <button type="button" class="btn btn-danger">Delete</button> <?php echo "</td>";
echo "</tr>";
}
}
clear-session.php : this page is supposed to clear session variables for items with days difference of less than 2 days but I'm not sure how to do it since I'm still having trouble with the Box-function.php page.
<?php
session_start();
$x = $_GET['id'];
//echo $x;
$_SESSION['DeliveryBy'][$x] = '';
$_SESSION['ItemNo'][$x] = '';
$_SESSION['InputTime'][$x] = '';
header('Location: User-display1.php');
?>
the code shows no error. but I cannot click on the delete button. there were no changes when the button was clicked. I think there might be some mistakes in the logic of this code. Can anyone advise me on what I should do?
Well, you didn't really specify what format the dates are in. But I do see this:
strtotime($_SESSION['InputTime'][$x])
So I assume its at least something that PHP understands. Knowing that, you can use something like this:
<?php
$old_o = new DateTime('2019-12-31');
$new_o = new DateTime;
echo $new_o->diff($old_o)->days, "\n";
https://php.net/class.dateinterval
I try to make a table where some cells shall have info according to an data base. If I do like this it works:
$date->modify('-1 day');
for ($x = 1; $x <=7; $x++) {
$date->modify('+1 day');
$b = true;
echo "<tr>", PHP_EOL;
echo "<td id='dag".$x."0' class='dag'>v".$date->format('W-D j/n')."</td>", PHP_EOL;
//*********** to function
foreach($t_tider as $field){
if ($field['datum'] == $date->format('Y-m-d') && $field['slot'] == 1){
echo "<td id='dag".$x."1'><div class='bokad'>".$field['lgh_nr']."-".$field['last_name']."</div></td>", PHP_EOL;
$b = false;
}
}
//***********
if($b) {
echo "<td id='dag".$x."1'>Ledig</td>", PHP_EOL;
}
// and so on, 7 rows and 5 columns and a header row
// but if I try to make a function of it it don’t recognize the array, only the first post are there.
function checkBokn($st, $tid, $d, $i){
foreach($st as $field){
if ($d->format('Y-m-d') == $field['datum'] && $field['slot'] == $tid){
echo "<td id='dag".$i.$tid."'><div class='bokad'>".$field['lgh_nr']."-".$field['last_name']."</div></td>", PHP_EOL;
return $bol = false;
}
// solved
/* else {
return $bol = true;
} */
}
return $bol = true; //moved
}
$b = checkBokn($t_tider, 2, $date, $x);
the $t_tider are an mysqli query.
BTW
Are there some way to add and subtract dates in strftime(), like on $date->modify('+1 day'); or make $date show days in another language than English?
Well I "solved" it, the else return breaks the loop, feeling stupid. Had to be after the loop.
I am having problems printing out a timetable using the results from a SQL statement and some HTML layout with PHP.
I have the times of the classes along the top of the page.
I am trying to put the days of the week along the side of the page and then check if the result of the SQL statement (containing a module) should be printed in the specific day and time.
//Print out the top of the array to display the times
echo "<div><table class='table table-bordered'><thead><tr><th></th><th>9-10</th><th>10-11</th><th>11-12</th><th>12-13</th><th>13-14</th><th>14-15</th><th>15-16</th><th>16-17</th></tr></thead><tbody>'";
//Now loop through the result of the SQL statement that contains the modules associated with the selected course
while($row = mysqli_fetch_array($result1)) {
for($d = 1; $d < 6; $d++){
$printday = $days[$d];
echo "$printday";
for($t = 9; $t < 17; $t++) {
if($row['Day'] == $d && $row['Time'] == $t){ //fix this area so that it moves along
echo "<td>" . $row['ModuleName'] . "<br/>\n " .$row['Location'] . "</td>";
} //if
else {
echo "<td></td>";
} //else
} //for 2
echo "</tr>";
} //for 1
} //while
The problem is that I am printing out Monday-Friday 3 times as there are 3 $row results. Any idea how I could get this to work.
Your looping through your data in the wrong spot, you first need to put that in an array so you can loop through it for each day/time.
//Print out the top of the array to display the times
echo "<div><table class='table table-bordered'><thead><tr><th>Day</th><th>9-10</th><th>10-11</th><th>11-12</th><th>12-13</th><th>13-14</th><th>14-15</th><th>15-16</th><th>16-17</th></tr></thead><tbody>'";
//Now loop through the result of the SQL statement that contains the modules associated with the selected course
$courses = array();
while($row = mysqli_fetch_array($result1)) {
$courses[] = $row;
} //while
for($d = 1; $d < 6; $d++){
$printday = $days[$d];
echo "<tr><td>" . $printday . "</td>";
for($t = 9; $t < 17; $t++) {
unset($course_row);
foreach($courses as $course){
if($course['Day'] == $d && $course['Time'] == $t){ //fix this area so that it moves along
$course_row .= $course['ModuleName'] . "<br/>\n " .$course['Location'] . "<br/>\n<br/>\n";
} //if
}
if(isset($course_row)){
echo "<td>" . $course_row . "</td>";
} else {
echo "<td> </td>";
} //else
} //for 2
echo "</tr>";
} //for 1
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">'; ?>
So basically I have a script that is suppose to display my code into rows of 3, and create a new row. However, it is all displayed in one column. Here's the code.
while($mysql2 = mysql_fetch_array($sql2)) {
$prodlogo = $mysql2['logo'];
$prodid = $mysql2['id'];
$prodname = $mysql2['name'];
$x = 1;
if($x==1) {
echo "<tr>";
}
echo "<td><img src=images/productpics/$prodlogo></br><a href=viewproduct.php?pid=$prodid>$prodname</a></td>";
$x++;
if($x==3) {
echo "</tr>";
$x = 1;
}
}
move the $x out of the loop,otherwise you are reset it to one in every loop
each <td> will be a separate column. I would suggest you format it thus.
while($mysql2 = mysql_fetch_assoc($sql2)) {
foreach($mysql2 as $row) {
echo "<tr>";
echo "<td>".$row['logo']."</td><td>$".row['id']."</td><td>".$row['name']."</td>";
echo "</tr>";
}
}
also you need to use mysql_fetch_assoc() to get an associative array you can access like $row['key']
You may need to instantiate $x = 1 outside of your while loop - you're resetting it to 1 each time you're running it.
Using modulo might be good too:
if (($x % 3) == 0) {
echo "<tr>"
}
echo "<td><img src=images/productpics/$prodlogo></br><a href=viewproduct.php?pid=$prodid>$prodname</a></td>";
if (($x % 3) == 0) {
echo "</tr>"
}
You wouldn't need to worry about resetting $x to 1...