Page 1 of 1

Calendar

Posted: Mon Jun 12, 2006 10:25 pm
by trock74
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm trying to create a calender, but I can not line my dates up on the same row. It list the dates on the correct day, but it puts is date in a new table row which I'm not trying to do. Here is my code listed below and the out put. What I'm I doing wrong, any suggestion?

Code:

Code: Select all

<html>
<title>Test Calendar</title>
<head></head>
<body>
<table border="0" cellpadding="2" cellspacing="2" bgcolor="#ffffff">
<tr valign="top">
<td align=center>Sun</td>
<td align=center>Mon</td>
<td align=center>Tue</td>
<td align=center>Wed</td>
<td align=center>Thu</td>
<td align=center>Fri</td>
<td align=center>Sat</td>
</tr>
<?php
// set numberic representation of a month
$nmon = date("n");
// set number of days in the given month
$ndays = date("t");
// set full numeric representation of year, 4 digits
$nyear = date("Y");
echo "<tr>\n";
for ($i = 1; $i <= $ndays; $i++) { // loop the number of days in the month
        for ($j = 0; $j <= 6; $j++) { // loop to find which day is the 1st on
                $dno = date("w", mktime(0, 0, 0, $nmon, $i, $nyear)); // capture what day is the 1st on
                if ($dno == $j) { // if
                        echo "<td align=center>$i</td>\n";
                }
                else {
                echo "<td>&nbsp;</td>\n";
                }
        }
        echo "</tr>\n";
}
?>
</body>
</html>
Output:

Code: Select all

Sun Mon Tue Wed Thu Fri Sat 
                              1     
                                    2   
                                         3 
4             
      5           
              6         
                      7       
                              8     
                                    9   
                                         10 
11             
      12           
              13         
                      14       
                              15     
                                    16   
                                         17 
18             
      19           
              20         
                      21       
                              22     
                                    23   
                                         24 
25             
      26           
              27         
                      28       
                              29     
                                    30

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jun 12, 2006 10:39 pm
by hawleyjr
Your opening <tr> tag is outside of the loop your closing tag is inside the loop:

Code: Select all

echo "<tr>\n";
for ($i = 1; $i <= $ndays; $i++) { // loop the number of days in the month
        for ($j = 0; $j <= 6; $j++) { // loop to find which day is the 1st on
        }
        echo "</tr>\n"; 
}

Posted: Mon Jun 12, 2006 10:57 pm
by feyd
This works as expected

Code: Select all

<html>
<title>Test Calendar</title>
<head></head>
<body>
<table border="0" cellpadding="2" cellspacing="2" bgcolor="#ffffff">
<tr valign="top">
<td align=center>Sun</td>
<td align=center>Mon</td>
<td align=center>Tue</td>
<td align=center>Wed</td>
<td align=center>Thu</td>
<td align=center>Fri</td>
<td align=center>Sat</td>
</tr>
<?php
// set numberic representation of a month
$nmon = date("n");
// set number of days in the given month
$ndays = date("t");
// set full numeric representation of year, 4 digits
$nyear = date("Y");

//	Reset the start to Sunday the week of the first.
$i = 1 - date('w', mktime(0, 0, 0, $nmon, 1, $nyear));
while($i <= $ndays) { // loop the number of days in the month
	echo "<tr>\n";
	for ($j = 0; $j <= 6; $j++, $i++) { // loop to find which day is the 1st on
		if ($i >= 1 and $i <= $ndays) { // if the day number is in the valid range
			echo "<td align=\"center\">$i</td>\n";
		}
		else {
			echo "<td>&nbsp;</td>\n";
		}
	}
	echo "</tr>\n";
}

?>
</body>
</html>

Calendar

Posted: Tue Jun 13, 2006 3:36 pm
by trock74
Thanks guys, it works like a charm.