Jump to content

save data to database checkboxe values checked or not


tom7890

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by tom7890
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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 by tom7890
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.