How to populate table from SQL data using PHP - php

I have a table with headers; Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday. I have gathered user "shifts" from a database...
<tr>
<th>Sunday</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
</tr>
<?php $sql4 = "SELECT * FROM shifts WHERE date = '" . $sunday . "' or date = '" . $monday . "'
or date = '" . $tuesday . "' or date = '" . $wednesday . "'
or date = '" . $thursday . "' or date = '" . $friday . "'
or date = '" . $saturday . "'";
$result1 = $database->query($sql4);
echo"<tr>";
while ($row4 = mysqli_fetch_array($result1)){
echo "<td>";
echo $row4['name'];
echo "</td>";
}
echo "</tr>"
So there can be any amount of shifts per day, but dependant on what day the shift is on i need to place the shift under the correct table heading.
The shifts table has: shift_id, name, date, day
$resultsArray = array();
while ($row4 = mysqli_fetch_array($result1)){
$weekDay = $row4['day'];
$resultsArray[$weekDay][] = $row4['name'];
}
for ($x = 0; $x <= count($resultsArray); $x++) {
$sundayData = (!empty($resultsArray['Sunday'][$x]) ) ? $resultsArray['Sunday'][$x] : "";
$mondayData = (!empty($resultsArray['Monday'][$x]) ) ? $resultsArray['Monday'][$x] : "";
$tuesdayData = (!empty($resultsArray['Tuesday'][$x]) ) ? $resultsArray['Tuesday'][$x] : "";
$wednesdayData = (!empty($resultsArray['Wednesday'][$x]) ) ? $resultsArray['Wednesday'][$x] : "";
$thursdayData = (!empty($resultsArray['Thursday'][$x]) ) ? $resultsArray['Thursday'][$x] : "";
$fridayData = (!empty($resultsArray['Friday'][$x]) ) ? $resultsArray['Friday'][$x] : "";
$saturdayData = (!empty($resultsArray['Saturday'][$x]) ) ? $resultsArray['Saturday'][$x] : "";
echo "<tr>";
echo "<td>".$sundayData."</td>";
echo "<td>".$mondayData."</td>";
echo "<td>".$tuesdayData."</td>";
echo "<td>".$wednesdayData."</td>";
echo "<td>".$thursdayData."</td>";
echo "<td>".$fridayData."</td>";
echo "<td>".$saturdayData."</td>";
echo "</tr>";
}
?>

It would be easier to answer your question if I could see the exact structure of your data, but I'll give this a whirl anyway and hopefully you can see where i'm going with it.
What I would do is create an array and fill in the information as you go through the results. Once you have the new resulting array, build your table.
$resultsArray = array();
while ($row4 = mysqli_fetch_array($result1)){
$weekDay = $row4['day'];
$resultsArray[$weekDay][] = $row4['shift'];
}
Now you have an array that contains all the shifts organized by day. Looping through this array will allow you to create your table the way you want.
This is untested so may have some mistakes in it, but here is an example...
for ($x = 0; $x <= 10; $x++) {
echo "<tr>";
echo "<td>".$resultsArray['Sunday'][$x]."</td>";
echo "<td>".$resultsArray['Monday'][$x]."</td>";
echo "<td>".$resultsArray['Tuesday'][$x]."</td>";
echo "<td>".$resultsArray['Wednesday'][$x]."</td>";
echo "<td>".$resultsArray['Thursday'][$x]."</td>";
echo "<td>".$resultsArray['Friday'][$x]."</td>";
echo "<td>".$resultsArray['Saturday'][$x]."</td>";
echo "</tr>";
}
Note : Keep in mind you'll have to change this portion $x <= 10 to fit your actual array size.

Related

Get missing date values from array in php dynamically

We are creating a system where employees need to update their daily status report. The fundamental purpose of the system is to note the missing dates on which they did not update the status report.
I have found a way to do that by checking the day difference between the 2 array values and then counting & displaying the days. However, I am not sure how to do this dynamically between the 2 array values.
Here's the code I have used along with the output:
//id of the person updating the DSR
$userid = $_id;
// Array to fetch all the DSR by specific user
$fine = getDSRbyUserIdOrderDate($userid);
$today = date('d-m-Y');
$tomorrow = date("d-m-Y", strtotime($today . " +1 day"));
$ok = count($fine) - 1;
//Array to get dates
$d1 = $fine[0]['date'];
$d2 = $fine[1]['date'];
$d3 = $fine[2]['date'];
// Function call to find date difference
$dateDiff = dateDiffInDays($d1, $d2);
$dateDiff1 = dateDiffInDays($d2, $d3);
echo "<h4 style='color:red'>You have missed the DSR on the following dates.</h4>";
for($p = 1; $p < $dateDiff; $p++){
$missingdate = date("d-m-Y", strtotime($d1 . " +$p day"));
echo "<span style='color:red'>$missingdate</span>";
echo "<br />";
}
for($p = 1; $p < $dateDiff1; $p++){
$missingdate = date("d-m-Y", strtotime($d2 . " +$p day"));
echo "<span style='color:red'>$missingdate</span>";
echo "<br />";
}
if($d2 != $today){
echo "<span style='color:red'>$today <i>- Kindly update DSR before midnight</i></span> ";
echo "<br />";
}
Output:
I would create first a list of entries by date and then "paint" it accordingly.
$starting_date = new DateTime('2019-11-23');
$num_days = 10
$from_db_by_date = [];
foreach ($fine as $entry) {
$from_db_by_date[$entry['date']] = $entry;
}
$all_by_date = [];
for ($i = 0; $i < $num_days; $i++) {
$date_str = $starting_date->format('d-m-Y');
$all_by_date[$date_str] = $from_db_by_date[$date_str] ?: null;
$starting_date->modify('+1 day');
}
Now you can loop through $all_by_date, check if the entry is null and act accordingly.

MySQL - PHP variable with date in query does not return any result

Hi I am trying to write a home budget forecast. I got to the point where I'd like to echo out table with dates from today up to certain date (next 2 months for example) and total value for this date.
I put some example data in database already but for some reason none of the records are being returned and all the values are 0 for each date in the table. I did not have a problem with creating same table for "Last Transactions" or "Upcoming Transactions".. Seems that here problem lays in query itself as num_rows is alwasys 0.
$currentDate = date('Y-m-d', time());
$endDate = "2019-09-30";
$content = '';
$dayTotal = 0;
while ($currentDate < $endDate) {
$getForecast_sql = "SELECT value FROM log WHERE date=".$currentDate."";
$getForecast_result = $conn->query($getForecast_sql);
if ($getForecast_result === false) {
echo "Query failed: " .$conn->error;
return false;
} else {
if ($getForecast_result->num_rows > 0) {
while($row = $getForecast_result->fetch_assoc()) {
$value = $row['value'];
$dayTotal += $value;
}
}
$content .= '<tr>
<td class="log-date">' . $currentDate . '</td>
<td class="log-desc">' . $dayTotal . '</td>
</tr>';
}
$currentDate = date('Y-m-d', strtotime($currentDate . '+1 day'));
}
echo $content;

How to add row values together in a html table (calendar)?

I have a calendar I made with php using an html table.
Each date has an integer value inserted from the database below the day number.
What I want to do is get the sum of each row (week) of the table and put it in the 8th column. How can I do this?
Any help?
<?php
$conn = mysqli_connect('localhost','username','password','database_name');
?>
<html>
<head>
<title>
Calendar
</title>
<link rel="stylesheet" href="/test/style.css">
<script src="/test/script.js"></script>
</head>
<body>
<?php
//This gets today's date
$date = time() ;
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date);
$year = date('Y', $date);
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
//This gets us the month name
$title = date('F', $first_day) ;
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ;
//Here we start building the table heads
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td>
<td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>Total:</td></tr>";
$day_count = 1;
$row_number = 1;
echo "<tr id='row" . $row_number . "'>";
$row_number++;
//first we take care of those blank days
/////////get beginning of month
if($month-1 != 0) {
$last_month = $month-1;
}
else {
$last_month = 12;
}
if($last_month == '12') {
$year = $year-1;
}
$last_month_first_day = mktime(0,0,0,$last_month, 1, $year);
$last_month_days_in_month = cal_days_in_month(0, $last_month, $year);
$last_month_day_of_week = date('D', $last_mont_days_in_month);
$last_month_days_to_add_to_last_month_end = $blank;
$last_month_end = $last_month_days_in_month-$last_month_days_to_add_to_last_month_end;
//end ^^
while ( $blank > 0) {
echo "<td><span style='color:grey'>" . $last_month_end . "</span></td>";
$last_month_end++;
$blank = $blank-1;
$day_count++;
}
//sets the first day of the month to 1
$day_num = 1;
//count up the days, untill we've done all of them in the month
$week_total_mileage = array();
$x = 0;
while ( $day_num <= $days_in_month ) {
//get total miles from database
$getDay = $year . "-" . $month . "-" . $day_num;
$query = "SELECT * FROM table WHERE date='" . $getDay . "'";
$doQuery = mysqli_query($conn,$query);
while($rows = mysqli_fetch_assoc($doQuery)) {
$total_miles = $rows['total_miles'];
}
$num_rows = mysqli_num_rows($doQuery);
echo "<td id='" . $day_count . $row_number . "' value='" . $total_miles . "'
>
<form method='post' action='/test/day.php'>
<input type='hidden' value='" . $day_num . "' name='day'>
<input type='hidden' value='" . $title . "' name='month'>
<input type='hidden' value='" . $year . "' name='year'>
<input type='button' id='dayNum' value='" . $day_num . "'>
</form>
<span id='totalMiless'>Total miles: ";
if($num_rows == 1) {
echo $total_miles;
}
else {
echo '-';
}
echo "</span>
</td>
<div class='hiddenDay' id='" . $day_num . $title . $year . "' style='display:none'>
<span id='totalMiles'>Total miles: ";
if($num_rows == 1) {
echo $total_miles;
}
else {
echo '0';
}
echo "</span></div>";
$week_total_mileage[$day_num] = $total_miles;
$day_num++;
$day_count++;
if ($day_count > 7) {
$total_total = 0;
while($x < 8) {
$total_total = $total_total + $week_total_mileage[$x];
$x++;
}
while($x < 14 && $x > 8) {
$total_total = $total_total + $week_total_mileage[$x];
}
echo "<td>" . $total_total .
"</td></tr><tr id='row" . $row_number . "'>";
empty($week_total_mileage);
$day_count = 1;
$row_number++;
}
}
//Finaly we finish out the table with some blank details if needed
$end_days = 1;
while ( $day_count >1 && $day_count <=7 ) {
echo "<td><span id='endDays'>" . $end_days . "</span></td>";
$day_count++;
$end_days++;
}
echo "</tr></table>";
?>
</body>
</html>
The above code outputs this:
I just went through your script, put all total miles into an array
and calculate sum with
array_sum
Try this code:
<?php
$conn = mysqli_connect('localhost','username','password','database_name');
?>
<html>
<head>
<title>
Calendar
</title>
<link rel="stylesheet" href="/test/style.css">
<script src="/test/script.js"></script>
</head>
<body>
<?php
//This gets today's date
$date = time() ;
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date);
$year = date('Y', $date);
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
//This gets us the month name
$title = date('F', $first_day) ;
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ;
//Here we start building the table heads
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td>
<td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>Total:</td></tr>";
$day_count = 1;
$row_number = 1;
echo "<tr id='row" . $row_number . "'>";
$row_number++;
//first we take care of those blank days
/////////get beginning of month
if($month-1 != 0) {
$last_month = $month-1;
}
else {
$last_month = 12;
}
if($last_month == '12') {
$year = $year-1;
}
$last_month_first_day = mktime(0,0,0,$last_month, 1, $year);
$last_month_days_in_month = cal_days_in_month(0, $last_month, $year);
$last_month_day_of_week = date('D', $last_mont_days_in_month);
$last_month_days_to_add_to_last_month_end = $blank;
$last_month_end = $last_month_days_in_month-$last_month_days_to_add_to_last_month_end;
//end ^^
while ( $blank > 0) {
echo "<td><span style='color:grey'>" . $last_month_end . "</span></td>";
$last_month_end++;
$blank = $blank-1;
$day_count++;
}
//sets the first day of the month to 1
$day_num = 1;
//count up the days, untill we've done all of them in the month
$week_total_mileage = array();
$weekly_total = array();
$x = 0;
while ( $day_num <= $days_in_month ) {
//get total miles from database
$getDay = $year . "-" . $month . "-" . $day_num;
$query = "SELECT * FROM table WHERE date='" . $getDay . "'";
$doQuery = mysqli_query($conn,$query);
while($rows = mysqli_fetch_assoc($doQuery)) {
$total_miles = $rows['total_miles'];
}
$num_rows = mysqli_num_rows($doQuery);
echo "<td id='" . $day_count . $row_number . "' value='" . $total_miles . "'
>
<form method='post' action='/test/day.php'>
<input type='hidden' value='" . $day_num . "' name='day'>
<input type='hidden' value='" . $title . "' name='month'>
<input type='hidden' value='" . $year . "' name='year'>
<input type='button' id='dayNum' value='" . $day_num . "'>
</form>
<span id='totalMiless'>Total miles: ";
if($num_rows == 1) {
echo $total_miles;
}
else {
echo '-';
}
echo "</span>
</td>
<div class='hiddenDay' id='" . $day_num . $title . $year . "' style='display:none'>
<span id='totalMiles'>Total miles: ";
if($num_rows == 1) {
echo $total_miles;
}
else {
echo '0';
}
echo "</span></div>";
if(is_numeric($total_miles)) {
$weekly_total[] = $total_miles;
}
$week_total_mileage[$day_num] = $total_miles;
$day_num++;
$day_count++;
if ($day_count > 7) {
$total_total = 0;
while($x < 8) {
$total_total = $total_total + $week_total_mileage[$x];
$x++;
}
while($x < 14 && $x > 8) {
$total_total = $total_total + $week_total_mileage[$x];
}
echo "<td>" . array_sum($weekly_total) .
"</td></tr><tr id='row" . $row_number . "'>";
empty($week_total_mileage);
$weekly_total = array();
$day_count = 1;
$row_number++;
}
}
//Finaly we finish out the table with some blank details if needed
$end_days = 1;
while ( $day_count >1 && $day_count <=7 ) {
echo "<td><span id='endDays'>" . $end_days . "</span></td>";
$day_count++;
$end_days++;
}
echo "</tr></table>";
?>
</body>
</html>

Separating a date array to have different output for 1st date in array

Basically I am building a calendar/event booking system on my local PC for personal use, I have the functionality working to get the days from my database that have a booking attached to them, and you recolour the dates in the array accordingly, but I'm trying to work out if I could customise the result of the 1st day in each range, and the following days being the same.
Here is how it looks(if it makes it any easier)
And here is the code I am currently using on the page..
<?php
function date_range($first, $last, $step = '+1 day', $output_format = 'Y-m-d' ) {
$dates = array();
$current = strtotime($first);
$last = strtotime($last);
while( $current <= $last ) {
$dates[] = date($output_format, $current);
$current = strtotime($step, $current);
}
return $dates;
}
?>
<table width="100%" cellspacing="0" cellpadding="0" border="0" style="background-color: #aaaaaa;">
<?php
$cmonth = date('F');
$cyear = date('Y');
$res = mysql_query("SELECT * FROM trips WHERE year(start_date) = '$cyear' ORDER BY start_date ASC");
$array_days = array();
while ($rows = mysql_fetch_array($res)) {
$selected_dates = date_range(date("Y-m-d",strtotime($rows['start_date'])), date("Y-m-d",strtotime($rows['end_date'])));
foreach($selected_dates as $selected_date){
$array_days[$selected_date]['start_end'] = "". $rows['trip_start'] . " to " . $rows['trip_end'];
$array_days[$selected_date]['colour'] = "". $rows['colour'];
$array_days[$selected_date]['booked'] = "". $rows['booked'];
$array_days[$selected_date]['capacity'] = "". $rows['capacity'];
}
}
?>
<tr>
<td> </td>
<?php for($i=1;$i<=31;$i++){?>
<td class="trip-date-cell"><?php echo $i;?></td>
<?php }?>
</tr>
<?php
$current_month = date("m");
$next_6_month = date("m", strtotime("+5 month", strtotime(date("F") . "1")));
for($i=$current_month;$i<=$next_6_month;$i++){ // 12 months in year
?>
<tr>
<td class="trip-month-cell"><?php echo date('M', mktime(0, 0, 0, $i,10, $cyear)); ?></td>
<?php
$days_in_month = cal_days_in_month(CAL_GREGORIAN,$i,$cyear);
foreach (range(1, $days_in_month) as $days) {
$key = date('Y-m-d', mktime(0, 0, 0, $i, $days, $cyear));
if(array_key_exists($key,$array_days)){
$color = $array_days[$key]['capacity'] - $array_days[$key]['booked'] == 0 ? "#303030" : $array_days[$key]['colour'];
echo "<td class='trip-day-book' style='background-color: ".$color."' alt='".$array_days[$key]['start_end']."' title='".$array_days[$key]['start_end']."'> </td>";
} else {
echo "<td class='trip-day-blank'> </td>";
}
}
?>
</tr>
<?php } ?>
</table>
Any suggestions or advice would be appreciated.
You could store the first date information in your array when reading in the date ranges:
while ($rows = mysql_fetch_array($res)) {
$selected_dates = date_range(date("Y-m-d",strtotime($rows['start_date'])), date("Y-m-d",strtotime($rows['end_date'])));
// default to true: first date is first day in range
$firstDate = true;
foreach($selected_dates as $selected_date) {
// store first day info in array
$array_days[$selected_date]['first_day'] = $firstDate;
// set to false for all following days
if ($firstDate) $firstDate = false;
$array_days[$selected_date]['start_end'] = "". $rows['trip_start'] . " to " . $rows['trip_end'];
$array_days[$selected_date]['colour'] = "". $rows['colour'];
$array_days[$selected_date]['booked'] = "". $rows['booked'];
$array_days[$selected_date]['capacity'] = "". $rows['capacity'];
}
}
and then while rendering access the information to achieve your desired result:
$color = $array_days[$key]['capacity'] - $array_days[$key]['booked'] == 0 ? "#303030" : $array_days[$key]['colour'];
// set content of cell according to first day info
$content = $array_days[$key]['first_day'] ? '<strong>1</strong>' : ' ';
echo "<td class='trip-day-book' style='background-color: ".$color."' alt='".$array_days[$key]['start_end']."' title='".$array_days[$key]['start_end']."'>$content</td>";

PHP Blog, need to make a good looking archives section

So! Basically I have a database with a load of blog posts, these are all sorted by a UNIX timestamp, and what I need is a way to make this code spit out headers when appropriate, so that it will output something like this:
2008
November
Title 1 - Date Goes Here
Title 2 - Date Goes Here
December
Title 3 - Date Goes Here
2009
January
Title 4 - Date Goes Here
etcetera
Here's my code so far, it works until the comparison of the year, and I still need to come up with a good way of how to make it compare months in a sensible fashion, so that January indeed comes after December, and not some ludicrous 13th month.
[code]
<?php
if ($db = new PDO('sqlite:./db/blog.sqlite3')) {
$stmt = $db->prepare("SELECT * FROM news ORDER BY date DESC");
if ($stmt->execute()) {
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
$current_year = date("Y", $row[1]);
$current_month = date("m", $row[1]);
if ($current_year > $last_year) {
echo "<h1>" . $current_year . "</h1>";
$last_year = $current_year;
}
echo "<tr>";
echo "<td align='left'><a href='view_post.php?post_id=". $row[1] ."'>" . $row['0'] . " - " . date("Y-m-d, H:i:s", $row[1]) . "</a></td>";
echo "</tr>";
}
}
} else {
die($sqliteerror);
}
?>
[/code]
With unix timestamps you could do something like (pseudo code obviously)
prev_month = null
prev_year = null
foreach results as r
new_month = date('F', r[timestamp]);
new_year = date('Y', r[timestamp]);
if(prev_month != new_month)
//display month
/if
if(prev_year != new_year)
//display year
/if
// display other info
prev_month = new_month
prev_year = new_year
/foreach
<?php
if ($db = new PDO('sqlite:./db/blog.sqlite3')) {
$stmt = $db->prepare("SELECT * FROM news ORDER BY date DESC");
if ($stmt->execute()) {
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
$current_year = date("Y", $row[1]);
$current_month = date("n", $row[1]); # n instead of m
if ($current_year > $last_year) {
echo "<h1>" . $current_year . "</h1>";
echo "<h2>" . $current_month . "</h2>";
$last_year = $current_year;
$last_month = $current_month;
}
elseif ($current_month > $last_month) {
echo "<h2>" . $current_month . "</h2>";
$last_month = $current_month;
}
echo "<tr>";
echo "<td align='left'><a href='view_post.php?post_id=". $row[1] ."'>" . $row['0'] . " - " . date("Y-m-d, H:i:s", $row[1]) . "</a></td>";
echo "</tr>";
}
}
} else {
die($sqliteerror);
}
?>
I did not test it.
Idea:
If the year changes, also the month changes.
The check for the next month happens only if there is not a change of the year, which is the only case where lastmonth might be bigger than currentmonth.
I would be tempted to do this in two steps, separating the database fetching from the display/html logic, for example:
<?php
//snip
//make big array of archive
$archive = array();
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
$year = date("Y", $row[1]);
$month = date("m", $row[1]);
if (!isset($archive[$year])) {
$archive[$year] = array();
}
if (!isset($archive[$year][$month])) {
$archive[$year][$month] = array();
}
$archive[$year][$month][] = $row;
}
//snip
//loop over array and display items
?>
<?php foreach ($achive as $year => $months): ?>
<h1><?php echo $year; ?></h1>
<?php foreach ($months as $month => $posts): ?>
<h2><?php echo $month; ?></h2>
<ul>
<?php foreach ($posts as $post): ?>
<li><?php echo $post[0]; ?> etc...</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
<?php endforeach; ?>
You should get the years and the months in reverse order as per your SQL query. I haven't tested this, but it should be vaguely correct.

Categories