CroNiX Posted January 22, 2015 Share Posted January 22, 2015 One question I have is, assuming you only want to allow a user to be able to book a single timeslot, why you are using checkboxes and allowing them to select multiple timeslots? What happens if I select them all? It might be better to use links instead, along with a querystring which would be comprised of the date, start time and end time. <a href="/book.php?date=2015-1-22&start=0800&end=0830">8:00am - 8:30am</a> and then in book.php, grab the date, start and end times $date = $_GET['date']; $start = $_GET['start']; $end = $_GET['end']; Then check the database to see if that timeslot is indeed free based on the date, start and end times If it is free, book the timeslot If not, show error message with a link back to the calendar page. Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503796 Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 That is a very good suggestion thank you, i did not think about that. The users can only select 2 timeslots after that they cant book any for that day. Would this be better? it is for a doctors center Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503799 Share on other sites More sharing options...
CroNiX Posted January 22, 2015 Share Posted January 22, 2015 So the user can only select 1, or 2, timeslots from the same day? Do they need to be consecutive time slots? One problem in helping with this stuff is only you know the business logic required for this application. So our answers may or may not fit into that since we don't know the requirements. We can answer specific questions posed, but the answers may not fit into how the app is supposed to logically work. Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503800 Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 yes i would need consecutive time slots as this will make sense, so i will need a condition; if a user selects two slots which are not consecutive there will be an error as they can only select two like so: 09:00-09:15 09:15-09:30 Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503801 Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 (edited) firstly i think i need to set the key for the available, booked, partially booked time slots for the day selected and then i will work on the condition for the consecutive days. Now that the time slot table is set up, do i need to get rid of the html time slot table i created? The calendar that i have created does that need to be in the database? Edited January 22, 2015 by tom7890 Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503802 Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 PLEASE IGNORE ABOVE POST firstly i think i need to set the key for the available, booked, partially booked time slots for the day selected and then i will work on the condition for the consecutive days. Now that the time slot table is set up, do i need to get rid of the html time slot table i created? The calendar that i have created needs to be in the database, how to add this to the database. my current code for the calendar is as follows: <?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; } ?> <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 $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{ $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></td>"; } if(($i % 7) == 6 ){ echo "</tr> "; } } ?> <script type="text/javascript"> <?php foreach($jsEvent as $event){ echo $event; } ?> function showForm(){ document.getElementById('timeslots').style.display="block"; }; </script> </table></td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503807 Share on other sites More sharing options...
Barand Posted January 22, 2015 Share Posted January 22, 2015 You only need to record slots that are booked in the database booking table, not the calendar. Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503808 Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 (edited) I don't understand how i link the dates and the time slots together. On the weekends there will be no time slots as these will be closed. Monday to Friday it is open 9am til 6pm. Edited January 22, 2015 by tom7890 Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503811 Share on other sites More sharing options...
Barand Posted January 22, 2015 Share Posted January 22, 2015 Your bookings table should contain date timeslot_id so you then know which slots are booked each day Your booking table would contain the date and the booked timeslot. See my diagram that i posted earlier http://forums.phpfreaks.com/topic/293995-php-sql-calendar-add-events/?do=findComment&comment=1503204 Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503813 Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 Your bookings table should contain date timeslot_id so you then know which slots are booked each day yes it does barand. Now that the time slot table is set up, do i need to get rid of the html time slot table i created? is this the best time to set key, available, booked, partially booked time slots for the selected day Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503814 Share on other sites More sharing options...
Barand Posted January 22, 2015 Share Posted January 22, 2015 Would this be better? it is for a doctors center This is a really frightening thought - their lives in your hands Now that the time slot table is set up, do i need to get rid of the html time slot table i created? You are going to need that for the bookings. What I said is you should hve generated that by looping though the records in the timeslot table and not by hard coding every start and end time. is this the best time to set key, available, booked, partially booked time slots for the selected day You do that when you generate the calendar. Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503815 Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 barand, the next task for me to do is: Retrieve the time slots from the database and show them on the webpage beside the calendar, am i correct? So when the user clicks on any date the time slots will show up. Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503816 Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 barand, the next task for me to do is: Retrieve the time slots from the database and show them on the webpage beside the calendar, am i correct? So when the user clicks on any date the time slots will show up. To retrieve the data from timeslot table in the database i have done the following but i have an error, i am trying to store the data in to a table. The code is: <?php $db = new mysqli('localhost', 'root', '', 'booking'); $sql = "SELECT timeslots_id, start_time, end_time FROM timeslot"; $db->query($sql); if ($db->num_rows > 0) { echo "<table><tr> <th>Time Slot ID</th> <th>Start Time</th> <th>End Time</th></tr>"; while ($row = $result->fetch_assoc()) { echo "<tr><td>" . $row["timeslots_id"]. "</td><td>" . $row["start_time"]. " " . $row["end_time"]. "</td></tr>"; } echo "</table>"; } else { echo "0 results"; } ?> </body> </html> The error is: Notice: Undefined property: mysqli::$num_rows Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503819 Share on other sites More sharing options...
CroNiX Posted January 22, 2015 Share Posted January 22, 2015 The manual is your friend $result = $db->query($sql); if ($result->num_rows) //... Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503821 Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 yes fixed it thanks Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503822 Share on other sites More sharing options...
ginerjm Posted January 22, 2015 Share Posted January 22, 2015 Have you EVER looked at the manual to solve any of your mysqli usage problems? $num_rows does not exist. It is not a local php variable. It is a property of a mysqli result WHICH if you had ever looked at the manual you would see you LEFT out of your code. Change this: $db->query($sql); to this: $results = $db->query($sql); NOW you have a result set to play with which you did not before Now change this: if ($db->num_rows > 0) { to this: if ($result->num_rows > 0) { Note the dollar sign. These would have been very clear to you if you EVER read the manual. PS - for the future - when you post an actual error message it would be nice to point out the line that it is referencing in the message. This one was easy but it won't always be that way. Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503823 Share on other sites More sharing options...
tom7890 Posted January 23, 2015 Author Share Posted January 23, 2015 (edited) @ginerjm thanks for your comments, will take them on board. @Barand, @Cornix As many changes have been made, i think i am on the right the right with you guys valuable comments, so i just want to say thanks As you know i created a timeslots table, when a user clicked on the date the time slot table appeared; now that i am retrieving the time slots from the database how do i change this? So when the user clicks on a date it shows the time slots from the database on the same page The previous code was: <?php $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{ $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></td>"; } if(($i % 7) == 6 ){ echo "</tr> "; } } ?> Edited January 23, 2015 by tom7890 Quote Link to comment https://forums.phpfreaks.com/topic/294112-save-data-to-database-checkboxe-values-checked-or-not/page/2/#findComment-1503827 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.