Free2Code
 
Time: 2008-12-04, 09:43pm
Calender and SQL
Subject: Calender and SQL  ·  Posted: 2007-04-05, 02:17am
Rank: ? (2)
Member #: 29591
Hello,

I have used a calender from the internet, now i want to build a new function in this script.
A user fill in a form and save his date, i have build up already a connection to connect with the database and get the day,month,year to check.
but i cant display the right day when you got a date.
in line 252 it build up a start...could somebody help me please?

so the question is: how can i display the right date in the calender and make that day red

Code:
  1. <html><center> 
  2. <style>/* Standaard CSS */ 
  3. body,td,th { 
  4.     font-family: Arial, Helvetica, sans-serif; 
  5.     font-size: 10px; 
  6.     color: #333333; 
  7.     padding: 4px; 
  8. body { 
  9.     margin-left: 0px; 
  10.     margin-top: 0px; 
  11.     margin-right: 0px; 
  12.     margin-bottom: 0px; 
  13. a:link { 
  14.     color: #006699; 
  15. a:visited { 
  16.     color: #006699; 
  17. a:hover { 
  18.     color: #FF9900; 
  19. a:active { 
  20.     color: #FF9900; 
  21. .maand_header { 
  22.     font-weight: bold; 
  23.     text-align: center; 
  24. .dag_header { 
  25.     font-style: italic; 
  26.     text-align: center; 
  27.     background-color: #EEE; 
  28. .dagen_dezemaand { 
  29.     text-align: center; 
  30. .weken_tekst { 
  31.     text-align: center; 
  32.     background-color: #EEE; 
  33. .dagen_anderemaand { 
  34.     text-align: center; 
  35.     color: #666666; 
  36.     background-color: #999999; 
  37. .dagen_vandaag { 
  38.     text-align: center; 
  39.     background-color: #3c68af; 
  40. .dagen_afspraak { 
  41.     text-align: center; 
  42.     background-color: #3c68af; 
  43. }</style> 
  44. <?php 
  45. //Database gegevens 
  46. $dbhost 'localhost';      // database host 
  47. $dbuser 'root';      // database user 
  48. $dbpass '';          // database password 
  49. $dbname 'kalender'// database name 
  50. //connect 
  51. $connect mysql_connect($dbhost$dbuser$dbpass) or die(mysql_error()); 
  52. mysql_select_db($dbname) or die(mysql_error()); 
  53. $sql mysql_query("Select * from opgeslagen"); 
  54. $x 0
  55. while($x mysql_num_rows($sql)){ 
  56. $link mysql_fetch_row($sql); 
  57. echo "$link[2],$link[3],$link[4]"
  58. $x++;} 
  59. // Names for the days of the week (currently Dutch): 
  60. $weekday['all'] = array(1=>"ma","di","wo","do","vr","za","zo"); 
  61. // Names for the months of the year (currently Dutch): 
  62. $maand = array(1=>"januari"2=> "februari"3=>"maart"4=> "april"5=> "mei"6=> "juni"7=> "juli"8=> "augustus"9=> "september"10=> "oktober"11=> "november"12=> "december"); 
  63. // Check the user-input, is it numeric and accepted by date(): 
  64. if(($_GET['month'] > 0) && ($_GET['month'] <= 12) && ($_GET['year'] > 1902) && ($_GET['year'] < 2036)) { 
  65.     if (is_numeric($_GET['month'])) { 
  66.         $input $_GET['month']; 
  67.     } 
  68.     if (is_numeric($_GET['year'])) { 
  69.         $yearinput $_GET['year']; 
  70.     } 
  71. } else { 
  72. // If no input of false input, take the current date: 
  73.     $input date('n'); 
  74.     $yearinput date('Y'); 
  75. // The current year and month: 
  76. $currentmonth date('n'); 
  77. $currentyear date('Y'); 
  78. // The timestamp from the first day of the current month: 
  79. $timestamp['start_month'] = mktime(0,0,0,$input,1,$yearinput); 
  80. // Number of days in current month: 
  81. $i_days['month'] = date('t',$timestamp['start_month']); 
  82. // Sets the first day: 
  83. $day['start_day'] = date('j',$timestamp['start_month']); 
  84. // Which day of the week is the first day: 
  85. $weekday['start_day'] = date('N',$timestamp['start_month']); 
  86. // Sets the timestamp for the last day: 
  87. $timestamp['last_month'] = mktime(0,0,0,$input,$i_days['month'],$yearinput); 
  88. // Sets the last day: 
  89. $day['last_day'] = date('j',$timestamp['last_month']); 
  90. // Which day of the week is the last day: 
  91. $weekday['last_day'] = date('N',$timestamp['last_month']); 
  92. // Collect all data (current month only!), from first to last day: 
  93. $i 1
  94. while ($i <= $i_days['month']) { 
  95.     $timestamp['now'] = mktime(0,0,0,$input,$i,$yearinput); 
  96.     $weekday['now'] = date('N',$timestamp['now']);    
  97.     $day['all'][$i] = array($i,$input,$yearinput,$weekday['all'][$weekday['now']]); 
  98.     $i++; 
  99. // Collect data from previous month: 
  100. $i $weekday['start_day']-1
  101. while ($i >= 1) { 
  102.     $i_int $i
  103.     $timestamp['now'] = mktime(0,0,0,$input,$i_int,$yearinput); 
  104.     $weekday['now'] = date('N',$timestamp['now']); 
  105.     $day['now'] = date('j',$timestamp['now']); 
  106.     $month['now'] = date('n',$timestamp['now']); 
  107.     $year['now'] = date('Y',$timestamp['now']); 
  108.     $day['prev']['all'][$i] = array($day['now'],$month['now'],$year['now'],$weekday['all'][$weekday['now']]); 
  109.     $i--; 
  110. // Sort the previous month: 
  111. @sort($day['prev']['all']); 
  112. // Collect data from next month: 
  113. if ($weekday['last_day'] < 7) { 
  114.     $i 1
  115.     while ($i $weekday['last_day'] <= 7) { 
  116.         $i_int $day['last_day'] + $i
  117.         $timestamp['now'] = mktime(0,0,0,$input,$i_int,$yearinput); 
  118.         $weekday['now'] = date('N',$timestamp['now']); 
  119.         $day['now'] = date('j',$timestamp['now']); 
  120.         $month['now'] = date('n',$timestamp['now']); 
  121.         $year['now'] = date('Y',$timestamp['now']); 
  122.         $day['next']['all'][$i] = array($day['now'],$month['now'],$year['now'],$weekday['all'][$weekday['now']]); 
  123.         $i++; 
  124.     } 
  125. } else { 
  126.     $day['next']['all'] == ""
  127. // Set the links to the previous en next month: 
  128. $prev $input 1
  129. $yearprev $yearinput
  130. if ($input == 1) { 
  131.     $prev 12
  132.     $yearprev $yearinput 1
  133. $next $input 1
  134. $yearnext $yearinput
  135. if ($input == 12) { 
  136.     $next 1
  137.     $yearnext $yearinput 1
  138. // Create a default header: 
  139. $header ""
  140. $header .= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
  141. $header .= "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
  142. $header .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n"
  143. $header .= "<title>Kalender</title>\n"
  144. $header .= "<link href=\"default.css\" rel=\"stylesheet\" type=\"text/css\" />\n"
  145. $header .= "</head>\n"
  146. // Create the body: 
  147. $body ""
  148. $body .= "<body>\n"
  149. // Create the table: 
  150. $table ""
  151. $table .= "<table cellspacing=\"0\" border=\"0\">\n"
  152. // The tableheader: 
  153. $table .= "<tr>\n"
  154. $table .= "<td class=\"maand_header\" height=\"30\" width=\"25\"><a href=\"kalender.php?month=".$prev."&amp;year=".$yearprev."\">&lt;</a></td>\n"
  155. $table .= "<td class=\"maand_header\" height=\"30\" colspan=\"6\">".ucfirst($maand[$input])." ".$yearinput."</td>\n"
  156. $table .= "<td class=\"maand_header\" height=\"30\" width=\"25\"><a href=\"kalender.php?month=".$next."&amp;year=".$yearnext."\">&gt;</a></td>\n"
  157. $table .= "</tr>\n"
  158. // The days of the week: 
  159. $table .= "<tr>\n"
  160. $table .= "<td class=\"dag_header\" height=\"30\" width=\"25\">Wk:</td>\n"
  161. $i 1
  162. while ($i <= count($weekday['all'])) { 
  163.     $table .= "<td class=\"dag_header\" height=\"30\" width=\"25\">".ucfirst($weekday['all'][$i])."</td>\n"
  164.     $i++; 
  165. // Total days to display this month: 
  166. $day['total_prev'] = count($day['prev']['all']); 
  167. $day['total_all'] = count($day['all']); 
  168. $day['total_next'] = count($day['next']['all']); 
  169. $day['total'] = $day['total_prev'] + $day['total_all'] + $day['total_next']; 
  170. // Create a array with the days in the correct order: 
  171. $i 0
  172. $i_int 0
  173. while ($i_int <= $day['total_prev']) { 
  174.     $display[$i] = $day['prev']['all'][$i_int]; 
  175.     $i_int++; 
  176.     $i++; 
  177. $i_int 0
  178. while ($i_int <= $day['total_all']) { 
  179.     $display[$i] = $day['all'][$i_int]; 
  180.     $i_int++; 
  181.     $i++; 
  182. if ($weekday['last_day'] < 7) { 
  183.     $i_int 0
  184.     while ($i_int <= $day['total_next']) { 
  185.         $display[$i] = $day['next']['all'][$i_int]; 
  186.         $i_int++; 
  187.         $i++; 
  188.     } 
  189. // Display the days: 
  190. $i 0
  191. while ($i count($display)) { 
  192.     $i_int 0
  193.     if (($display[$i][0] == "") && ($display[$i][3] == "")) { 
  194.         $timestamp['now'] = mktime(0,0,0,$display[6][1],1,$display[6][2]); 
  195.     } else { 
  196.         $timestamp['now'] = mktime(0,0,0,$display[$i][1],$display[$i][0],$display[$i][2]); 
  197.     } 
  198.     $week['now'] = date('W',$timestamp['now']); 
  199.     $table .= "<tr>\n"
  200.     $table .= "<td class=\"weken_tekst\" height=\"30\" width=\"25\">".$week['now']."</td>\n"
  201.     while ($i_int 7) { 
  202.         if (is_array($display[$i])) { 
  203.             if ($display[$i][1] == $input) { 
  204.                 $style "dagen_dezemaand"
  205.             } else { 
  206.                 $style "dagen_anderemaand"
  207.             } 
  208.             if (($display[$i][0] == date('j')) && ($display[$i][1] == date('m')) && ($yearinput == $currentyear)) { 
  209.                 $style "dagen_vandaag"
  210.             } 
  211.             $table .= "<td class=\"".$style."\" height=\"30\" width=\"25\">".$display[$i][0]."</td>\n"
  212.             $i_int++; 
  213.         } 
  214.         $i++; 
  215.     } 
  216.         while ($i_int 7) { 
  217.         if (is_array($display[$i])) { 
  218.             if (($display[$i][0] == $link[2]) && ($currentmonth == $link[3]) && ($yearinput == $link[4])) { 
  219.                 $style "dagen_afspraak"
  220.             } 
  221.             $table .= "<td class=\"".$style."\" height=\"30\" width=\"25\">".$display[$i][0]."</td>\n"
  222.             $i_int++; 
  223.         } 
  224.         $i++; 
  225.     } 
  226.     $table .= "</tr>\n"
  227. // The tablefooter: 
  228. $table .= "<tr>\n"
  229. $table .= "<td class=\"maand_header\" height=\"30\" width=\"25\">&nbsp;</td>\n"
  230. $table .= "<td class=\"maand_header\" height=\"30\" colspan=\"6\"><a href=\"kalender.php?month=".$currentmonth."&amp;year=".$currentyear."\">naar huidige maand</a></td>\n"
  231. $table .= "<td class=\"maand_header\" height=\"30\" width=\"25\">&nbsp;</td>\n"
  232. $table .= "</tr>\n"
  233. $table .= "</table>\n"
  234. $body .= $table
  235. $body .= "</body>\n"
  236. // Create a default footer: 
  237. $footer ""
  238. $footer .= "</html>\n"
  239. // Display the header, body and footer: 
  240. echo $header
  241. echo $body
  242. echo $footer
  243. ?> </center></html>


 
  Reply to this ·  Post link ·  Top
Subject: Re: Calender and SQL  ·  Posted: 2007-04-05, 06:12am
Rank: ? (767)
Member #: 11085
I'm not certain about what you are asking, but if you are asking how to make the current day on the calendar red, take the current day of the month, which your script has already figured out, and check against the day result from the database, if the days are equal, set the color to red, if not, to black (or some other color of your choosing).

Also, for security purposes, you most likely do not want to use the root user for MySQL, especially with no password.

- relpats_eht
 
  Reply to this ·  Post link ·  Top
Subject: Re: Calender and SQL  ·  Posted: 2007-04-05, 10:35am
Rank: ? (2)
Member #: 29591
Hey thanx for the fast responds!

Yes, you answered my question, the day when a user have submitted a date should be red colored.

I have tried that already by myself but i cant fix it, is it possible for you (or somebody else) to fix that for me?rolleyes
On the line 252 till 259 have i tried...also in the CSS is it changed.
$link[2] = day
$link[3] = month
$link[4] = year

if that is displayed in the current month(where the user looked at that time) and the user got of a day a date, it should colored red.

Ow and btw, on my real server i used a password so it is protected. But thanx for your tip! I presiate that!

 
  Reply to this ·  Post link ·  Top
Subject: Re: Calender and SQL  ·  Posted: 2007-04-05, 11:52am
Rank: ? (767)
Member #: 11085
Well, I can tell you why lines 252-259 are not running without to much deciphering, but in all honesty, not to be rude, you really ought to clean up your code. A problem such as this could and should be done in no more than about fifty lines, whereas you are using well over one hundred. Remove all redundancy and reduce code where less is needed. Also, your style could use a bit of work. Whatever style you happen to choose, stick to it and remain consistent, and above all, make sure your code is easily readable via descriptive, yet short, variable names. You know your are coding well when you can still understand your code easily after six months of absence.

Comments on code aside, your problem is that the while loop on line 252 only runs while the variable $i_int is less than seven, meanwhile, the while loop directly above line 252 runs while the variable $i_int is less than seven. This means that by line 252, $i_int must at least be seven, therefore, lines 252-259 will not run. The solution is to simply set it to zero before the next while loop.

- relpats_eht
 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

icons