Background
I have a script which is used to rotate employees to share an allotment of car parks. Everything works fine i.e. i am able to retrieve a list of employees from a mysql db into an array, sort alphabetically and fill a table in a rotating fasion. All good.
What I am trying to achieve
What I would like to able to do is if I change the month i.e. from August to September the rotation from the current month will continue from where it left over in the previous month. Right now with the current script it will rotate perfectly within the given month but if i enter a new month the rotation is reset.
I have been thinking long and hard as to how i can carry over the rotation but struggling to figure this out.
Personal thoughts..
One thought maybe is should I flag in the mysqldb the last person in the rotation then in the next month continue the rotation beginning from the person who was flagged?
Here is the current working script
<?php
$weeks = array();
$month = 'Aug';
$year = '2015';
$date = new DateTime("first Monday of $month $year");
$thisMonth = $date->format('m');
while ($date->format('m') === $thisMonth) {
array_push($weeks, $date->format('d-m-Y'));
$date->modify('next Monday');
}
//print_r($weeks);
$weeks_count = count($weeks);
//echo $weeks_count;
//=========================================
include_once 'connect.php';
dbconnect("roster");
$NoOfRows = 8;
$NoOfWeeks = $weeks_count;
$totalLots = $NoOfRows * $NoOfWeeks;
$dataArr = array();
//fetch all persons
$query = "SELECT * FROM carpark ORDER BY First_Name";
$result = mysqli_query($conn, $query) or die("error getting data");
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
array_push($dataArr, $row['First_Name']);
}
$noOfPersons = count($dataArr);
//Create a new array with filling all lots
while ($noOfPersons < $totalLots) {
$dataArr = array_merge($dataArr, $dataArr);
$noOfPersons = count($dataArr);
}
$weekArr = array_chunk($dataArr , $NoOfRows);
//print the table
echo "<table border=1>";
for ($row = 0; $row < $NoOfRows; $row++) {
if ($row == 0) {
echo "<tr><th></th>";
for ($col = 1; $col <= $NoOfWeeks; $col++) {
$weeks_loop = $col-1;
echo "<th>Week $col<br>$weeks[$weeks_loop]</th>";
}
echo "</tr>";
}
for ($col = 0; $col < $NoOfWeeks; $col++) {
if ($col == 0) {
echo "<tr><td><strong>Bay $row</strong></td>";
}
echo "<td>";
echo $weekArr[$col][$row];
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Screenshot of output
What should happen is if i change the next month, the rotation should start with Claire, Duncan, Gabriel and so on..
Related
I have made a code where I get all nubers in the month then I am adding on events from my database. I need the numbers to come from the database in $sqldates I am now only getting array as when I echo the $sqldates. I have 4, 10, 22 and 26 in my database now.
This is what I am getting now look at the picture
enter image description here
This is the result I want look at this picture.
enter image description here
How do I get the results from my database as picture 2 shows? Pleas help how to get the numbers from array.
<table>
<?php
//database connent
include 'con.php';
//get day from event
$sql = "SELECT day, color FROM events";
$result = mysqli_query($con, $sql);
$sqldates = array(array('day', 'color_value'), array('date_value', 'color_value'), array('date_value', color_value));
while($row=mysqli_fetch_array($result)){
array_push($sqldates, $row['day'], $row['color'] );
echo $row['day'];
}
$counter = 0;
//first day
$firstday = '1';
$two = cal_days_in_month(CAL_GREGORIAN, 8, 2018); // 31
//for loop get all day in month
for ($number = $firstday; $number <= $two; $number++)
if (in_array($number , $sqldates)) {
echo "<td width=50 bgcolor='#{$sqldates[$counter][1]}'>$sqldates[$counter][0]</td>";
$counter++;
} else {
echo"<td width=50 bgcolor='#1e8e8e'>$number</td>";
}
?>
</table>
For your updated question this should be good guide:
<table>
<?php
//database connect
include 'con.php';
//get day from event
$sql = "SELECT day, color FROM events";
$result = mysqli_query($con, $sql);
$sqldates = array();
$colors = array();
while($row=mysqli_fetch_array($result)){
array_push($sqldates, $row['day']);
array_push($colors, $row['color']);
}
//first day
$firstday = '1';
$two = cal_days_in_month(CAL_GREGORIAN, 8, 2018); // 31
//for loop get all day in month
$counter = 0;
for ($number = $firstday; $number <= $two; $number++)
if (in_array($number , $sqldates)) {
echo "<td width=50 bgcolor='$colors[$counter]'>$sqldates[$counter]</td>";
$counter++;
} else {
echo"<td width=50 bgcolor='#1e8e8e'>$number</td>";
}
?>
</table>
Note that you need to have defined color for each day, otherwise you will get "Undefined offset" notice.
You are trying to echo an array. Instead you should echo a value from that array with specified index. If I am not wrong, this should work:
//first day
$firstday = '1';
$two = cal_days_in_month(CAL_GREGORIAN, 8, 2018); // 31
//for loop get all day in month
$counter = 0;
for ($number = $firstday; $number <= $two; $number++)
if (in_array($number , $sqldates)) {
echo"<td width=50 bgcolor='#f44242'>$sqldates[$counter]</td>";
$counter++;
} else {
echo"<td width=50 bgcolor='#1e8e8e'>$number</td>";
}
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'm wanting to have a list of states and the cities under it. I want 5 states per row with the cities that have records under it, then I want it to start a new row. I started trying to figure it out and I've been going for hours and my brain hurts. Can anyone help?
Here's what I have right now:
$lookup = "SELECT state, city, count(city) as num FROM needs WHERE country IS NULL AND
status='posted' GROUP BY state, city ORDER BY state, city";
if ($result = mysql_query($lookup)) {
$num_rows = mysql_num_rows($result);
echo "<table>";
$i = 1;
$cols = 3;
$prev = '';
while ($frows = mysql_fetch_array($result)) {
$fcity = $frows['city'];
$fstate = $frows['state'];
$fcitycount = $frows['num']; // num is holding your count by city
if ($fstate != $prev) {
echo "<tr><td><h2>$fstate</h2></td>";
$prev = $fstate;
}
echo "<tr><td><a href='browseresults.php?city=$fcity&state=$fstate'>$fcity, $fstate ($fcitycount)</a></td></tr>";
}
echo "</table>";
}
EDIT:
It should look like this:
Arkansas Tennessee Missouri
Searcy, AR (1) Bartlett, TN (1) St. Louis, MO (4)
Little Rock, AR (4) Memphis, TN (3) Perry, MO (3)
Benton, AR (2) Branson, MO (1)
Shell, MO (2)
Except it will be done 5 times instead of 3
EDIT 2:
Here's the code I tried but it's still not working :-/
$lookup = "SELECT state, city, count(city) as num FROM needs WHERE country IS NULL AND status='posted' GROUP BY state, city ORDER BY state, city";
if ($result = mysql_query($lookup)) {
$num_rows = mysql_num_rows($result);
echo "<table><td>";
$i = 1;
$j = 1;
$cols = 5;
$prev = '';
while ($frows = mysql_fetch_array($result)) {
$fcity = $frows['city'];
$fstate = $frows['state'];
$fcitycount = $frows['num']; // num is holding your count by city
if ($fstate != $prev) {
echo "<tr><h2>$fstate</h2></tr>";
$prev = $fstate;
}
echo "<tr><a href='browseresults.php?city=$fcity&state=$fstate'>$fcity, $fstate ($fcitycount)</a><br></tr>";
if ($fstate != $prev) {
echo "</td><td>";
}
}
echo "</td></table>";
}
Here is one way to do it, based off your current code attempt. The state and all of its cities are within the same cell.
echo "<table>";
$i = 1;
$cols = 3; // of cells per row
$prev = '';
echo "<tr>"; // start the first row
while ($frows = mysql_fetch_array($result)) {
$fcity = $frows['city'];
$fstate = $frows['state'];
$fcitycount = $frows['num']; // num is holding your count by city
if ($fstate != $prev) {
if($i>$cols){ // check if we have reached our # of $cols
echo "</td></tr><tr>"; // end cell/row and start new row
$i=1; //reset counter
}
elseif ($prev != '') { // if not the first cell/state
echo "</td>"; // end cell before starting new cell/state
}
echo "<td><h2>$fstate</h2>";
$prev = $fstate;
$i++;
}
echo "<a href='browseresults.php?city=$fcity&state=$fstate'>$fcity, $fstate ($fcitycount)</a><br />";
}
echo "</td></tr>"; //end the last cell/row
echo "</table>";
By changing $cols value (ie $cols=5, you can change the number of cells per row.
See an example of this in a phpfiddle - http://phpfiddle.org/main/code/pkg-90i
this creates a table like this-
I'm building an interactive timeline that loads in films and calculates the difference between their release dates, thus displaying "days" where there isn't any film release to create a timeline effect. However I have some films that are released on the same date. I don't want to repeat the date for these films over and over again, rather just display the date for the first film in like a group effect.
I have manipulated an if statement that loops through the table and checks whether the date is the same as the previous row's date.
http://ignitethatdesign.com/CheckFilm/test_search.php
However how would I use PHP to only display the date for the first film in a group of films with the same date?
I'm using a complex loop system, here is the full code
<?php
$last_value = null;
echo "<div class=\"timeline\">";
$pNumber = 0;
while ($row = mysql_fetch_assoc($sql)) {
if (!is_null($last_value)) {
$pNumber++;
$a = new DateTime($row['FilmRelease']);
$film_date = $row['FilmRelease'];
$film_name = urldecode($row['FilmName']);
$film_desc = urldecode($row['Synopsis']);
echo "<div id=\"t".$pNumber."\"><p class=\"everyday\">".$film_name."</div>";
if (empty($film_desc)) {
echo "<div id=\"b".$pNumber."\" style=\"font-size:12px;\">No film information for ".$film_name."</div>";
}
else
{
echo "<div id=\"b".$pNumber."\" style=\"font-size:12px;\">".$film_desc."</div>";
}
?> <script type="text/javascript">
$("#b<?php echo $pNumber ?>").hide();
$("#t<?php echo $pNumber ?>").click(function() {
$("#b<?php echo $pNumber ?>").fadeToggle("slow");
});
</script>
<?php
$interval = $a->diff(new DateTime($last_value));
//echo $interval->format('%d days');
$b = new DateTime($last_value);
$i = 0;
}
if ($b == $a)
{
echo "same date as above";
} else
{
echo "This date is different";
}
$howManydays = $interval->days;
for ( $i; $i < $howManydays; $i++) {
echo "<p class=\"day\"></p>";
}
$last_value = $row['FilmRelease'];
}
echo "<div id=\"end\"></end>";
echo "</div>";
?>
Any help would be greatly appreciated :)
Simple example:
$lastDate = null;
while (...) {
$currentDate = date('Y-m-d', strtotime($film['date']));
if ($currentDate != $lastDate) {
echo "<h1>$currentDate</h1>";
}
$lastDate = $currentDate;
// output film details
}
This will give you something like:
2012-04-01
Film A
Film B
2012-04-02
Film C
2012-04-05
Film D
Film E