Alexhoward Posted November 24, 2008 Share Posted November 24, 2008 Hi Guys! I'm trying to create a simple calendar booking form, just for a bit of an education at the moment... I'm trying to display one result for if we're booked, and one for when we're available.. I've got the calendar working but am having trouble with the "we're booked" part.. the problem being that it will only pull back one result, and if i move the while loop further it will duplicate cells, or the entire table... any suggestions would be much appreciated! thanks in advance <?php include ('config.php'); //connect to the mysql server $link = mysql_connect($host, $db, $pass) or die('Could not connect to mysql because ' . mysql_error()); //select the database mysql_select_db($db) or die('Could not select database because ' . mysql_error()); $query = ' SELECT * FROM book'; $result = mysql_query($query); while ($array= mysql_fetch_array($result)) { $booked = $array['date_req']; } //This gets today's date if(!isset($_GET['date'])) { $_GET['date'] = time() ; } $date = $_GET['date'] ; //This puts the day, month, and year in seperate variables $day = date('d', $date) ; $month = date('m', $date) ; $year = date('Y', $date) ; //Here we generate the first day of the month $first_day = mktime(0,0,0,$month, 1, $year) ; //This gets us the month name $title = date('F', $first_day) ; //Here we find out what day of the week the first day of the month falls on $day_of_week = date('D', $first_day) ; //Once we know what day of the week it falls on, we know how many blank days occure before it. //If the first day of the week is a Sunday then it would be zero switch($day_of_week){ case "Sun": $blank = 0; break; case "Mon": $blank = 1; break; case "Tue": $blank = 2; break; case "Wed": $blank = 3; break; case "Thu": $blank = 4; break; case "Fri": $blank = 5; break; case "Sat": $blank = 6; break; } //We then determine how many days are in the current month $days_in_month = cal_days_in_month(0, $month, $year) ; $days_left = ($days_in_month - $day) ; $days_gone = $day ; $next = $date + (($days_left + 1) * 24 * 60 * 60) ; $prev = $date - (($days_gone + 1) * 24 * 60 * 60) ; //Here we start building the table heads echo "<table border=1 width=700>"; echo "<tr><th colspan='7'><a href=availability.php?date=$prev style='text-decoration:none; color:#000000;'><<</a> $title $year <a href=availability.php?date=$next style='text-decoration:none; color:#000000';>>></a></th></tr>"; echo "<tr><td width=100>Sun</td><td width=100>Mon</td><td width=100>Tue</td><td width=100>Wed</td><td width=100>Thur</td><td width=100>Fri</td><td width=100>Sat</td></tr>"; //This counts the days in the week, up to 7 $day_count = 1; echo "<tr>"; //first we take care of those blank days while ( $blank > 0 ) { echo "<td bgcolor='#CCCCCC'> </td>"; $blank = $blank-1; $day_count++; } //sets the first day of the month to 1 $day_num = 1; //count up the days, untill we've done all of them in the month while ( $day_num <= $days_in_month ) { $booked_date = explode('-', $booked) ; if($booked_date[0] == $year && $booked_date[1] == $month && $booked_date[2] == $day_num ) { $book = 'Apologies we are booked <br> <a href=who.php>more info</a>' ; $colour = 'bgcolor=#A4A4FF' ; } else { $book = 'Available<p><input type=submit name=book value=book us>' ; $colour = 'bgcolor=#A6FFA6' ; } echo " <form> <td height='100' width='100' align='left' valign='top' $colour> <strong>$day_num</strong> <input type='hidden' name='when' value='$year-$month-$day_num'> <input type='hidden' name='date' value='$date'> <p> <div align='center'> $book </div> </td> </form>"; $day_num++; $day_count++; //Make sure we start a new row every week if ($day_count > 7) { echo "</tr><tr>"; $day_count = 1; } } //Finaly we finish out the table with some blank details if needed while ( $day_count >1 && $day_count <=7 ) { echo "<td bgcolor='#CCCCCC'> </td>"; $day_count++; } echo "</tr></table>"; mysql_close($link) ; ?> Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted November 24, 2008 Author Share Posted November 24, 2008 friendly push... Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted November 24, 2008 Author Share Posted November 24, 2008 OK, let me narrow things down a bit... the calendars fine, and it displays ok (not great design but it works) i have an MySQL table that stores the date required i then want to be able to pull the dates, and display a "booked" results in that day on the calendar. the issue lies here : <?php //connect to the mysql server $link = mysql_connect($host, $db, $pass) or die('Could not connect to mysql because ' . mysql_error()); //select the database mysql_select_db($db) or die('Could not select database because ' . mysql_error()); $query = ' SELECT * FROM book'; $result = mysql_query($query); while ($array= mysql_fetch_array($result)) { $booked = $array['date_req']; } ?> as i am only pulling the loop once to stop the table being duplicated, or a cell i only get the last result in the table, i only get on booking displayed... can anyone help me to do this properly? thank again! Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted November 24, 2008 Author Share Posted November 24, 2008 can anyone please help... i'm soo stuck..... Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted November 25, 2008 Author Share Posted November 25, 2008 Sorry to push this again, but can anyone please help me...? Quote Link to comment Share on other sites More sharing options...
waynew Posted November 25, 2008 Share Posted November 25, 2008 Forgive me if I'm wrong, but if there are more than one bookings, the only thing this piece of code below will achieve is overwrite the variable $booked each time. while ($array= mysql_fetch_array($result)) { $booked = $array['date_req']; } Try using an array: $booked = array(); while ($array= mysql_fetch_array($result)) { array_push($booked,$array['date_req']); // adding each date_req to array } Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted November 25, 2008 Author Share Posted November 25, 2008 Hey! thanks fot the reply i'm getting the error: array_push() [function.array-push]: First argument should be an array with that... maybe i should try foreach as i suggested to the other guy... didn't think of it before?!? would that work? Quote Link to comment Share on other sites More sharing options...
waynew Posted November 25, 2008 Share Posted November 25, 2008 Check your code for typos. Did you declare the array like I did? Did you spell it incorrectly? Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted November 25, 2008 Author Share Posted November 25, 2008 Hi, i replaced the code exactly as you said above.... Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted November 25, 2008 Author Share Posted November 25, 2008 am now getting the error : Array to string conversion Quote Link to comment Share on other sites More sharing options...
simonrs Posted November 25, 2008 Share Posted November 25, 2008 Try: $booked = array(); while(condition) { $booked[] = 'item'; } Obviously adapt condition and 'item' to suit your code. Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted November 25, 2008 Author Share Posted November 25, 2008 Hi simonrs, thanks for the help. however i'm not sure what you mean... Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted November 25, 2008 Author Share Posted November 25, 2008 $booked = array(); while(condition) { $booked[] = 'item'; } alright so i've got this working like : $booked = array(); while($array= mysql_fetch_array($result)) { $booked[] = $array['date_req']; } but i can't get them all at once, therefore i can only do : $booked_date = explode('-', $booked[0]) ; to pull one result at a time... somehow i need to pull them all out at once in one loop and display the booked dates in the correct dates on the calendar... thanks for getting me this far, any other suggestions...? Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted November 26, 2008 Author Share Posted November 26, 2008 can anyone help...? or point me in the right direction... Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted November 26, 2008 Author Share Posted November 26, 2008 I've sorted it!!! So what i done was use the loop that gets the days, then selected by that day to see if there was a booking. if the num rows come back with 1 (as you can only book one on a given day) then = "Booked" if zero Then = "Available!" Woo!! Quote Link to comment 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.