Jenling Posted March 10, 2008 Share Posted March 10, 2008 Its me again! I want to ask if there is a way to loop the query in the database? Im building a booking system recently. I wanted to make a table to show all the status of occupied or available in the column of the table. However, I need to compare the startTime_of_booking,end_time_of_booking and the room in my database in order to show the value on the table. Can somebody help me with this? This is my code: <?php require_once('connections/connect.php'); session_start(); ?> <script type="text/javascript"> <!-- function redirect() { window.location = "index.php" } //--> </script> <? if(!session_is_registered(username)) { echo "<script type=\"text/javascript\">". "alert('Unauthorized access to this page. Please Log In');". "redirect()". "</script>"; } ?> <html> <head> <meta http-equiv="Content-Language\" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>SKTM Online Booking System</title> <script> <language="javascript" type="text/javascript"> function switchpage(select) { var index; for(index=0; index<select.options.length; index++) if(select.options[index].selected) { if(select.options[index].value!="") window.location.href=select.options[index].value; break; } } </script> <SCRIPT language=Javascript> function displayMenu(currentMenu) { var thisMenu = document.getElementById(currentMenu).style if (thisMenu.display == "block") { thisMenu.display = "none" } else { thisMenu.display = "block" } return false } </script> <style type="text/css"> <!-- .cornercell { text-align: center; border: 1px solid black; height: 20px; background-color: #DCDCDC; } .daycell { text-align: center; width: 12%; height: 20px; border-bottom: 1px solid black; border-right: 1px solid black; border-top: 1px solid black; background-color: #DCDCDC; } .timecell { text-align: center; border-bottom: 1px solid black; border-right: 1px solid black; border-left: 1px solid black; background-color: #DCDCDC; width: 11%; height: 20px; } .concell { border-right: 1px solid black; border-bottom: 1px solid black; background-color: #F2F2F2; text-align: center; } .setcell { border-right: 1px solid black; border-bottom: 1px solid black; background-color: #EFEFEF; text-align: center; } --> </style> <body background="img/bg_bar.jpg"> <table align="center" border="0" width="989" cellspacing="0" cellpadding="0"> <tr> <td rowspan="2" width="10%"> <img border="0" src="img/UMS.jpg" width="107" height="108"></td> <td width="89%" height="84"> <p align="center"><font face="Century Gothic" size="6" color="#0099FF">SKTM Online Booking System</font></td> </tr> <tr> <td width="89%" bgcolor="#0099FF" height="15"><font face="Tw Cen MT"> <font color="#FFFFFF">Welcome! You are currently log on as <? echo "$username";?></font></font></td> </tr> <tr> <td colspan="2" bordercolor="#0099FF"> <table border="1" width="100%" cellspacing="0" cellpadding="0" bordercolor="#0099FF"> <tr> <td valign="top" width="182" height="660"><p> </p> <? include "siteBar.php"; ?></td> <td valign="top" width=""> <table border="1" width="100%" height="38"> <tr> <? include "MRBar.php"; ?> </tr> </table> <table width= "100%" height="208" border="0"> <tr> <td width="77%" height="204" valign="top"> <h3>Meeting Room >> View Availability</h3> <form action="" method="post" name="form1" target="_self" id="form1"> <label> <div align="center" ><h3>Select a meeting room to view :<h3> </font> <select name="select"> <option value="Meeting Room 1">Meeting Room 1</option> <option value="Meeting Room 2">Meeting Room 2</option> <option value="Meeting Room 3">Meeting Room 3</option> <option value="Meeting Room 4">Meeting Room 4</option> <option value="Meeting Room 5">Meeting Room 5</option> <option value="Meeting Room 6">Meeting Room 6</option> <option value="Eksekutif">Eksekutive</option> <option value="Seminar Room">Seminar Room</option> </select> <input type="submit" name="Submit" value="Select" /> </form> <div align="center"><?php if($_POST[select]==$select1) { echo ("Meeting Room 1"); } else if ($_POST[select]==$select2) { echo ("Meeting Room 2");} else if ($_POST[select]==$select3) { echo ("Meeting Room 3");} else if ($_POST[select]==$select4) { echo ("Meeting Room 4");} else if ($_POST[select]==$select5) { echo ("Meeting Room 5");} else if ($_POST[select]==$select6) { echo ("Meeting Room 6");} else if ($_POST[select]==$select7) { echo ("Eksekutive");} else if ($_POST[select]==$select8) { echo ("Seminar Room");} $select=$_POST[select];?> <?php $start = 800; $stop = 2230; //$number_of_rooms = 7; $http = $_SERVER['PHP_SELF']; $week_days = array("Mon","Tue","Wed","Thur","Fri","Sat","Sun"); $result = array(); $cycle = array(); Function search_between($val, $arr) { $rtn = false; foreach($arr as $array) { if($array['startTime'] <= $val && $array['endTime'] > $val) { $rtn = $array; break; } } return $rtn; } Function get_last_monday($day,$month,$year) { $timestamp = mktime(0,0,0,$month,$day,$year); While(date('w',$timestamp) != 1) $timestamp -= 60*60*24; return $timestamp; } $day = $_GET['day']; $month = $_GET['month']; $year = $_GET['year']; if(empty($_GET['day'])) $day = date('d'); if(empty($_GET['month'])) $month = date('m'); if(empty($_GET['year'])) $year = date('Y'); $last_monday = get_last_monday($day,$month,$year); $forward['day'] = date("d", ($last_monday+(8*24*3600))); $forward['month'] = date("m", ($last_monday+(8*24*3600))); $forward['year'] = date("Y", ($last_monday+(8*24*3600))); $back['day'] = date("d", ($last_monday-(6*24*3600))); $back['month'] = date("m", ($last_monday-(6*24*3600))); $back['year'] = date("Y", ($last_monday-(6*24*3600))); if(isset($_POST['confi'])) // If 'jump to day' button has been pressed { // Error notifications if ($_POST['day'] > 31) die("Error! There can't be that many days in a month"); if ($_POST['month'] > 12) die("Error! There can't be that many months in a day"); if ($_POST['year'] < 2007) die("Error! You can't set a reservation event in the past"); } elseif(isset($_POST['confirm'])) { if ($_POST['day'] > 31) die("Virhe! Montakos päivää voi olla kuukaudessa..?"); if ($_POST['month'] > 12) die("Virhe! Montakos kuukautta olikaan vuodessa?"); if ($_POST['year'] < 2003) die("Virhe! Onko varaus kenties menneisyydessä?"); if ($_POST['hour'] > 24) die("Virhe! Montakos tuntia päivässä olikaan?"); if ($_POST['minute'] > 60) die("Virhe! Montakos niitä minuutteja olikaan?"); if ($_POST['hour2'] > 24) die("Virhe! Montakos tuntia päivässä olikaan?"); if ($_POST['minute2'] > 60) die("Virhe! Montakos niitä minuutteja olikaan?"); if ($_POST['hour'] > $_POST['hour2']) die("Virhe! Aloitustunti on suurempi kuin lopetustunti!"); $contents .= ' <table cellpadding="6" border="0" width="100%"> <tr> <td align="left"> Today Date: '.date("d.m.Y", $last_monday).' - '.date("d.m.Y", ($last_monday+6*24*3600)).'<br> <a href="MRview.php?day='.$back['day'].'&month='.$back['month'].'&year='.$back['year'].'">[Previous Day]</a> <a href="MRview.php?day='.date('d').'&month='.date('m').'&year='.date('Y').'">[This Day]</a> <a href="MRview.php?day='.$forward['day'].'&month='.$forward['month'].'&year='.$forward['year'].'">[Next Day]</a><br> </td> <td align="right"> <form method="get" action="MRview.php" style="margin: 0px;"> Jump To: <input type="text" size="2" value="'.$day.'" maxlength="2" name="day"> <b>.</b> <input type="text" size="2" value="'.$month.'" maxlength="2" name="month"> <b>.</b> <input type="text" size="4" value="'.$year.'" maxlength="4" name="year"> <input type="submit" value=">>"> </form> </td> </tr> </table> <table cellspacing="0" cellpadding="1" width="100%"> <tr> <td class="cornercell"> Time </td> '; for($f=0;$f<7;$f++) { $contents .= " <td class=\"daycell\"> ".$week_days[$f]." ".date("d.m.Y",$last_monday+($f*60*60*24))." </td>\n"; $set[$g] = 0; } $contents .= " </tr>\n"; for($i=$start; $i<$stop; $i=$i+50) { $contents .= " <tr>\n"; $first_num = substr($i, 0, -2); if(substr($i, -2) == 50) $last_num = '30'; else $last_num = '00'; if($last_num == 30) { $sec_last_num = '00'; $sec_first_num = $first_num+1; } else { $sec_last_num = '30'; $sec_first_num = $first_num; } $contents .= " <td class=\"timecell\">".$first_num.":".$last_num."-".$sec_first_num.":".$sec_last_num."</td>\n"; $query1 = "SELECT MRname FROM booking WHERE MRname='$select'"; $result1 = mysql_query($query1); $row=mysql_fetch_assoc($result1); do{ for($g=0; $g<7; $g++) { $seek_time = $g*24*60*60+$last_monday+($first_num*60*60+$last_num*60); $s_result = search_between($seek_time, $result); if(!$s_result) { $MRview = " "; $not_found = false; } else { $not_found = true; $MRname = stripslashes($s_result['MRname']); if($cycle[$g] == 0 && (($s_result['endTime'] - $s_result['startTime'])/60/30) >= 1) { $cycle[$g] = ($s_result['endTime'] - $s_result['startTime'])/60/30; $begin = 1; } } if($cycle[$g] == 0) { $contents .= " <td class=\"concell\"> </td>\n"; } elseif($begin == 1) { $contents .= " <td class=\"setcell\" rowspan=\"".$cycle[$g]."\">"; if($not_found) $contents .= '<a href="'.$http.'?act=poista&id='.$s_result['id'].'&day='.$day.'&month='.$month.'&year='.$year.'">[D]</a> '; $contents .= $MRname."</td>\n"; $begin = 0; } if($cycle[$g] > 0) { $cycle[$g]--; } } $contents .= " </tr>\n"; } while($row=mysql_fetch_assoc($result1)); /* $contents .= ' </table><br> <form action="'.$http.'?day='.$day.'&month='.$month.'&year='.$year.'" method="post" name="form"> <table border="0" cellpadding="3" cellspacing="0" width="98%"> <tr> <td valign="middle" align="left">Varattava aika</td> <td valign="middle" align="left"> <input type="text" size="2" value="'.$day.'" maxlength="2" name="day"> <b>.</b> <input type="text" size="2" value="'.$month.'" maxlength="2" name="month"> <b>.</b> <input type="text" size="4" value="'.$year.'" maxlength="4" name="year"> klo: <select name="hour" size="1"> '; for($r=substr($start, 0, -2); $r<=substr($stop, 0, -2); $r++) $contents .= ' <option value="'.$r.'">'.$r."</option>\n"; $contents .= ' </select> <b>:</b> <select name="minute" size="1"> <option value="00">00</option> <option value="30">30</option> </select> - <select name="hour2" size="1"> '; for($r=substr($start, 0, -2); $r<=substr($stop, 0, -2); $r++) $contents .= ' <option value="'.$r.'">'.$r."</option>\n"; */ $contents .= ' </table> </form> '; print $contents; ?> </table> </td> </tr> </td> </tr> </table> <table align="center" border="0" width="800" cellspacing="0" cellpadding="0"> <td rowspan="1" width="30%"> <p align="center" style="margin-top: 0; margin-bottom: 0"> </p> <p align="center" style="margin-top: 0; margin-bottom: 0"> </p> <p align="center" style="margin-top: 0; margin-bottom: 0"> <font size="2" face="Times New Roman">© </font> <font face="Lucida Console" size="2">UMS Copyright 2007. All Rights Reserved.</font></p> <p align="center" style="margin-top: 0; margin-bottom: 0"> </p> <p align="center" style="margin-top: 0; margin-bottom: 0"> <font face="Lucida Console" size="2">University Malaysia Sabah</font></p> <p align="center" style="margin-top: 0; margin-bottom: 0"> <font face="Lucida Console" size="2">88450 Kota Kinabalu</font></p> </table></td> </body> </html> Link to comment https://forums.phpfreaks.com/topic/95481-loop-from-the-query/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.