rascle Posted October 10, 2009 Share Posted October 10, 2009 Hi i have created a calendar from a table: Calendar: October 2009 <table width=300 border="1"> <tr> <th><a href="calendar.php"><</a></th> <th>M</th> <th>T</th> <th>W</th> <th>T</th> <th>F</th> <th>S</th> <th>S</th> <th><a href="calendar.php?nov09">></a></th> </tr> <tr> <td></td> <td> </td> <td> </td> <td> </td> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td></td> <td>5 </td> <td>6</td> <td>7 </td> <td>8 </td> <td>9 </td> <td>10 </td> <td>11 </td> </tr> <tr> <tr> <td></td> <td>12 </td> <td>13 </td> <td>14</td> <td>15 </td> <td>16 </td> <td>17 </td> <td>18 </td> </tr> <tr> <td></td> <td>19 </td> <td>20 </td> <td>21 </td> <td>22</td> <td>23 </td> <td>24 </td> <td>25 </td> </tr> <tr> <td></td> <td>26 </td> <td>27 </td> <td>28 </td> <td>29 </td> <td>30 </td> <td>31 </td> <td> </td> </tr> </table> The table works fine. But i have a mysql table and it has some dates in it, so for example it has 01102009 which is the 1st October 2009 and it has another date eg. 12102009 which is the 12th October 2009 etc. I want it so that the table recognises the dates and the dates are shown in blue. I was going to do this like this: $booked = mysql_query("SELECT * FROM calendar WHERE `company` = '$logged[company]'"); $booked = mysql_fetch_array($booked); echo' <table border="1"> <tr> <td>M</td> <td>T</td> <td>W</td> <td>T</td> <td>F</td> <td>S</td> <td>S</td> </tr> <tr> <td>'; if($booked[datebooked] == "01102009"){ echo"<font color='blue'>01</font>"; } else{ echo"01"; } echo'</td> <td>'; if($booked[datebooked] == "02102009"){ echo"<font color="blue">02</font>"; } else{ echo"02"; } echo'</td> <td>03</td> <td>04</td> <td>05</td> </tr> </table> '; And so on, this works the first time so 01 would be blue, but 12 would be normal, i know this is something to do with it only being able to select one at a time, but i dont know how to fix it, any ideas. I could maybe use a while loop if anyone hasnt got any better ideas??? Thanks In Advance Rhys Quote Link to comment Share on other sites More sharing options...
Garethp Posted October 10, 2009 Share Posted October 10, 2009 Ok, no offense, but you're doing this completely the wrong way. You should be making it so that the table is generated automatically, rather than you typing it up. Anyway, show us the code that you would put in for 12, and let's see why it's not working Quote Link to comment Share on other sites More sharing options...
rascle Posted October 10, 2009 Author Share Posted October 10, 2009 I would like to have it automatically generated but i dont know how to do it. And for the 2th is the same as the rest would be: <td>'; if($booked[datebooked] == "11102009"){ echo'<font color="blue">11</font>'; } else{ echo"11"; } echo'</td> <td>'; if($booked[datebooked] == "12102009"){ echo'<font color="blue">12</font>'; } else{ echo"12"; } echo'</td> <td>'; if($booked[datebooked] == "13102009"){ echo'<font color="blue">13</font>'; } else{ echo"13"; } Quote Link to comment Share on other sites More sharing options...
Garethp Posted October 10, 2009 Share Posted October 10, 2009 Change echo"12"; to echo $booked[datebooked]; Now, for Automated calanders, you want to look up the date and mktime functions. I'm bored atm, so I might just make one for you, and you can see how it's done Quote Link to comment Share on other sites More sharing options...
rascle Posted October 10, 2009 Author Share Posted October 10, 2009 If you can make me one that would be great thanks. But when you said "Change echo"12"; to echo $booked[datebooked];" i dont see what you mean... i cant see this doing anything to the script other than changing the date to 14102009??? Thanks Quote Link to comment Share on other sites More sharing options...
Garethp Posted October 10, 2009 Share Posted October 10, 2009 It's for informational purposes. You said if($booked[datebooked] == "12102009") doesn't work for the twelthed day, correct? So we want to know what it does equal, if not 12102009. If we know what it DOES echo, then we know the problem Quote Link to comment Share on other sites More sharing options...
rascle Posted October 10, 2009 Author Share Posted October 10, 2009 Yes it echoes 01102009 which is the first row in my MySQL calendar table, i have another row which is the 12102009 row but this in under the row (01102009). Quote Link to comment Share on other sites More sharing options...
rascle Posted October 10, 2009 Author Share Posted October 10, 2009 So it is only reading the first row and not the others. If i can get it to read the others then the script should work! Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted October 10, 2009 Share Posted October 10, 2009 $days_in_month = 31; $booked_date = substr($booked['datebooked'],0,2); for($i = 1; $i < $days_in_month; $i++) { echo '<td>'; $num = str_pad($i,2,'0',STR_PAD_LEFT); echo ($booked_date == $i) ? '<font color="blue">'.$num.'</font>' : $num; echo '</td>'; } That should get you started Quote Link to comment Share on other sites More sharing options...
rascle Posted October 10, 2009 Author Share Posted October 10, 2009 Thanks for that, i cant really understand what some of the code does lol! How would i implement that into a table? and what would it do if the month only has 30 days in etc??? Thanks anyway Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted October 10, 2009 Share Posted October 10, 2009 That was the whole reason for implementing the $days_in_month, so that you can set that before the for loop depending on the month Quote Link to comment Share on other sites More sharing options...
Garethp Posted October 10, 2009 Share Posted October 10, 2009 I must have been really bored <?php $Month = 10; $Year = 2009; $i = 1; $num = cal_days_in_month(CAL_GREGORIAN, $Month, $Year); echo "<table> <tr> <td> Mon </td> <td> Tue </td> <td> Wed </td> <td> Thur </td> <td> Fri </td> <td> Sat </td> <td> Sun </td> </tr>"; $booked['datebooked'] = "12102009"; $i = 1; $Timestamp = mktime(0, 0, 0, $Month, 1, $Year); $Day = date("D", $Timestamp); echo date("N", $Timestamp); while($i < date("N", $Timestamp)) { $i ++; echo "<td> </td>"; } if(date("N", $Timestamp) == 7) { echo "</tr><tr>"; } for($i = 1; $i <= $num; $i++) { $Timestamp = mktime(0, 0, 0, $Month, $i, $Year); $Day = date("d", $Timestamp); echo "<td>"; $Fulldate = $Day . $Month . $Year; if($booked['datebooked'] == $Fulldate) { echo "<font color='blue'>$Day</font>"; } else { echo $Day; } echo "</td>"; if(date("D", $Timestamp) == "Sun") { echo "</tr>"; if($i != $num) { echo "<tr>"; } } } echo "</table>"; ?> Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted October 10, 2009 Share Posted October 10, 2009 lol you must have Quote Link to comment Share on other sites More sharing options...
rascle Posted October 10, 2009 Author Share Posted October 10, 2009 Brilliant thanks! Quote Link to comment Share on other sites More sharing options...
rascle Posted October 10, 2009 Author Share Posted October 10, 2009 If i change the code to: <?php $Month = 10; $Year = 2009; $i = 1; $num = cal_days_in_month(CAL_GREGORIAN, $Month, $Year); echo "<table> <tr> <td> Mon </td> <td> Tue </td> <td> Wed </td> <td> Thur </td> <td> Fri </td> <td> Sat </td> <td> Sun </td> </tr>"; $booked = mysql_query("SELECT * FROM calendar WHERE `companyname` = '$logged[companyname]'"); $booked = mysql_fetch_array($booked); echo $booked['datebooked']; $i = 1; $Timestamp = mktime(0, 0, 0, $Month, 1, $Year); $Day = date("D", $Timestamp); echo date("N", $Timestamp); while($i < date("N", $Timestamp)) { $i ++; echo "<td> </td>"; } if(date("N", $Timestamp) == 7) { echo "</tr><tr>"; } for($i = 1; $i <= $num; $i++) { $Timestamp = mktime(0, 0, 0, $Month, $i, $Year); $Day = date("d", $Timestamp); echo "<td>"; $Fulldate = $Day . $Month . $Year; if($booked['datebooked'] == $Fulldate) { echo "<font color='blue'>$Day</font>"; } else { echo $Day; } echo "</td>"; if(date("D", $Timestamp) == "Sun") { echo "</tr>"; if($i != $num) { echo "<tr>"; } } } echo "</table>"; ?> Note i changed $booked['datebooked'] = "12102009"; to $booked = mysql_query("SELECT * FROM calendar WHERE `companyname` = '$logged[companyname]'"); $booked = mysql_fetch_array($booked); echo $booked['datebooked']; it doesnt work. What do i do to get the dates from the MySQL table to show on the calendar??? Thanks Again Rhys Quote Link to comment Share on other sites More sharing options...
rascle Posted October 10, 2009 Author Share Posted October 10, 2009 Sorry it does work but it only displays one row from the table. So in my table i have the date 14102009 and also another row 22102009. But the calendar only shows 14102009 (the first row) as blue. Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted October 10, 2009 Share Posted October 10, 2009 My first advice would be learn how to use simple mysql functions, since you clearly don't know what you're doing Check out a few tutorials. I recommend http://www.phpvideotutorials.com/lesson13/ http://www.phpvideotutorials.com/lesson14/ or http://www.w3schools.com/php/php_mysql_intro.asp Quote Link to comment Share on other sites More sharing options...
rascle Posted October 10, 2009 Author Share Posted October 10, 2009 Ok i have read the w3school tutorial and the only thing i can think of doing is to create a while loop to make see if the date is in the table and if it is then to change it to the colour blue. But how would i write this in the calendar script???? Thanks Quote Link to comment Share on other sites More sharing options...
Garethp Posted October 10, 2009 Share Posted October 10, 2009 rather than if($booked['datebooked'] == $Fulldate) use if(in_array($Fulldate, $booked)) $booked is just an array of rows from the mysql table. So just check if $Fulldate is in it Quote Link to comment Share on other sites More sharing options...
rascle Posted October 10, 2009 Author Share Posted October 10, 2009 Ok thanks i am currently using: <?php include "headinfo.php"; $Month = 10; $Year = 2009; $i = 1; $num = cal_days_in_month(CAL_GREGORIAN, $Month, $Year); echo "<table> <tr> <td> Mon </td> <td> Tue </td> <td> Wed </td> <td> Thur </td> <td> Fri </td> <td> Sat </td> <td> Sun </td> </tr>"; $booked = mysql_query("SELECT * FROM calendar WHERE `companyname` = '$logged[companyname]'"); $booked = mysql_fetch_array($booked); $i = 1; $Timestamp = mktime(0, 0, 0, $Month, 1, $Year); $Day = date("D", $Timestamp); echo date("N", $Timestamp); while($i < date("N", $Timestamp)) { $i ++; echo "<td> </td>"; } if(date("N", $Timestamp) == 7) { echo "</tr><tr>"; } for($i = 1; $i <= $num; $i++) { $Timestamp = mktime(0, 0, 0, $Month, $i, $Year); $Day = date("d", $Timestamp); echo "<td>"; $Fulldate = $Day . $Month . $Year; if(in_array($Fulldate, $booked)) { echo "<font color='blue'>$Day</font>"; } else { echo $Day; } echo "</td>"; if(date("D", $Timestamp) == "Sun") { echo "</tr>"; if($i != $num) { echo "<tr>"; } } } echo "</table>"; ?> But i am still only getting 14 (the first row) turning blue!!!! What am i doing wrong??! Thanks Rhys Quote Link to comment Share on other sites More sharing options...
rascle Posted October 10, 2009 Author Share Posted October 10, 2009 Any Ideas??? Quote Link to comment Share on other sites More sharing options...
Garethp Posted October 10, 2009 Share Posted October 10, 2009 after $booked = mysql_fetch_array($booked); Put echo "<pre>"; print_r($booked); exit(); And then tell me what the page displays Quote Link to comment Share on other sites More sharing options...
rascle Posted October 10, 2009 Author Share Posted October 10, 2009 This is the output: Mon Tue Wed Thur Fri Sat Sun Array ( [0] => ABC [companyname] => ABC [1] => Jose [name] => Jose [2] => 19102009 [datebooked] => 19102009 ) Mon Tue Wed Thur Fri Sat Sun it does not display the dates Quote Link to comment Share on other sites More sharing options...
rascle Posted October 10, 2009 Author Share Posted October 10, 2009 PRINT SHOT [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
rascle Posted October 10, 2009 Author Share Posted October 10, 2009 Any idea on what i should do??? Thanks 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.