select next event from database - php

I am working on a function which selects a list of dates, and highlight the next event. I have managed to highlight all future events, however I would like to only highlight the nearest future event. How can this be done?
<?php
function racesbydate($sql) {
include 'connect.php';
$year = $_GET['year'];
$get = $year.'%';
$select = $conn->prepare($sql);
$select->bind_param('s', $get);
$select->execute();
$select->store_result();
if ($select->num_rows > 0) {
echo "<div class='races' id='$year' style='display:block'>
<table>\n<th colspan='5'>Cycling Season $year </th>\n
<tr id='information'>\n
<th id='date'>Date</th>\n
<th id='race'>Race</th>\n
<th id='route'>Route</th>\n
<th id='info'>Entry</th>\n
<th id='rizultz'>Results</th>";
$meta = $select->result_metadata();
while ($field = $meta->fetch_field()) {
$params[] = &$row[$field->name];
}
call_user_func_array(array($select, 'bind_result'), $params);
while ($select->fetch()) {
$today = date("Y-m-d");
$date = new DateTime($row['date']);
$formatted_date = $date->format('d/m/Y');
if ($row['date'] > $today) {
if ($row['infoID'] != "0") {
echo "<tr class='racedetails' id='nextrace' >\n
<td class='dark' id='date'>".$formatted_date."</td>\n
<td class='light' id='race'>".$row["title"]."</td>\n
<td class='dark' id='route'>".$row["route"]."</td>\n
<td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
<td class='dark' id='rizults'>N/A</td>\n</tr>\n";
}
} else {
if (($row['infoID'] != "0") && ($row['resultID'] !="0" )) {
echo "<tr class='racedetails' id='race' >\n
<td class='dark' id='date'>".$formatted_date."</td>\n
<td class='light' id='race'>".$row["title"]."</td>\n
<td class='dark' id='route'>".$row["route"]."</td>\n
<td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
<td class='dark' id='rizults'><a href='".$row["resultID"]."'>Results</a></td>\n</tr>\n";
}
}
}
}
echo "</table>\n</div>";
$select->close();
}
current result
<tr class='racedetails' id='nextrace' ><td class='dark' id='date'>06/01/2016</td></tr>
<tr class='racedetails' id='nextrace' ><td class='dark' id='date'>08/02/2016</td></tr>
goal results
<tr class='racedetails' id='nextrace' ><td class='dark' id='date'>06/01/2016</td></tr>
<tr class='racedetails' id='race' ><td class='dark' id='date'>08/02/2016</td></tr>

If I understood you correctly, you only need a flag to store the information whether the next race has already been seen or not:
:
call_user_func_array(array($select, 'bind_result'), $params);
$nextRace = true;
while ($select->fetch()) {
$today = date("Y-m-d");
$date = new DateTime($row['date']);
$formatted_date = $date->format('d/m/Y');
if ($row['date'] > $today) {
if ($row['infoID'] != "0") {
$cssId = $nextRace ? 'nextrace' : 'race';
$nextRace = false;
echo "<tr class='racedetails' id='".$cssId."' >\n
<td class='dark' id='date'>".$formatted_date."</td>\n
<td class='light' id='race'>".$row["title"]."</td>\n
<td class='dark' id='route'>".$row["route"]."</td>\n
<td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
<td class='dark' id='rizults'>N/A</td>\n</tr>\n";
}
} else {
:
This assumes that the order of the races is ascending by date (which your examples suggest).

Related

Generate a daily transaction Register

Please, i need assistance on how to generate a register for a transaction which is entered into the database daily as a register, group by each car. See my code below:
Selection Criteria:
<tr>
<td>Start Date:<input type="date" name="d1" class="form-control" id="datepicker" /></td>
<td>End Date:<input type="date" name="d2" class="form-control" id="datepicker" /></td>
<?php $s = mysqli_query($connection,"SELECT * FROM tab_location");?>
<td>Location:
<select name="loc" class="form-control">
<option>Select Location...</option>
<?php while($lo = mysqli_fetch_array($s)) { ?>
<option value="<?php echo $lo['location_name'];?>"><?php echo $lo['location_name'];?></option>
<?php } ?>
</select>
</td>
<td>Action:<input type="submit" name="sbt" class="form-control btn btn-success" value="Generate Report" /></td>
</tr>
Table to Display the result:
<table width="70%" border="0" style="margin-top:15px;" align="left" class="table table-bordered">
<thead>
<tr>
<th>S/N</th>
<th nowrap="nowrap">FLT NO</th>
<?php $list = array();
$month = date('m');
$year = date('Y');
$dy = cal_days_in_month(CAL_GREGORIAN,date('n'),date('Y'));
for($d=1; $d<=$dy; $d++) {
$time = mktime(12, 0, 0, $month, $d, $year);
if (date('m', $time) == $month)
$list[]=date('Y-m-d', $time);
}
foreach ($list as $li){
echo "<th>".$li."</th>";
}?>
</tr>
</thead>
<tbody>
<?php if(isset($_POST['sbt'])){
$loc = $_POST['loc'];
$d1 = $_POST['d1'];
$d2 = $_POST['d2'];
$c = 0;
$st = mysqli_query($connection, "SELECT DISTINCT(fltno),created_at,id,status FROM tab_ddaily WHERE loc='$loc' AND CAST(created_at as date) BETWEEN '$d1' AND '$d2' ORDER BY fltno");
while($r = mysqli_fetch_array($st)){
$c++;?>
<tr>
<td><?php echo $c;?></td>
<td nowrap="nowrap"><?php echo $r['fltno'];?></td>
<td nowrap="nowrap"><?php echo $r['status']=='Available'?'<img src="includes/images/pass.jpe" width="20" height="10" />':'<img src="includes/images/wrong.jpe" width="20" height="10" />';?></td>
</tr>
<?php }
} else {
$loc = "";
$d1 = "";
$d2 = "";
}?>
</tbody>
</table>
The Above produces:
I want it to produce:Distinct FLT No on a row with the status of each day spreading from the 1st day to the last day of the month. Kindly help on how to achieve this.
See structure and Data below:
[![enter image description here][2]][2]
I have modified the output array returned from database and then reiterate it to show FLT and status date-wise.
<tbody>
<?php if(isset($_POST['sbt'])){
$loc = $_POST['loc'];
$d1 = $_POST['d1'];
$d2 = $_POST['d2'];
$c = 1;
$st = mysqli_query($connection, "SELECT DISTINCT(fltno),created_at,id,status FROM tab_ddaily WHERE loc='$loc' AND CAST(created_at as date) BETWEEN '$d1' AND '$d2' ORDER BY fltno");
$temp = array();
while($r = mysqli_fetch_array($st)){
$temp[ $r['fltno'] ][ $r['created_at'] ] = array('id' => $r['id'], 'status' => $r['status']);
}
foreach($temp as $key => $values){
$dates = array_keys($values); ?>
<tr>
<td><?php echo $c;?></td>
<td nowrap="nowrap"><?php echo $key;?></td>
<?php foreach ($list as $li){
if(in_array($li, $dates)){ ?>
<td nowrap="nowrap"><?php echo ($values[$li]['status'] == 'Available') ? '<img src="includes/images/pass.jpe" width="20" height="10" />':'<img src="includes/images/wrong.jpe" width="20" height="10" />';?></td>
<?php } else{ ?>
<td nowrap="nowrap">n/a</td>
<?php }
} ?>
</tr>
<?php $c++;
}
} else {
$loc = "";
$d1 = "";
$d2 = "";
}?>
</tbody>
Outer loop is printing table row, SR_NO, FLT_NO and inner loop is printing status date wise.

How to show calendar month's previous days?

I'm trying to make a calendar that shows this month, and then fills in the remaining days on the end rows with the dates from the next and previous months.
I managed to make it show next months dates and the thing is the issue is that I need it to show previous months dates as well.
If someone manages to figure out the issues please add what the problem was as well how you managed to fix it, to help me in the future.
I will add a image of the calendar as well.
The script:
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="view-calendar">
<tr>
<td valign="top" width="14.2857%">Mondag</td>
<td valign="top" width="14.2857%">Tuesday</td>
<td valign="top" width="14.2857%">Wednesday</td>
<td valign="top" width="14.2857%">Thursday</td>
<td valign="top" width="14.2857%">Friday</td>
<td valign="top" width="14.2857%">Saturday</td>
<td valign="top" width="14.2857%">Sunday</td>
</tr>
<?php
// Get current month dates
$days_count = date('t');
$current_day = date('d');
$week_day_first = date('N', mktime(0, 0, 0, date('m'), 1, date('Y')));
// Get previous month dates
// Get next month dates
$next_start = strtotime(date("Y-m-00", strtotime("+1 month")));
$next_dates = array();
for ($w = 1 - $week_day_first + 1; $w <= $days_count; $w = $w + 7){
echo '<tr>';
$counter = 0;
for ($d = $w; $d <= $w + 6; $d++){
if($d < 10){
$current_date = date("Y").date("m").'0'.$d;
}else{
$current_date = date("Y").date("m").$d;
}
echo '<td valign="top" width="14.2857%"'.(($d > 0 ? ($d > $days_count ? ' class="disabled"' : '') : ' class="disabled"')).(($counter > 4 ? ' class="week-day"' : '')).'>';
if($d > 0){
if($d > $days_count){
for($in = 1; $in <= 1; $in++){
echo array_push($next_dates, date('j', strtotime("+ $in day", $next_start)));
}
}else if($current_day == $d){
echo '<div class="current-day"><span class="given-date">'.$d.'</span></div>';
}else{
echo '<span class="given-date">'.$d.'</span>';
}
}else{
//Here comes previous dates
}
echo '</td>';
$counter++;
}
echo '</tr>';
}
?>
</table>
When you calculated $week_day_first, you can save your time calculated from the first day of the month into a variable.
$time_first_day_of_month = mktime(0, 0, 0, date('m'), 1, date('Y'));
Then you can reuse that later to calculate the offset of days before the first of the month.
date('d', strtotime("$offset day",$time_first_day_of_month))
Putting it all together:
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="view-calendar">
<tr>
<td valign="top" width="14.2857%">Mondag</td>
<td valign="top" width="14.2857%">Tuesday</td>
<td valign="top" width="14.2857%">Wednesday</td>
<td valign="top" width="14.2857%">Thursday</td>
<td valign="top" width="14.2857%">Friday</td>
<td valign="top" width="14.2857%">Saturday</td>
<td valign="top" width="14.2857%">Sunday</td>
</tr>
<?php
// Get current month dates
$days_count = date('t');
$current_day = date('d');
// save time of first day for later use
$time_first_day_of_month = mktime(0, 0, 0, date('m'), 1, date('Y'));
$week_day_first = date('N', $time_first_day_of_month);
// Get next month dates
$next_start = strtotime(date("Y-m-00", strtotime("+1 month")));
$next_dates = array();
for ($w = 1 - $week_day_first + 1; $w <= $days_count; $w = $w + 7){
echo '<tr>';
$counter = 0;
for ($d = $w; $d <= $w + 6; $d++){
if($d < 10){
$current_date = date("Y").date("m").'0'.$d;
}else{
$current_date = date("Y").date("m").$d;
}
echo '<td valign="top" width="14.2857%"'.(($d > 0 ? ($d > $days_count ? ' class="disabled"' : '') : ' class="disabled"')).(($counter > 4 ? ' class="week-day"' : '')).'>';
if($d > 0){
// next month's dates
if($d > $days_count){
for($in = 1; $in <= 1; $in++){
echo array_push($next_dates, date('j', strtotime("+ $in day", $next_start)));
}
}
// today
else if($current_day == $d){
echo '<div class="current-day"><span class="given-date">'.$d.'</span></div>';
}
// this month's dates
else{
echo '<span class="given-date">'.$d.'</span>';
}
}
// last month's dates
else{
//Here comes previous dates
$offset = $d - 1;
echo '<span class="given-date">'.date('d', strtotime("$offset day",$time_first_day_of_month)).'</span>';
}
echo '</td>';
$counter++;
}
echo '</tr>';
}
?>
</table>
Results in this output:
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="view-calendar">
<tr>
<td valign="top" width="14.2857%">Mondag</td>
<td valign="top" width="14.2857%">Tuesday</td>
<td valign="top" width="14.2857%">Wednesday</td>
<td valign="top" width="14.2857%">Thursday</td>
<td valign="top" width="14.2857%">Friday</td>
<td valign="top" width="14.2857%">Saturday</td>
<td valign="top" width="14.2857%">Sunday</td>
</tr>
<tr><td valign="top" width="14.2857%" class="disabled"><span class="given-date">30</span></td><td valign="top" width="14.2857%" class="disabled"><span class="given-date">31</span></td><td valign="top" width="14.2857%"><span class="given-date">1</span></td><td valign="top" width="14.2857%"><span class="given-date">2</span></td><td valign="top" width="14.2857%"><span class="given-date">3</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">4</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">5</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">6</span></td><td valign="top" width="14.2857%"><span class="given-date">7</span></td><td valign="top" width="14.2857%"><span class="given-date">8</span></td><td valign="top" width="14.2857%"><span class="given-date">9</span></td><td valign="top" width="14.2857%"><span class="given-date">10</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">11</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">12</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">13</span></td><td valign="top" width="14.2857%"><span class="given-date">14</span></td><td valign="top" width="14.2857%"><span class="given-date">15</span></td><td valign="top" width="14.2857%"><span class="given-date">16</span></td><td valign="top" width="14.2857%"><div class="current-day"><span class="given-date">17</span></div></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">18</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">19</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">20</span></td><td valign="top" width="14.2857%"><span class="given-date">21</span></td><td valign="top" width="14.2857%"><span class="given-date">22</span></td><td valign="top" width="14.2857%"><span class="given-date">23</span></td><td valign="top" width="14.2857%"><span class="given-date">24</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">25</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">26</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">27</span></td><td valign="top" width="14.2857%"><span class="given-date">28</span></td><td valign="top" width="14.2857%"><span class="given-date">29</span></td><td valign="top" width="14.2857%"><span class="given-date">30</span></td><td valign="top" width="14.2857%" class="disabled">1</td><td valign="top" width="14.2857%" class="disabled" class="week-day">2</td><td valign="top" width="14.2857%" class="disabled" class="week-day">3</td></tr></table>
Here is code using the DateTime class, which makes it quite readable:
$today = new DateTime('today'); // only change this line to test other months
$this_month = $today->format('M');
$date = clone $today;
$date->modify('first day of this month')->modify('+1 day')->modify('last Monday');
do {
echo '<tr>';
for ($weekday = 0; $weekday < 7; $weekday++){
$out = str_replace($this_month, '', $date->format('M'));
$content = "<span class='given-date'>$out {$date->format('d')}</span>";
if ($today == $date) $content = "<div class='current-day'>$content</div>";
$class = ($out ? 'disabled ' : '') . ($weekday > 4 ? 'week-day' : '');
echo "<td valign='top' width='14.2857%' class='$class'>$content</td>";
$date->modify('+1 day');
}
echo '</tr>';
} while ($date->format('M') == $this_month);
Although this is quite old now I found Jeff's solution very useful. I refactored it a little and put it in this Gist, I hope someone finds it useful. Part of it is here:
/**
* Return part of an HTML table containing all the days
* in the current month, plus padd it with next and
* previous months days if needed to fill out the grid
*
*
* #param date $date date object containing month to display
*
* #return string
*/
function get_calendar_days_in_month_html($date) {
$date_format = "Y-m-d";
$days_count = $date->format('t'); //Get number of days in this month
$weeks_count = ceil($days_count / 7); //How many weeks in this month?
$total_cells = $weeks_count * 7;
//clone is used or we literally are modifying the $date variable
$first_date_of_month = clone $date->modify('first day of this month');
$first_day_of_month = $first_date_of_month->format("N"); //returns 1-7 EG Mon-Fri
$first_date_of_grid = $first_date_of_month ->modify('-' . $first_day_of_month . ' days');
$todays_date = new DateTime();
$todays_date_str = $todays_date->format($date_format);
$selected_date_str = $date->format($date_format);
$day_of_week = 1; //FIXME: allow starting with Sunday or whatever
$ht = '<tr>';
for ($cell=1; $cell <= $total_cells ; $cell++) {
$classes = []; //CSS classes
$current_date = $first_date_of_grid->modify("+1 day");
$current_date_str = $current_date->format($date_format);
if($current_date_str == $todays_date_str) $classes[] = "today";
if($selected_date_str == $todays_date_str) $classes[] = "selected-date";
/* if current date is not from this month (EG previous or next month) then give
it a special class, you might want to grey it out or whatever */
if($date->format("m") !== $current_date->format("m")) $classes[] = "grid-filler";
$ht .= '
<td date="' . $current_date_str . '" class="' . implode(" ", $classes) . '">' . $current_date->format("j") . '</td>';
$day_of_week ++;
if($day_of_week == 8) {
$ht .= '</tr>';
if($cell < $total_cells) $ht .= '<tr>';
$day_of_week = 1;
}
}
return $ht;
}

Event calendar not showing February

I am creating an events calendar for my website and I have used this tutorial and this other one, as well as the youtube tutorial by unknown ghost.
However, when I enter an event, it comes up as successful but doesn't turn blue.
Have I made an error/typo that could explain why it isn't turning blue?
Also once an event has been added, it does display at the bottom but if I refresh the page the event gets added again automatically.
Went through each month and realised February doesn't come up. Why is this?
Here is my code:
calendar.php
<?php
include("functions.php")
?>
<html>
<head>
<title>Event Calendar</title>
<script>
function goLastMonth(month, year){
if(month == 1) {
--year;
month = 13;
}
--month
var monthstring = ""+month+"";
var monthlength = monthstring.length;
if(monthlength <=1){
monthstring = "0"+monthstring;
}
document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
}
function goNextMonth(month, year){
if(month == 12) {
++year;
month = 0;
}
++month
var monthstring = ""+month+"";
var monthlength = monthstring.length;
if(monthlength <=1){
monthstring = "0"+monthstring;
}
document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
}
</script>
<style>
.today{
background-color: #00ff00;
}
.event{
background-color: #0000ff;
}
</style>
</head>
<body>
<?php
//get current date or specific month and year
if (isset($_GET['day'])){
$day = $_GET['day'];
} else {
$day = date("d");
}
if(isset($_GET['month'])){
$month = $_GET['month'];
} else {
$month = date("m");
}
if(isset($_GET['year'])){
$year = $_GET['year'];
}else{
$year = date("Y");
}
//get date data for display such as month name
$currentTimeStamp = strtotime( "$day-$month-$year");
$monthName = date("F", $currentTimeStamp);
//get the number of days in the current month and year
$numDays = date("t", $currentTimeStamp);
//keep track of the number of cell created
$counter = 0;
?>
<?php
if(isset($_GET['add'])) {
$name = $_POST['Event_Name'];
$details = $_POST['Details'];
$date = $day."/".$month."/".$year;
$location = $_POST['Location'];
$staffrq = $_POST['Staff_Required'];
$cadetsrq = $_POST['Cadets_Required'];
//insert into database
$sqlinsert = "INSERT INTO Event_Detail (Event_Name, Details, Date, Location, Staff_Required, Cadets_Required) VALUES
('".$name."', '".$details."', '".$date."', '".$location."', '".$staffrq."','".$cadetsrq."')";
$resultinsert = mysql_query($sqlinsert);
if($resultinsert) {
echo "Event Successfully Added...";
}else{
echo "Event Failed to be Added...";
}
}
?>
<table border="1px">
<tr>
<td align="center">
<input style='width:80px;' type='button' value='<'name='previousbutton' onclick ="goLastMonth(<?php echo $month.','.$year?>)">
</td>
<td align="center" colspan="5"><?php echo $monthName." ".$year; ?></td>
<td align="center">
<input style='width:80px;' type='button' value='>'name='nextbutton' onclick ="goNextMonth(<?php echo $month.','.$year?>)">
</td>
</tr>
<tr>
<td align="center" width='80px'>Monday</td>
<td align="center" width='80px'>Tuesday</td>
<td align="center" width='80px'>Wednesday</td>
<td align="center" width='80px'>Thursday</td>
<td align="center" width='80px'>Friday</td>
<td align="center" width='80px'>Saturday</td>
<td align="center" width='80px'>Sunday</td>
</tr>
<tr align="center">
<?php
for($i = 1; $i < $numDays+1; $i++, $counter++){
$timeStamp = strtotime("$i-$month-$year");
//create empty cell until first day of the month
if($i == 1) {
//0=sun, 1=mon, 2=tue, ...
$firstDay = date("w", $timeStamp);
//if sunday change the firstDay to 7
if($firstDay == 0)
$firstDay = 7;
//decrement firstDay by 1
$firstDay--;
for($j = 0; $j < $firstDay; $j++, $counter++) {
echo "<td> </td>";
}
}
//create new row
if($counter % 7 == 0) {
echo"</tr><tr>";
}
$monthstring = $month;
$monthlength = strlen($monthstring);
$daystring = $i;
$daylength = strlen($daystring);
if($monthlength <=1){
$monthstring = "0".$monthstring;
}
if($daylength <=1) {
$daystring = "0".$daystring;
}
$todaysDate = date("d/m/Y");
$dateToCompare = $daystring.'/'.$monthstring.'/'.$year;
//print day number
echo "<td align='center'";
if ($todaysDate == $dateToCompare) {
echo "class='today'";
}else{
$sqlCount = "SELECT * FROM Event_Detail WHERE Date='".$dateToCompare."'";
$noOfEvent = mysql_num_rows(mysql_query($sqlCount));
if($noOfEvents >=1) {
echo "class='event'";
}
}
echo "><a href='".$_SERVER['PHP_SELF']."?day=".$daystring."&month=".$monthstring."&year=".$year."&v=true'>".$i."</a></td>";
}
//fill up the leftover cells of the table
while($counter % 7 != 0) {
echo "<td> </td>";
$counter++;
}
?>
</tr>
</table>
<?php
if(isset($_GET['v'])){
echo "<a href='".$_SERVER['PHP_SELF']."?day=".$day."&month=".$month."&year=".$year."&v=true&f=true'>Add Event</a>";
if(isset($_GET['f'])){
include("eventform.php");
}
$sqlEvent = "SELECT * FROM Event_Detail WHERE Date='".$day."/".$month."/".$year."'";
$resultEvents=mysql_query($sqlEvent);
echo "<hr>";
while($events=mysql_fetch_array($resultEvents)){
echo "Event Name: ".$events['Event_Name']."<br>";
echo "Details: ".$events['Details']."<br>";
echo "Date: ".$events['Date']."<br>";
echo "Location: ".$events['Location']."<br>";
echo "Staff Required: ".$events['Staff_Required']."<br>";
echo "Cadets Required: ".$events['Cadets_Required']."<br>";
}
}
?>
</body>
</html>
eventform.php
<title>Event</title>
<form name="eventform" method="POST" action="<?php $_SERVER['PHP_SELF']; ? >?day=<?php echo $day;?>&month=<?php echo $month;?>&year=<?php echo $year;? >&v=true&add=true">
<table width="400px">
<tr>
<td width="150px">Event Name</td>
<td width="250px"><input type="text" name="Event_Name" required></td>
</tr>
<tr>
<td width="150px">Details</td>
<td width="250px"><textarea name="Details" required></textarea></td>
</tr>
<tr>
<td width='150px'>Location</td>
<td width='250px'><textarea name="Location" required></textarea></td>
</tr>
<tr>
<td width='150px'>Staff Required</td>
<td width='250px'><input type='number' name="Staff_Required" required></td>
</tr>
<tr>
<td width='150px'>Cadets Required</td>
<td width='50px'><input type='number' name="Cadets_Required" required></td>
</tr>
<tr>
<td colspan='2' align='center'><input type='submit' name='btnadd' value='Add Event'></td>
</tr>

nested while loops php mysql

I have this code:
<?php
if( isset($_POST['groups'])){
$groups = $_POST['groups'];
$subject = $_POST['subject'];
$sql="SELECT
a.groupcode, a.groupstudents, a.studentid,
b.groupcode, b.coursename, b.studentid, b.date, b.class1, b.attend, b.attendno
FROM table_1 a, table_2 b
WHERE b.groupcode = '$groups' AND b.coursename = '$subject' AND
(a.studentid = b.studentid AND a.groupcode = b.groupcode)";
$result=mysqli_query($GLOBALS["___mysqli_ston"], $sql); ?>
<table width="100%" border="1" cellspacing="0" cellpadding="3" >
<tr>
<td align="center"><strong><font size="2">Students</font></strong></td>
<td align="center"><strong><font size="2">Date</font></strong></td>
<td align="center"><strong><font size="2">Attendance</font></strong> </td>
</tr>
<?php
while($rows=mysqli_fetch_array($result)){
$date = $rows['date']; $date2 = date("d-F-Y", strtotime($date));
$class1 = $rows['class1'];
if ($class1 == 0) $class1 = "No Class"; if ($class1 == 1) $class1 = "Absent";
if ($class1 == 3) $class1 = "Present"; if ($class1 == 2) $class1 = "Late";
?>
<tr>
<td align="center"><font size="2"><?php echo $rows['groupstudents']; ?></font> </td>
<td align="center"><strong><font size="2"><?php echo $date2; ?></font></strong> </td>
<td align="center"><font size="2"><?php echo $class1; ?></font></td>
</tr>
<?php
}
?>
which gives the below output.
Now my question is how to modify my code (use nested loops?!) so the output is:
Thank you kindly.
NB: Sorry, I do not have enough reputation to attach images. I have uploaded them on an external site.
It may not be the best solution, but I cannot think of something better right now.
In the pre-execution I create the grid you want, and in the layout this grid-array is displayed.
<?php
if( isset($_POST['groups'])){
$groups = $_POST['groups'];
$subject = $_POST['subject'];
$sql="SELECT
a.groupcode, a.groupstudents, a.studentid,
b.groupcode, b.coursename, b.studentid, b.date, b.class1, b.attend, b.attendno
FROM table_1 a, table_2 b
WHERE b.groupcode = '$groups' AND b.coursename = '$subject' AND
(a.studentid = b.studentid AND a.groupcode = b.groupcode)";
$result=mysqli_query($GLOBALS["___mysqli_ston"], $sql);
$dates = array();
$display = array();
while ($rows=mysqli_fetch_array($result)) {
if (!isset($display[$rows['groupstudents']])) {
$display[$rows['groupstudents']] = array();
}
if (!isset($dates[strtotime($rows['date'])])) {
$dates[strtotime($rows['date'])] = count($dates);
}
$class1 = $rows['class1'];
if ($class1 == 0) $class1 = "No Class"; if ($class1 == 1) $class1 = "Absent";
if ($class1 == 3) $class1 = "Present"; if ($class1 == 2) $class1 = "Late";
$display[$rows['groupstudents']][$dates[strtotime($rows['date'])]] = $class1;
}
echo '<table width="100%" border="1" cellspacing="0" cellpadding="3">';
echo '<tr>';
echo '<td align="center"><strong><font size="2">Students</font></strong></td>';
foreach ($dates as $date => $reversedIndex) {
echo '<td align="center"><strong><font size="2">' . date("d-F-Y", $date) . '</font></strong></td>';
}
echo '</tr>';
foreach ($display as $student => $row) {
echo '<tr>';
echo '<td align="center"><font size="2">' . $student . '</font></td>';
foreach ($dates as $date => $index) {
echo '<td align="center"><font size="2">';
if (isset($row[$index])) {
echo $row[$index];
} else {
echo '';
}
echo '</font></td>';
}
echo '</tr>';
}
echo '</table>';
?>

PHP search data with 2 table relations based on date range input

I have a problem with my code here :
The error is : Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\siix_dev\overtime\track_reports.php on line 333
I think the error in join table function, but I can't analyze whats solution for this case.
<?php
include ("config.php");
$bagianWhere = "";
if (isset($_POST['chkBadge']))
{
$badge_id = $_POST['badge_id'];
$status_acknowledge = "Acknowledged";
if (empty($bagianWhere))
{
$bagianWhere .= "badge_id = '$badge_id' order and t_submissions.submission_no=t_acknowledged.submission_no and t_acknowledged.status_acknowledge='$status_acknowledge' by time_submission DESC";
}
}
if (isset($_POST['chkEmp']))
{
$employee_name = $_POST['employee_name'];
$status_acknowledge = "Acknowledged";
if (empty($bagianWhere))
{
$bagianWhere .= "employee_name LIKE '$employee_name' and t_submissions.submission_no=t_acknowledged.submission_no and t_acknowledged.status_acknowledge='$status_acknowledge' order by time_submission DESC";
}
else
{
$bagianWhere .= " AND employee_name LIKE '$employee_name' and t_submissions.submission_no=t_acknowledged.submission_no and t_acknowledged.status_acknowledge='$status_acknowledge' order by time_submission DESC";
}
}
if (isset($_POST['chkOtdate']))
{
$date_from = $_POST['date_from'];
$date_to = $_POST['date_to'];
$status_acknowledge = "Acknowledged";
if (empty($bagianWhere))
{
$bagianWhere .= "ot_date between '$date_from' and '$date_to' and t_submissions.submission_no=t_acknowledged.submission_no and t_acknowledged.status_acknowledge='$status_acknowledge' order by time_submission DESC";
}
else
{
$bagianWhere .= " AND ot_date between '$date_from' and '$date_to' and t_submissions.submission_no=t_acknowledged.submission_no and t_acknowledged.status_acknowledge='$status_acknowledge' order by time_submission DESC";
}
}
$query = "SELECT t_submissions.submission_no, t_submissions.badge_id,
t_submissions.employee_name, t_submissions.ot_date, t_submissions.dept_name,
t_submissions.ot_from, t_submissions.ot_to,
t_submissions.remarks, t_submissions.submission_by, t_acknowledged.acknowledge_by,
FROM t_submissions, t_acknowledged WHERE ".$bagianWhere;
$hasil = mysql_query($query);
if(mysql_num_rows($hasil) > 0)
{
?>
<div class="outer">
<div id="main" class="wrapper">
<div class="content-area">
<table cellspacing="0" class="font">
<thead>
<tr>
<th class="th">Form No</th>
<th class="th">Badge ID</th>
<th class="th">Name</th>
<th class="th">OT Date</th>
<th class="th">OT From</th>
<th class="th">OT To</th>
<th class="th">Submission By</th>
<th class="th">Status Ack.</th>
<th class="th">Status App.</th>
</tr>
</thead>
<?php
while($submission = mysql_fetch_array($hasil))
{?>
<tbody>
<tr>
<td class="td"><?php echo $submission['submission_no'];?></td>
<td class="td"><?php echo $submission['badge_id'];?></td>
<td class="td"><?php echo $submission['employee_name'];?></td>
<td class="td"><?php echo $submission['ot_date'];?></td>
<td class="td"><?php echo $submission['ot_from'];?></td>
<td class="td"><?php echo $submission['ot_to'];?></td>
<td class="td"><?php echo $submission['submission_by'];?></td>
<td class="td"><?php echo $submission['acknowledge_by'];?></td>
<td class="td"><?php echo $submission['approval_by'];?></td>
</tr>
</tbody>
<?php
}?>
</table>
</div>
</div>
</div>
<?php
}
else
{
echo '<p STYLE="position:absolute; TOP:170px; left:500px;">Data not found.</p>';
}
;
?>
The search using date range input and 2 table relations.
Please help me to solve this problem.
Thank you.
The SQL query is being set incorrectly (as such supplying a FALSE to mysql_num_rows). Most likely within your WHERE clause $bagianWhere. From first glance it seems that if both $_POST['chkEmp'] and $_POST['chkOtdate'] are set it would return an error as the latter does not begin with an AND.
I would recommend you test the query by itself with all possible combinations (they seem small anyway).
Also, is a cartesian product really needed here?
Edit: $bagianWhere .= "badge_id = '$badge_id' order and - that order seems out of place?

Categories