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>";
}
}
Related
I have a table with reservation information, for example rooms.
The task is, i have to write them out, The problem is in the selection.
If person A booked the second room on 2018-03-12, and person B booked room 16 on the same day, i get two rows intead of just one.
I think this code could be shorter, but this is my logic...
The result of the code:
$todaysDate = date('Ymd');
$oneWeekLaterDate = date('Ymd', strtotime("+1 week"));
for($todaysDate; $todaysDate<$oneWeekLaterDate; $todaysDate++) {
$selectRooms = "SELECT room1, room2, room3, room4, room5, room6, room7, room8, room9, room10, room11,
room12, room13, room14, room15, room16, room17, room18, room19
FROM events
WHERE '$todaysDate'
BETWEEN start and end - interval 1 day";
$result = mysqli_query($connection, $selectRooms);
if (mysqli_num_rows($result)) {
While ($row = mysqli_fetch_array($result)) {
echo "<table align='center' id='table' border='1'><tr ><td colspan='20'>$todaysDate</td></tr><tr>";
for ($i = 1; $i < 20; $i++) {
if ($row['room' . $i] == '1') {
echo "<td style='background-color:red;color:white'>$i</td>";
} else {
echo "<td style='background-color:green;color:white'>$i</td>";
}
}
echo "</tr></table>";
}
}
}
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.
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 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.
I need help rendering data in PHP:
I want to use the results of a SQL query to populate a table, displaying images in a 4-column grid. Here's what I've written so far:
$result = mysql_query("SELECT * FROM gallery ORDER BY id ASC ");
while($row = mysql_fetch_array($result))
{
echo "<td align=\"center\" ><img src=\"upload_gallery/thumbnail/sml_".$row['image_name']."\" width=\"200\" height=\"170\" /></td>";
}
<table><tr>
<?$result = mysql_query("SELECT * FROM gallery ORDER BY id ASC ");
$result =mysql_fetch_array($result)
$count=0;
for($row =0; $row<count($result); $row++)
{
$count++
if($count==4){
echo '</tr><tr>';
$count=0;
}
echo "<td align=\"center\" ><img src=\"upload_gallery/thumbnail/sml_".$row['image_name']."\" width=\"200\" height=\"170\" /></td>";
}
?>
</tr>
</table>
You can do a for loop as was suggested by Frederick:
$num_rows = mysql_num_rows($result);
for($i = 0; $i < $num_rows; $i++)
{
// Check if beginning of row
if($i % 4 == 0)
{
//If not the first row, end the last row first
if($i > 0)
{
echo "</tr>";
}
echo "<tr>";
}
$row = mysql_fetch_assoc($result);
//Output each individual image
echo "<td> {*EACH IMAGE code*}</td>";
}
That should give you the idea.
Try this one:
$arr =array("Test1","Test2","Test3","Test4","Test5","Test6","Test7","Test8","Test9","Test10");
echo "<table><tr>";
for($i = 1 ; $i <= count($arr) ; $i++)
{
echo"<td>".$arr[$i-1]."</td>";
$last = $i % 4 == 0? "<tr/><tr>":"";
echo $last;
}
echo "</table>";
Try this, hope it works for you,
while($row = mysql_fetch_array($result))
{
$data[]= $row['image_name'];## Pass the image data to new array
}
$ImagePerRow = 4;### Set the value of image per table row
echo "<table border='1'><tr>";
foreach($data as $key => $ImageName)
{
if(($key+1) == $ImagePerRow)## if key index+1 is equal to ImagePerRow
{
echo "<td align=\"center\" ><img src=\"upload_gallery/thumbnail/sml_".$ImageName."\" width=\"200\" height=\"170\" /></td></tr><tr>";## then close the table row and start a new table row
$ImagePerRow+=4;### Add a value of 4 in order to increment the imageperRow value on the next loop
}else{
echo "<td align=\"center\" >".$ImageName."</td>";### else echo the normal table data
}
}
echo "</tr></table>";