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
Related
How do I solve this problem to print out matrix which highlights the first row as image
Using for loop PHP to print matrix 10*10 in table form. Note the first column will color with pink
as image
example
Try this code
<?php
echo "<table>";
for($x=1;$x<=10;$x++){
echo "<tr>";
for($y=1;$y<=10;$y++){
echo "<td>".$x*$y."</td>";
}
echo "</tr>";
}
echo "</table>";
To achive this we will check if its first row and the first column in nested for loop. Just see the whole there is not much to explain here.
WHOLE CODE
echo "<table>";
for($x=1;$x<=10;$x++){
echo "<tr>";
if($x == 1){
echo "<td></td>";
for($i = 1; $i <= 10; $i++){
echo "<td style='background-color:blue;'>".$i ."</td>";
}
echo "</tr><tr>";
}
for($y=1;$y<=10;$y++){
if($y == 1){
echo "<td style='background-color:blue;'>".$x ."</td>";
}
echo "<td>".$x*$y."</td>";
}
echo "</tr>";
}
echo "</table>";
OUTPUT
You can just edit the design. since you didn't post your css and html code.
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
I have a scenario whereby I need to see if a date falls between a start date and an end date, then every table cell between those dates I need to fill with a word like continue (See picture below for example).
Pulling the data from the database and then comparing the start dates and end dates against an of array dates I am able to populate a table like the above image. The complications begin to arise when I try to add information to the cells between looping.
I used:
if(date("Y-m-d", strtotime($test->range[$i])) > $data['sData'] && date("Y-m-d", strtotime($test->range[$i])) > $data['eData']){
echo "<td class='col-md-6'>Continue</td>";
}
However, this didn't provide the output I was after.
My current code is:
$all = $db->prepare("SELECT * FROM `assignment` WHERE user_id = $user ORDER BY `sDate` ASC");
$all->execute();
$rows = $all->fetchAll(PDO::FETCH_ASSOC);
echo "<table class='table table-hover'><tr><td></td>";
for($i = 0; $i < count($test->range); $i++){
echo "<td class='col-md-6'>" . date("d-m-Y", strtotime($test->range[$i])) . "</td>";
}
echo "</tr>";
foreach($rows as $data){
echo "<tr><td>" . $data['name'] . "</td>";
for($i = 0; $i < count($test->range); $i++){
if($data['sDate'] === date("Y-m-d", strtotime($test->range[$i]))){
echo "<td class='col-md-6'>Start</td>";
}
if(date("Y-m-d", strtotime($test->range[$i])) > $data['sData'] && date("Y-m-d", strtotime($test->range[$i])) > $data['eData']){
echo "<td class='col-md-6'>Continue</td>";
}
if($data['eDate'] === date("Y-m-d", strtotime($test->range[$i]))){
echo "<td class='col-md-6'>End</td>";
}
echo "<td class='col-md-6'></td>";
}
}
echo "</table>"
How can I fill the table cells between start and end with the word continue?
Just use a helper variable?
foreach($rows as $data){
echo "<tr><td>" . $data['name'] . "</td>";
$started = false;
for($i = 0; $i < count($test->range); $i++){
if($data['sDate'] === date("Y-m-d", strtotime($test->range[$i]))){
echo "<td class='col-md-6'>Start</td>";
$started = true;
}
if($data['eDate'] === date("Y-m-d", strtotime($test->range[$i]))){
echo "<td class='col-md-6'>End</td>";
$started = false;
}
if($started){
echo "<td class='col-md-6'>Continue</td>";
}
echo "<td class='col-md-6'></td>";
}
}
I am using the following code to get results and want to have it displayed in two rows horizontially.
echo "<table border='0' width='700px' align='center'>";
echo "<tr>";
while($number = mysqli_fetch_array($result2))
{
echo "<td class='ball_p'>" . $number['number'] . "</td>";
**echo "</tr><tr>";**
echo "<td class='search'>" . $number['count'] . "</td>";
}
echo "<tr>";
echo "</table>";
Hence I need this part of the code not to be included in the loop echo "";
Please help, this must be really simple.
I want the results to be displayed 10 across as I limit my query to
the top 10.
So it would be:
Number Number Number etc. Count Count Count etc.
Because the tables need to be drawn one row at a time, you could store all of your number values in one array and all of your count values in a second array, then loop through each array as you're building your rows. So first get your values from the database:
// let's get a variable with the total number of records, i.e. horizontal cells:
$totalRecords = 0;
// get values from db:
while($number = mysqli_fetch_array($result2))
{
$numberArr[] = $number['number'];
$countArr[] = $number['count'];
$totalRecords++;
}
Then in another set of loops render the table (one loop for each row):
// first row
echo "<tr>";
for ($i = 0; $i < $totalRecords; ++$i) // you could also use count($numberArr) instead of $totalRecords
{
echo "<td class='ball_p'>" . $numberArr[$i] . "</td>";
}
echo "</tr>";
// second row
echo "<tr>";
for ($i = 0; $i < $totalRecords; ++$i)
{
echo "<td class='ball_p'>" . $countArr[$i] . "</td>";
}
echo "</tr>";
What about this:
$i = 0;
while($number = mysqli_fetch_array($result2))
{
echo "<td class='ball_p'>" . $number['number'] . "</td>";
while($i++ >= 1)
{
echo "</tr><tr>";
$i = 0;
}
echo "<td class='search'>" . $number['count'] . "</td>";
}
I haven't tested it but the ++ after the $i in the second while loop should increment it after the evaluation which should skip the </tr><tr> the first time and print it the second.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am making a loop that displays a table with 12 columns. It starts with the number 1912 and ends with 2013. The problem is, when it loops to 1920, it has no remainder and starts a new row. I need to get the code to make a new row after the 12th column.
This is the result I'm getting:
And here is what I have so far:
<?php
$columns = 12;
$Year = 1912;
echo "<table width="755" border="1">";
echo "<tr>";
while ($Year <= 2013) {
if (!($Year % $columns)) {
echo "</tr><tr>";
}
echo "<td>$Year</td>";
++$Year;
}
echo "</tr>";
echo "</table>";
?>
<?php
$columns = 12;
$Year = 1912;
$i=1;
echo "<table width=\"755\" border=\"1\">";
echo "<tr>";
while ($Year <= 2013)
{
if ($i==13)
{
echo "</tr><tr>";
$i=1;
}
echo "<td>$Year</td>";
$Year++;
$i++;
}
echo "</tr>";
echo "</table>";
?>
SOLUTION 1
You need to append and prepend columns for the years where this matches, like so:
And here's the code for doing so:
<?php
$columns = 12;
$startingYear = 1912;
$endingYear = 2013;
$realStartingYear = $startingYear;
$realEndingYear = $endingYear;
//Find the real starting year by going back a year until we hit the right one
while ($realStartingYear % $columns) {
$realStartingYear--;
}
//Find the real ending year by going forward a year until we hit the right one
while ($realEndingYear % $columns) {
$realEndingYear++;
}
echo '<table width="755" border="1">';
echo "<tr>";
for ($year = $realStartingYear; $year < $realEndingYear; $year++) {
if (!($year % $columns)) {
echo "</tr><tr>";
}
echo "<td>" . ($year >= $startingYear && $year <= $endingYear ? $year : "") . "</td>";
}
echo "</tr>";
echo "</table>";
?>
If you want to show the other columns as well, just change
echo "<td>" . ($year >= $startingYear && $year <= $endingYear ? $year : "") . "</td>";
to
echo "<td>" . $year . "</td>";
SOLUTION 2
You want to start the table with 1912, like so:
The code will be a lot simpler:
<?php
$columns = 12;
$startingYear = 1912;
$endingYear = 2013;
echo '<table width="755" border="1">';
for ($i = $startingYear; $i <= $endingYear; $i += $columns) {
echo "<tr>";
for ($j = 0; $j < $columns; $j++) {
echo "<td>" . ($i + $j) . "</td>";
}
echo "</tr>";
}
echo "</tr>";
echo "</table>";
?>
You want to wrap every 12 columns, just keep track of the current column and use that to determine when you should end the row (e.g. every 12th column) --
$col = 1;
while ($Year <= 2013 && $col++) {
if (!($col % $columns)) {
//...
Using $Year to determine when to wrap just complicates things.