While loop and array in php - php

I have a while loop that will loop for 2 times (Monday and Tuesday).
Within the while loop I have a condition to check whether the first looped is "Monday", if is Monday it will check for the value and checked the corresponding checkbox.
The code work perfectly for Monday. But checkboxes of Tuesday is not echo at all.
<div class="form-group">
<table>
<tr>
<td style="width: 10%"> </td>
<?php
//Get the list of time
$sqlTime="SELECT timeDesc FROM time";
$resultTime=mysql_query($sqlTime);
while($rowTime=mysql_fetch_array($resultTime))
{
?>
<td style="text-align: center"><?php echo $rowTime['timeDesc'];?></td>
<?php
}
?>
</tr>
<?php
//Get the list of day
$sqlDay="SELECT dayDesc FROM day";
$resultDay=mysql_query($sqlDay);
while($rowDay=mysql_fetch_array($resultDay))
{
?>
<tr>
<td><?php echo $rowDay['dayDesc'];?></td>
<?php
//Get the list of time
$sqlTime="SELECT * FROM time";
$resultTime=mysql_query($sqlTime);
while($rowTime=mysql_fetch_array($resultTime))
{
if($rowDay['dayDesc'] == 'Monday')
{
//Get tutor preferences for Monday
$sqlGetMonday = mysql_query("SELECT tutorPreferencesMon FROM tutorpreferences WHERE tutorID='$showTutorID'");
$resultGetMonday = mysql_fetch_array($sqlGetMonday);
$tutorGetMonday = $resultGetMonday['tutorPreferencesMon'];
$showMonday = $tutorGetMonday;
$mondayArray = explode(',', $showMonday);
while($rowTimeList=mysql_fetch_array($resultTimeList))
{
$checkedMonday = "";
foreach($mondayArray as $monday_Array)
{
if($monday_Array == $rowTimeList['timeID'])
{
$checkedMonday = "checked";
}
}
echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedMonday."></td>";
}
}//End if Monday
elseif($rowDay['dayDesc'] == 'Tuesday')
{
//Get tutor preferences for Tuesday
$sqlGetTuesday = mysql_query("SELECT tutorPreferencesTues FROM tutorpreferences WHERE tutorID='$showTutorID'");
$resultGetTuesday = mysql_fetch_array($sqlGetTuesday);
$tutorGetTuesday = $resultGetTuesday['tutorPreferencesTues'];
$showTuesday = $tutorGetTuesday;
$tuesdayArray = explode(',', $showTuesday);
while($rowTimeList=mysql_fetch_array($resultTimeList))
{
$checkedTuesday = "";
foreach($tuesdayArray as $tuesday_Array)
{
if($tuesday_Array == $rowTimeList['timeID'])
{
$checkedTuesday = "checked";
}
}
echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedTuesday."></td>";
}
}//End if Tuesday
elseif($rowDay['dayDesc'] == 'Wednesday')
{
//Get tutor preferences for Wednesday
$sqlGetWednesday = mysql_query("SELECT tutorPreferencesWed FROM tutorpreferences WHERE tutorID='$showTutorID'");
$resultGetWednesday = mysql_fetch_array($sqlGetWednesday);
$tutorGetWednesday = $resultGetWednesday['tutorPreferencesWed'];
$showWednesday = $tutorGetWednesday;
$wednesdayArray = explode(',', $showWednesday);
while($rowTimeList=mysql_fetch_array($resultTimeList))
{
$checkedWednesday = "";
foreach($wednesdayArray as $wednesday_Array)
{
if($wednesday_Array == $rowTimeList['timeID'])
{
$checkedWednesday = "checked";
}
}
echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedWednesday."></td>";
}
}//End if Wednesday
elseif($rowDay['dayDesc'] == 'Thursday')
{
//Get tutor preferences for Thursday
$sqlGetThursday = mysql_query("SELECT tutorPreferencesThurs FROM tutorpreferences WHERE tutorID='$showTutorID'");
$resultGetThursday = mysql_fetch_array($sqlGetThursday);
$tutorGetThursday = $resultGetThursday['tutorPreferencesThurs'];
$showThursday = $tutorGetThursday;
$thursdayArray = explode(',', $showThursday);
while($rowTimeList=mysql_fetch_array($resultTimeList))
{
$checkedThursday = "";
foreach($thursdayArray as $thursday_Array)
{
if($thursday_Array == $rowTimeList['timeID'])
{
$checkedThursday = "checked";
}
}
echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedThursday."></td>";
}
}//End if Thursday
elseif($rowDay['dayDesc'] == 'Friday')
{
//Get tutor preferences for Friday
$sqlGetFriday = mysql_query("SELECT tutorPreferencesFri FROM tutorpreferences WHERE tutorID='$showTutorID'");
$resultGetFriday = mysql_fetch_array($sqlGetFriday);
$tutorGetFriday = $resultGetFriday['tutorPreferencesFri'];
$showFriday = $tutorGetFriday;
$fridayArray = explode(',', $showFriday);
while($rowTimeList=mysql_fetch_array($resultTimeList))
{
$checkedFriday = "";
foreach($fridayArray as $friday_Array)
{
if($friday_Array == $rowTimeList['timeID'])
{
$checkedFriday = "checked";
}
}
echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedFriday."></td>";
}
}//End if Friday
elseif($rowDay['dayDesc'] == 'Saturday')
{
//Get tutor preferences for Saturday
$sqlGetSaturday = mysql_query("SELECT tutorPreferencesSat FROM tutorpreferences WHERE tutorID='$showTutorID'");
$resultGetSaturday = mysql_fetch_array($sqlGetSaturday);
$tutorGetSaturday = $resultGetSaturday['tutorPreferencesSat'];
$showSaturday = $tutorGetSaturday;
$saturdayArray = explode(',', $showSaturday);
while($rowTimeList=mysql_fetch_array($resultTimeList))
{
$checkedSaturday = "";
foreach($saturdayArray as $saturday_Array)
{
if($saturday_Array == $rowTimeList['timeID'])
{
$checkedSaturday = "checked";
}
}
echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedSaturday."></td>";
}
}//End if Saturday
elseif($rowDay['dayDesc'] == 'Sunday')
{
//Get tutor preferences for Sunday
$sqlGetSunday = mysql_query("SELECT tutorPreferencesSun FROM tutorpreferences WHERE tutorID='$showTutorID'");
$resultGetSunday = mysql_fetch_array($sqlGetSunday);
$tutorGetSunday = $resultGetSunday['tutorPreferencesSun'];
$showSunday = $tutorGetSunday;
$sundayArray = explode(',', $showSunday);
while($rowTimeList=mysql_fetch_array($resultTimeList))
{
$checkedSunday = "";
foreach($sundayArray as $sunday_Array)
{
if($sunday_Array == $rowTimeList['timeID'])
{
$checkedSunday = "checked";
}
}
echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedSunday."></td>";
}
}//End if Sunday
}//End while
?>
</tr>
<?php
}
?>
</table>
</div>

Your while loop changes variable $rowTime, while you're checking for the variable $rowDay. You may want to double-check that.
EDIT:
Use this code:
<div class="form-group">
<table>
<tr>
<td style="width: 10%"> </td>
<?php
//Get the list of time
$sqlTime="SELECT timeDesc FROM time";
$resultTime=mysql_query($sqlTime);
while($rowTime=mysql_fetch_array($resultTime))
{
?>
<td style="text-align: center"><?php echo $rowTime['timeDesc'];?></td>
<?php
}
?>
</tr>
<?php
//Get the list of day
$sqlDay="SELECT dayDesc FROM day";
$resultDay=mysql_query($sqlDay);
while($rowDay=mysql_fetch_array($resultDay))
{
?>
<tr>
<td><?php echo $rowDay['dayDesc'];?></td>
<?php
//Get the list of time
$sqlTime="SELECT * FROM time";
$resultTime=mysql_query($sqlTime);
$preferences = array('Monday' => 'tutorPreferencesMon',
'Tuesday' => 'tutorPreferencesTues',
'Wednesday' => 'tutorPreferencesWed',
'Thursday' => 'tutorPreferencesThurs',
'Friday' => 'tutorPreferencesFri',
'Saturday' => 'tutorPreferencesSat',
'Sunday' => 'tutorPreferencesSun');
while($rowTime=mysql_fetch_array($resultTime))
{
$preferences = $preferences[$rowDay['dayDesc']];
$sqlGetMonday = mysql_query("SELECT ".$preferences." FROM tutorpreferences WHERE tutorID='$showTutorID'");
$resultGetMonday = mysql_fetch_array($sqlGetMonday);
$tutorGetMonday = $resultGetMonday[$preferences];
$showMonday = $tutorGetMonday;
$mondayArray = explode(',', $showMonday);
while($rowTimeList=mysql_fetch_array($resultTimeList))
{
$checkedMonday = "";
foreach($mondayArray as $monday_Array)
{
if($monday_Array == $rowTimeList['timeID'])
{
$checkedMonday = "checked";
}
}
echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedMonday."></td>";
}
}//End while
?>
</tr>
<?php
}
?>
</table>
</div>

Related

Unable to insert data into MySQL event database

I am trying to create a PHP/MySQL event calendar. However it doesn't go quite right. Below is my code:
// calender.php
$servername = "localhost";
$username = "username";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
#mysqli_select_db("calen");
if (!#mysqli_select_db) {
die("database connection failed".mysqli_connect_error());
}
/*
mysqli_connect($hostname,$username,$password) or die ($error);
mysqli_select_db($dbname) or die ($error);
*/
?>
<html>
<head>
<script>
function lastmonth(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 = "<?= $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
}
function nextmonth(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 = "<?=$_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
}
</script>
</head>
<body>
<?php
if (isset($_GET['day'])) { //to pass variable
$day = $_GET['day'];
} else {
$day = date("j");
}
if (isset($_GET['month'])) {
$month=$_GET['month'];
} else {
$month = date("n");
}
if (isset($_GET['year'])) {
$year=$_GET['year'];
} else {
$year = date("y");
}
$currentTimeStamp = strtotime("$year-$month-$day");
$monthname = date("F", $currentTimeStamp);
$numofdays = date("t", $currentTimeStamp);
$counter = 0;
?>
<?php
if (isset($_GET['add'])) {
$title =$_POST['txttitle'];
$detail =$_POST['txtdetail'];
$eventdate = $month."/".$day."/".$year;
$insert = "INSERT into calendar(Title,Detail,EventDate,DateAdded) values ('".$title."','".$detail."','".$eventdate."',now())";
$check = mysqli_query($conn,$insert);
if ($check ) {
echo "Event Added...";
} else {
echo "Failed....";
}
}
?>
<table border='1'>
<tr>
<td><input style='width:50px;' type='button' value='<' name='previous' onclick="lastmonth(<?php echo $month.",".$year ?>)"> </td>
<td colspan='5' align='center'> <?php echo $monthname.",".$year ?> </td>
<td><input style='width:50px;' type='button' value='>' name='next' onclick="nextmonth(<?php echo $month.",".$year ?>)"></td>
</tr>
<tr>
<td width='50px' align='center'>Sun</td>
<td width='50px'align='center'>Mon</td>
<td width='50px'align='center'>Tue</td>
<td width='50px'align='center'>Wed</td>
<td width='50px'align='center'>Thu</td>
<td width='50px'align='center'>Fri</td>
<td width='50px'align='center'>Sat</td>
</tr>
<?php
echo "<tr>";
for ($i=1;$i<$numofdays+1;$i++,$counter++) {
$TimeStamp = strtotime("$year-$month-$i");
if ($i == 1) {
$firstday = date("w", $TimeStamp); //which day 1 falls on
for ($j = 0; $j < $firstday; $j++, $counter++) {
echo "<td> </td>";
}
}
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;
}
echo "<td align='center'> <a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true'>".$i."</a> </td>";
}
echo "</tr>";
?>
</table>
<?php
if (isset($_GET['v'])) {
echo "<a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true&f=true'>Add Event </a>";
if (isset($_GET['f'])) {
include("event.php");
}
}
?>
</body>
</html>
event.php
<form name='event' method='POST' action="<?php $_SERVER['PHP_SELF']; ?>?month=<?php echo $month;?>&day=<?php echo $day;?>&year=<?php echo $year; ?>&v=true&add=true">
<table width='400px' border='0'>
<tr>
<td width='150px'>Title</td>
<td width='250px'><input type='text' name='txttitle'</td>
</tr>
<tr>
<td width='150px'>Detail</td>
<td width='250px'><textarea name='txtdetail'></textarea></td>
</tr>
<tr>
<td colspan='2' align='center'><input type='submit' name='btnadd' value='Add Event'></td>
</tr>
</table>
</form>
SQL:
CREATE TABLE `calendar` (
`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`Title` VARCHAR( 65 ) NOT NULL ,
`Detail` VARCHAR( 255 ) NOT NULL ,
`EventDate` VARCHAR( 10 ) NOT NULL ,
`DateAdded` DATE NOT NULL
);
I am unable to insert data into the database. I have pasted my calendar.php, event.php and SQL code. Kindly please check the code and help me out in inserting the data.
You need to add the connection variable with the mysqli_select_db(). So your code will be:
<?php
$servername = "localhost";
$username = "username";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//#mysqli_select_db($conn, "calen");
if(!mysqli_select_db($conn, "calen")) {
die ("database connection failed".mysqli_connect_error());
}
/*
mysqli_connect($hostname,$username,$password) or die ($error);
mysqli_select_db($dbname) or die ($error);
*/
?>
<html>
<head>
<script>
function lastmonth(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 nextmonth(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>
</head>
<body>
<?php
if (isset($_GET['day'])) { //to pass variable
$day=$_GET['day'];
} else {
$day = date("j");
}
if (isset($_GET['month'])) {
$month=$_GET['month'];
} else {
$month = date("n");
if (isset($_GET['year'])) {
$year=$_GET['year'];
} else {
$year = date("y");
}
$currentTimeStamp = strtotime("$year-$month-$day");
$monthname = date("F", $currentTimeStamp);
$numofdays = date("t", $currentTimeStamp);
$counter = 0;
?>
<?php
if (isset($_GET['add'])) {
$title =$_POST['txttitle'];
$detail =$_POST['txtdetail'];
$eventdate = $month."/".$day."/".$year;
$insert = "INSERT INTO calendar (Title,Detail,EventDate,DateAdded) VALUES ('".$title."','".$detail."','".$eventdate."', NOW())";
$check = mysqli_query($conn,$insert);
if ($check) {
echo "Event Added...";
} else {
echo "Failed....";
}
}
?>
<table border='1'>
<tr>
<td> <input style='width:50px;' type='button' value='<' name='previous' onclick="lastmonth(<?php echo $month.",".$year ?>)"> </td>
<td colspan='5' align='center'> <?php echo $monthname.",".$year ?> </td>
<td><input style='width:50px;' type='button' value='>' name='next' onclick="nextmonth(<?php echo $month.",".$year ?>)"></td>
</tr>
<tr>
<td width='50px' align='center'>Sun</td>
<td width='50px'align='center'>Mon</td>
<td width='50px'align='center'>Tue</td>
<td width='50px'align='center'>Wed</td>
<td width='50px'align='center'>Thu</td>
<td width='50px'align='center'>Fri</td>
<td width='50px'align='center'>Sat</td>
</tr>
<?php
echo "<tr>";
for ($i=1;$i<$numofdays+1;$i++,$counter++) {
$TimeStamp = strtotime("$year-$month-$i");
if ($i == 1) {
$firstday = date("w", $TimeStamp); //which day 1 falls on
for ($j = 0; $j < $firstday; $j++, $counter++) {
echo "<td> </td>";
}
}
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;
}
echo "<td align='center'> <a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true'>".$i."</a> </td>";
}
echo "</tr>";
?>
</table>
<?php if (isset($_GET['v'])) {
echo "<a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true&f=true'>Add Event </a>";
if (isset($_GET['f'])) {
include("event.php");
}
}
?>
</body>
</html>
Hope this works for you!
More information at http://www.w3schools.com/php/func_mysqli_select_db.asp

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 Timetable Issue - While Loop Issue

I'm making a simple PHP timetabling website. I've made a table and I have the times in a horizontal header and just Monday at the moment but i want to add 5 days, Monday to Friday. I'm just unsure of how to put the correct data in the correct times and day.
My database has id, name, startTime, endTime and Day
Some data could be 0, Basketball, 9:15, 10:15, Mon.
1, Tennis, 10:15, 11:15, Mon.
2, Cricket, 11:15, 12:15 Tue.
I currently have a while loop that can fill in the table, but not in the
correct slots (td).
Here is my table code so far:
<?php
$host = 'localhost';
$user = 'root';
$pass = '******';
$db = 'test';
$error = 'Error Connecting to database';
$error1 = 'Error Selecting to database';
$connect = mysql_connect($host,$user,$pass) or die($error);
$select_db = mysql_select_db($db) or die($error1);
?>
<table border="1px">
<tr>
<div class="differentLine">
<th > </th>
<th > 9:15 - 10:15 </th>
<th > 10:15 - 11:15 </th>
<th > 11:15 - 12:15 </th>
<th > 12:15 - 13:15 </th>
<th > 13:15 - 14:15 </th>
<th > 14:15 - 15:15 </th>
<th > 15:15 - 16:15 </th>
<th > 16:15 - 17:15 </th>
<th > 17:15 - 18:15 </th>
</div>
</tr >
<tr>
<th >Monday </th>
<?php
$sqlip = "Select * From Modules";
$query = mysql_query($sqlip) or die("Error");
while($row = mysql_fetch_array($query))
{
$name = $row['Name'];
$day = $row['Day'];
$start_time = $row['Start_Time'];
$End_Time = $row['End_Time'];
if($start_time == "9:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "10:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "11:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "12:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "13:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "14:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "15:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "16:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "17:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
echo "</tr>";
}
?>
There are couple of things.
Why do you have
<tr>
<th >Monday </th>
this isn't being database fed, you need to automate that and put it on a
if you want to dynamically print
1, Tennis, 10:15, 11:15, Mon.
you need to have html aligned this way.
<tr><th><?=$order?>,</th> <th><?=$sports?>,</th><th><?=$timing?></th><th><?=$day?></th></tr>
You should also use switch and case statements instead of if statements to take up less code.
You also have $name2 which is printing a variable which doesn't have any value assigned to it; maybe you want to use $name instead to match up the td's, it looks like $name has a value of $row['Name'] in your code.
Don't use mysql_ function, its insecured and deprecated, use mysqli or pdo instead
In addition, all this code can change to 3 rows:
$times_array = array("9:15","11:15","12:15","13:15","14:15","15:15","16:15","17:15")
$row = in_array($start_time,$times_array)?$name2:"";
echo "<td>".$row."</td>";
========================
if($start_time == "9:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "10:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "11:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "12:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "13:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "14:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "15:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "16:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
if($start_time == "17:15"){
echo"<td> $name2</td>";
} else {
echo"<td> </td>";
}
Calendars/timetables are easily created in php when using arrays and loops. Here is how I would do it.
First, create the array and times-
//an array of all your start/end times
$times = array(
array('start'=>'9:15', 'end'=>'10:15'),
array('start'=>'10:15', 'end'=>'11:15'),
array('start'=>'11:15', 'end'=>'12:15'),
array('start'=>'12:15', 'end'=>'13:15'),
array('start'=>'13:15', 'end'=>'14:15'),
array('start'=>'14:15', 'end'=>'15:15'),
array('start'=>'15:15', 'end'=>'16:15'),
array('start'=>'16:15', 'end'=>'17:15'),
array('start'=>'17:15', 'end'=>'18:15')
);
Now you can build your header using a loop-
<table border="1px">
<tr class="differentLine">
<th > </th>
<?php foreach($times as $time){
echo '<th > '.$time['start'].' - '.$time['end'].' </th>';
} ?>
</tr >
Second, create an array of the days-
//use the short version (Mon, Tue, etc) as keys, as that is what you show in your data
//and use the long version for displaying
$days = array('Mon'=>'Monday', 'Tue'=>'Tuesday', 'Wed'=>'Wednesday', 'Thu'=>'Thursday', 'Fri'=>'Friday');
Now you can build your rows using a loop-
<?php foreach($days as $short=>$long){ ?>
<tr>
<th > <?php echo $long; ?> </th>
</tr>
<?php } ?>
Third, create an array of your database data-
// order your database data by the Day and Start_Time
<?php
$sqlip = "Select * From Modules ORDER BY Day, Start_Time";
$query = mysql_query($sqlip) or die("Error");
// create an array to hold your data
$events = array();
// loop through your database rows and add to the array
while($row = mysql_fetch_array($query)){
//add the row to the array, using the Day as a key and the Start_time as a key
$events[$row['Day']][$row['Start_Time']] = $row;
}
Now you have an array that would look like-
$events = array(
'Mon'=> array(
'9:15' => array('id'=>0, 'name'=>'Basketball', 'startTime'=>'9:15', 'endTime'=>'10:15', 'Day'=>'Mon'),
'10:15' => array('id'=>1, 'name'=>'Tennis', 'startTime'=>'10:15', 'endTime'=>'11:15', 'Day'=>'Mon')
),
'Tue'=> array(
'11:15' => array('id'=>2, 'name'=>'Cricket', 'startTime'=>'11:15', 'endTime'=>'12:15', 'Day'=>'Wed')
)
);
Fourth, now in your day row loop, loop through each time array, and check if an event array exists-
<?php foreach($days as $short=>$long){ ?>
<tr>
<th > <?php echo $long; ?> </th>
<?php
// check if there are any events for this day
if(isset($events[$short])){
//since there are events for this day, loop through all the times
foreach($times as $k=>$time){
//check if there is an event at this time
if(isset($events[$short][$time['start']])){
//echo the event
echo '<td>'.$events[$short][$time['start']]['name'].'</td>';
}
else {
//since no event, echo blank cell
echo '<td></td>';
}
}
}
else {
// since no events for the day, create blank cells for each time
for($t=0;$t<count($times);$t++){
echo '<td></td>';
}
} ?>
</tr>';
<?php } ?>
Now when you put it all together it would look like -
<?php
//your database connection
...
//
//an array of all your start/end times
$times = array(
array('start'=>'9:15', 'end'=>'10:15'),
array('start'=>'10:15', 'end'=>'11:15'),
array('start'=>'11:15', 'end'=>'12:15'),
array('start'=>'12:15', 'end'=>'13:15'),
array('start'=>'13:15', 'end'=>'14:15'),
array('start'=>'14:15', 'end'=>'15:15'),
array('start'=>'15:15', 'end'=>'16:15'),
array('start'=>'16:15', 'end'=>'17:15'),
array('start'=>'17:15', 'end'=>'18:15')
);
//use the short version (Mon, Tue, etc) as keys, as that is what you show in your data
//and use the long version for displaying
$days = array('Mon'=>'Monday', 'Tue'=>'Tuesday', 'Wed'=>'Wednesday', 'Thu'=>'Thursday', 'Fri'=>'Friday');
// order your database data by the Day and Start_Time
<?php
$sqlip = "Select * From Modules ORDER BY Day, Start_Time";
$query = mysql_query($sqlip) or die("Error");
// create an array to hold your data
$events = array();
// loop through your database rows and add to the array
while($row = mysql_fetch_array($query)){
//add the row to the array, using the Day as a key and the Start_time as a key
$events[$row['Day']][$row['Start_Time']] = $row;
} ?>
<table border="1px">
<tr class="differentLine">
<th > </th>
<?php foreach($times as $time){
echo '<th > '.$time['start'].' - '.$time['end'].' </th>';
} ?>
</tr >
<?php foreach($days as $short=>$long){ ?>
<tr>
<th > <?php echo $long; ?> </th>
<?php
// check if there are any events for this day
if(isset($events[$short])){
//since there are events for this day, loop through all the times
foreach($times as $k=>$time){
//check if there is an event at this time
if(isset($events[$short][$time['start']])){
//echo the event
echo '<td>'.$events[$short][$time['start']]['name'].'</td>';
}
else {
//since no event, echo blank cell
echo '<td></td>';
}
}
}
else {
// since no events for the day, create blank cells for each time
for($t=0;$t<count($times);$t++){
echo '<td></td>';
}
} ?>
</tr>';
<?php } ?>

Show title in calender PHP

I have php to display the calender and another php to check database to see bookings, If there is a booking on certain day it displays "Booked". In database i have column for date of booking and title. How could i make it to display Title from the database instead of "Booked"?
This is to display calender.
<?php
$monthNames = Array("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December");
?>
<?php
if (!isset($_REQUEST["month"]))
$_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"]))
$_REQUEST["year"] = date("Y");
?>
<?php
$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;
if ($prev_month == 0 )
{
$prev_month = 12;
$prev_year = $cYear - 1;
}
if ($next_month == 13 )
{
$next_month = 1;
$next_year = $cYear + 1;
}
require("BookingsDB.php");
$myBookingsDB = new BookingsDB();
$bookings = $myBookingsDB->getMonthlyBookings($cMonth,$cYear);
?>
<div align="left">
<table width="400" border="5" align="left" id="calendar">
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF"><table width="100%" border="0"
cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="left"><a href="<?php echo $_SERVER["PHP_SELF"]
. "?month=". $prev_month . "&year=" . $prev_year; ?>"
style="color:#FFFFFF">Previous</a></td>
<td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"]
. "?month=". $next_month . "&year=" . $next_year; ?>"
style="color:#FFFFFF">Next</a></td>
</tr>
</table></td>
</tr>
<tr>
<td align="center"><table width="100%" border="2" cellpadding="2"
cellspacing="2">
<tr align="center">
<td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong> <?php
echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>S</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>M</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>W</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>F</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>S</strong></td>
</tr>
<?php
require("connection.php");
$con=mysqli_connect("$mysql_host","$mysql_user","$mysql_password","$mysql_database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= "select activityEvent.activityTitle from activityEvent";
$result = mysqli_query($con,$sql);
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
$today = getdate();
for ($i=0; $i<($maxday+$startday); $i++)
{
if(($i % 7) == 0 )
{
echo "<tr> ";
}
if($i < $startday)
{
echo "<td></td> ";
}
else
{
$day = $i - $startday + 1;
$thisDate = new DateTime("$cYear-$cMonth-$day");
$jsEvent[] = "document.getElementById('trigger" . $i . "').onclick = function() {showForm()};";
echo "<td align='center' valign='middle' height='20px'><a href='#' id='trigger" . $i . "'>". ($i - $startday + 1) . "</a>";
foreach ($bookings as $bookingDate)
{
if ($thisDate==$bookingDate)
{
echo "Booked";
while($row = mysqli_fetch_array($result))
{
echo $sql= "select activityEvent.activityTitle from activityEvent";
}
}
}
echo "</td>";
}
if(($i % 7) == 6 )
{
echo "</tr> ";
}
}
mysqli_close($con);
?>
<script type="text/javascript">
<?php foreach($jsEvent as $event)
{
echo $event;
}
?>
function showForm(){
document.getElementById('timeslots').style.display="block";
};
</script>
</table></td>
</tr>
</table>
This PHP checks booked dates in the database.
<?php
class BookingsDB
{
private $bookings = array();
private $con;
public function BookingsDB()
{
require("connection.php");
$this->con = mysqli_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
}
public function getMonthlyBookings($thisMonth,$thisYear)
{
$sql = "SELECT date,activityTitle FROM activityEvent WHERE YEAR(date) = $thisYear AND MONTH(date) = $thisMonth";
$result = mysqli_query($this->con,$sql);
while($row=mysqli_fetch_array($result))
{
$thisBooking = $row['date'];
$thisBookingDateTime = new DateTime($thisBooking);
array_push($this->bookings,$thisBookingDateTime);
}
return $this->bookings;
}
public function close()
{
mysqli_close($this->con);
}
}
?>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
.red
{
color: red
}
.green
{
background-color: aquamarine;
}
table
{
text-align: center;
}
td
{
height: 60px;
width: 100px;
}
.opcity
{
background-color: rgba(0,0,0,.2);
}
</style>
</head>
<body>
<?php
$Cur_day = date('d');
$Cur_Month = date('m');
$Cur_Year = date('Y');
$op = '';
if (isset($_REQUEST['month']) || isset($_REQUEST['year']) && isset($_REQUEST['stday']))
{
$Mo = $_REQUEST['month'];
$Year = $_REQUEST['year'];
$query_date = $Year . '-' . $Mo . '-21';
if (isset($_REQUEST['opra']))
{
$str = $_REQUEST['opra'];
if ($str == 'iny')
{
$op = '+1 year';
}
if ($str == 'dey')
{
$op = '-1 year';
}
if ($str == 'inm')
{
$op = '+1 month';
}
if ($str == 'dem')
{
$op = '-1 month';
}
if ($str == 'dem' || $str == 'inm' || $str == 'dey' || $str == 'iny')
{
$query_date1 = date('Y-m-01', strtotime($op, strtotime($query_date)));
$lastDay = date('t', strtotime($op, strtotime($query_date)));
$year = date('Y', strtotime($op, strtotime($query_date)));
$month = date('n', strtotime($op, strtotime($query_date)));
$Month_Name = date('M', strtotime($op, strtotime($query_date)));
$pre_month = date('t', strtotime('-1 month', strtotime($year . '-' . $month . '-21')));
}
if ($str == 'No')
{
$query_date1 = date('Y-m-01', strtotime($query_date));
$lastDay = date('t', strtotime($query_date));
$year = date('Y', strtotime($query_date));
$month = date('n', strtotime($query_date));
$Month_Name = date('M', strtotime($query_date));
$pre_month = date('t', strtotime('-1 month', strtotime($year . '-' . $month . '-21')));
}
}
?>
<input type="hidden" name="txt_year" id="txt_year1" value="<?php echo $year; ?>">
<input type="hidden" name="txt_month1" id="txt_month1" value="<?php echo $month; ?>">
<table border="1">
<tr>
<td><<</td>
<td><</td>
<td colspan="3"><?php echo $Month_Name . "-" . $year; ?></td>
<td>></td>
<td>>></td>
</tr>
<tr>
<?php
echo '<tr>';
for ($i = 1; $i <= 7; $i++)
{
if ($_REQUEST['stday'] == 'Sun')
{
$j_val = 0;
echo "<th>" . date("l", gmmktime(0, 0, 0, 11, $i, 2015));
echo '</th>';
} else
{
$j_val = 1;
echo "<th>" . date("l", gmmktime(0, 0, 0, 6, $i, 2015));
echo '</th>';
}
}
echo '</tr>';
?>
</tr>
<?php
$Conn = new mysqli("localhost", "root", "", "demo");
$firstDays = mktime(0, 0, 0, date($month), 1, date($year));
$weekday = date('N', $firstDays);
$i = 0;
$j = 0;
$date = 1;
$Count = 0;
$Count1 = 0;
$next_mo = 1;
function Holiday($day1, $month1, $year1)
{
global $Conn;
$result = $Conn->query("SELECT * FROM holiday");
while ($row = mysqli_fetch_assoc($result))
{
if ($row['day'] == $day1 && $row['month'] == $month1 && $row['year'] == $year1)
{
return $row['title'] . "<br>";
}
}
}
for ($i = 0; $i < 6; $i++)
{
echo '<tr>';
$pre_month = $pre_month - ($weekday - $j_val);
for ($j = $j_val; $j <= 6 + $j_val; $j++)
{
if ($Count < $weekday - $j_val && ($weekday != 7 || $_REQUEST['stday'] == 'Mon'))
{
echo "<td class='opcity'>" . ( ++$pre_month) . "</td>";
$Count++;
} else
{
if ($date <= $lastDay)
{
if ($Cur_day == $date && $Cur_Month == $month && $Cur_Year == $year)
{
if (date("l", gmmktime(0, 0, 0, $month, $date, $year)) == 'Sunday')
{
echo '<td class = "green red">';
echo Holiday($date, $month, $year);
} else
{
echo '<td class = "green">';
echo Holiday($date, $month, $year);
}
} else
{
if (date("l", gmmktime(0, 0, 0, $month, $date, $year)) == 'Sunday')
{
echo '<td class = "red">';
echo Holiday($date, $month, $year);
} else
{
echo '<td >';
echo Holiday($date, $month, $year);
}
}
echo $date;
echo '</td>';
$date++;
if ($date == $lastDay)
{
$i = 7;
}
} else
{
echo "<td class='opcity'>" . $next_mo++ . "</td>";
}
}
}
echo '</tr>';
}
}
?>
</table>
</body>
</html>
//<Ajax_cal.php
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="JS/jquery-1.11.3.min.js" type="text/javascript"></script>
<script>
function Caleonload()
{
$('#month').val('<?php echo date("n"); ?>');
$('#year').val('<?php echo date('Y'); ?>');
Opretion('No');
}
function Cal(month)
{
$('#month').val(month);
Opretion('No');
}
function Cal1(year)
{
$('#year').val(year);
Opretion('No');
}
function Opretion(Op)
{
var st_day = $('input[type="radio"][class="rd_sun_mon"]:checked').val();
var Mo = $('#month').val();
var Year = $('#year').val();
var Op1 = Op;
$.post("AJAX/Ajax_Cal.php",
{
month: Mo,
year: Year,
opra: Op1,
stday: st_day
},
function (data)
{
$('#Show_Cal').html(data);
var txt_year = $('#txt_year1').val();
$("#year").html('');
for (var y = parseInt(txt_year) - 5; y < parseInt(txt_year) + 5; y++)
{
$("#year").append('<option value="' + y + '">' + y + '</option>');
}
$('#year').val(txt_year);
$('#month').val($('#txt_month1').val());
}
);
}
function incyear()
{
Opretion('iny');
}
function deyear()
{
Opretion('dey');
}
function incmonth()
{
Opretion('inm');
}
function demonth()
{
Opretion('dem');
}
</script>
</head>
<body onload="Caleonload()">
<select name="month" id="month" onchange="Cal(this.value)" size="1">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="year" id="year" onchange="Cal1(this.value)" size="1">
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select>
Starting Day :
<input type="radio" name="rd_sun_mon" class="rd_sun_mon" value="Sun" checked="" onchange="Opretion('No')">Sunday
<input type="radio" name="rd_sun_mon" class="rd_sun_mon" value="Mon" onchange="Opretion('No')">Monday
<div id="Show_Cal"></div>
</body>
</html>
DATA BASE PHP MY ADDMIN:
CREATE TABLE IF NOT EXISTS holiday (
id int(5) NOT NULL AUTO_INCREMENT,
day int(4) NOT NULL,
month int(4) NOT NULL,
year int(6) NOT NULL,
title varchar(20) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
I think your problem is that is that you need to put the format in order to compare two Datetime :
if ($thisDate->format('Y-m-d')==$bookingDate->format('Y-m-d'))
{
echo "Booked";
....
}
In addition to #SimonGuichard's answer, I suggest that you return an associative array from the getMonthlyBookings function that contains both the date and the activityTitle. This way, you wouldn't have to select the title from the database again when it needs to be displayed.
public function getMonthlyBookings($thisMonth,$thisYear) {
$sql = "SELECT date,activityTitle FROM activityEvent WHERE YEAR(date) = $thisYear AND MONTH(date) = $thisMonth";
$result = mysqli_query($this->con,$sql);
while($row=mysqli_fetch_array($result)) {
$this->bookings[] = array('date'=>new DateTime($row['date']), 'activityTitle'=>$row['activityTitle']);
}
return $this->bookings;
}
Then in the script that displays the calendar, you should modify it this way:
$day = $i - $startday + 1;
$thisDate = new DateTime("$cYear-$cMonth-$day");
$jsEvent[] = "document.getElementById('trigger" . $i . "').onclick = function() {showForm()};";
echo "<td align='center' valign='middle' height='20px'><a href='#' id='trigger" . $i . "'>". ($i - $startday + 1) . "</a>";
// $bookings is an associative array of the form array('0'=>array('date'=>'date_value', 'activityTitle'=>'activityTitle_value'))
foreach ($bookings as $key => $value) {
if ($thisDate->format('Y-m-d') === $value['date']->format('Y-m-d')) {
// this prints out the activity title
echo $value['activityTitle'];
}
}

call or create session variable in SUM

I am not very familiar with PHP or MySQL, but after researching here I have been able to learn and get very close to what I need and build my first sum query. I am trying to read the database and sum values based on several variables.
I need the reservation_pax from the reservations table where reservation_time is 8:00 where reservation_hidden = 0 and reservation_date is something. I can manually enter the date and it works. I am now trying to use a session code already in the script or find a way to to dynamically add based on selected date.
Here is the code I have working without the dynamic aspect or session.
$result = mysql_query("SELECT SUM(reservation_pax)
FROM reservations
WHERE reservation_time = '8:00:00'
AND reservation_date = '2014-10-27'
AND reservation_hidden ='0'") ;
if ($result === false) {
die(mysql_error()); // TODO: better error handling
}
while($row = mysql_fetch_array($result)) {
echo $row['SUM(reservation_pax)'];
}
Here is the full code of the page where I entered the above addition. Can anyone help me figure out how to call the selected date rather than having to manually enter.
<!-- Begin reservation table data -->
<br/>
<table class="global resv-table-small" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<?php
echo "<td class='noprint'> </td>";
echo "<td>Time</td>";
echo "<td>Guests/Type</td>";
echo "<td>Name</td>";
echo "<td>Special Instructions/Notes</td>";
echo "<td class='noprint'>Table</td>";
echo "<td class='noprint'>Status</td>";
echo "<td class='noprint'>Created</td>";
echo "<td class='noprint'>Details/Delete</td>";
echo "</tr>";
// Clear reservation variable
$reservations ='';
if ($_SESSION['page'] == 1) {
$reservations = querySQL('all_reservations');
}else{
$reservations = querySQL('reservations');
}
// reset total counters
$tablesum = 0;
$guestsum = 0;
if ($reservations) {
//start printing out reservation grid
foreach($reservations as $row) {
// reservation ID
$id = $row->reservation_id;
$_SESSION['reservation_guest_name'] = $row->reservation_guest_name;
// check if reservation is tautologous
$tautologous = querySQL('tautologous');
echo "<tr id='res-".$id."'>";
echo "<td";
// daylight coloring
if ($row->reservation_time > $daylight_evening){
echo " class='evening noprint'";
}else if ($row->reservation_time > $daylight_noon){
echo " class='afternoon noprint'";
}else if ($row->reservation_time < $daylight_noon){
echo " class='morning noprint'";
}
echo " style='width:10px !important; padding:0px;'> </td>";
echo "<td id='tb_time'";
// reservation after maitre message
if ($row->reservation_timestamp > $maitre['maitre_timestamp'] && $maitre['maitre_comment_day']!='') {
echo " class='tautologous' title='"._sentence_13."' ";
}
echo ">";
echo "<strong>".formatTime($row->reservation_time,$general['timeformat'])."</strong></td>";
echo "<td id='tb_pax'><strong class='big'>".$row->reservation_pax."</strong> <span class='noprint'>";
printType($row->reservation_hotelguest_yn);
//echo "<img src='images/icons/user-silhouette.png' class='middle'/>";
echo "</span></td><td style='width:10%' id='tb_name'><span class='noprint'>".printTitle($row->reservation_title)."</span><strong> <a id='detlbuttontrigger' href='ajax/guest_detail.php?id=".$id."'";
// color guest name if tautologous
if($tautologous>1){echo" class='tautologous tipsy' title='"._tautologous_booking."'";}
echo ">".$row->reservation_guest_name."</a></strong>";
// old reservations symbol
if( (strtotime($row->reservation_timestamp) + $general['old_days']*86400) <= time() ){
echo "<img src='images/icons/clock-bolt.png' class='help tipsyold middle smicon' title='"._sentence_11."' />";
}
// recurring symbol
if ($row->repeat_id !=0) {
echo " <img src='images/icons/loop-alt.png' alt='"._recurring.
"' title='"._recurring."' class='tipsy' border='0' >";
}
echo"</td><td style='width:10%' id='tb_note'>";
if ($_SESSION['page'] == 1) {
echo $row->outlet_name;
}else{
echo $row->reservation_notes;
}
echo "</td>";
if($_SESSION['wait'] == 0){
echo "<td class='big tb_nr' style='width:85px;' id='tb_table'><img src='images/icons/table_II.png' class='tipsy leftside noprint' title='"._table."' /><div id='reservation_table-".$id."' class='inlineedit'>".$row->reservation_table."</div></td>";
}
echo "<td class='noprint'><div>";
getStatusList($id, $row->reservation_status);
echo "</div></td>";
echo "<td class='noprint'>";
echo "<small>".$row->reservation_booker_name." | ".humanize($row->reservation_timestamp)."</small>";
echo "</td>";
echo "<td class='noprint'>";
// MOVE BUTTON
// echo "<a href=''><img src='images/icons/arrow.png' alt='move' class='help' title='"._move_reservation_to."'/></a>";
// WAITLIST ALLOW BUTTON
if($_SESSION['wait'] == 1){
$leftspace = leftSpace(substr($row->reservation_time,0,5), $availability);
if($leftspace >= $row->reservation_pax && $_SESSION['outlet_max_tables']-$tbl_availability[substr($row->reservation_time,0,5)] >= 1){
echo" <a href='#' name='".$id."' class='alwbtn'><img src='images/icons/check-alt.png' name='".$id."' alt='"._allow."' class='help' title='"._allow."'/></a> ";
}
}
// EDIT/DETAIL BUTTON
echo "<a href='?p=102&resID=".$id."'><img src='images/icons/pen-fill.png' alt='"._detail."' class='help' title='"._detail."'/></a> ";
// DELETE BUTTON
if ( current_user_can( 'Reservation-Delete' ) && $q!=3 ){
echo"<a href='#modalsecurity' name='".$row->repeat_id."' id='".$id."' class='delbtn'>
<img src='images/icons/delete.png' alt='"._cancelled."' class='help' title='"._delete."'/></a>";
}
echo"</td></tr>";
$tablesum ++;
$guestsum += $row->reservation_pax;
}
}
?>
</tbody>
<tfoot>
<tr style="border:1px #000;">
<td class=" noprint"></td><td></td>
<td colspan="2" class="bold"><?php echo $guestsum;?> <?php echo _guest_summary;?></td>
<td></td>
<td colspan="2" class="bold"><?php echo $tablesum;?> <?php echo _tables_summary;?></td>
<td colspan="2" class="bold"><?php
$result = mysql_query("SELECT SUM(`reservation_pax`) FROM `reservations` WHERE `reservation_time` = '8:00:00' AND `reservation_date` = '{$_SESSION['selectedDate']}' AND `reservation_hidden` ='0'") ;
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while($row = mysql_fetch_array($result))
{
echo $row['SUM(reservation_pax)'];
}
?>
<?php echo '/ 40 ', _guest_summary, ' -8:00 AM';?></td>
<td></td>
<?php
if($_SESSION['wait'] == 0){
//echo "<td></td>";
}
?>
</tr>
</tfoot>
</table>
<!-- End reservation table data -->
you can use alias field, as:
$result = mysql_query("SELECT SUM(reservation_pax) AS reserve_sum
FROM reservations WHERE reservation_time = '8:00:00'
AND reservation_date = '2014-10-27'
AND reservation_hidden ='0'"
) ;
....
and get access it as:
while($row = mysql_fetch_array($result))
{
echo $row['reserve_sum'];
}
$result = mysql_query("SELECT SUM(reservation_pax) FROM reservations WHERE reservation_time = '8:00:00' AND reservation_date = '{$_SESSION['selectedDate']}' AND reservation_hidden ='0'") ;
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while($row = mysql_fetch_array($result))
{
echo $row['SUM(reservation_pax)'];
}
?>

Categories