Jump to content

[SOLVED] Calendar problem


bschultz

Recommended Posts

I have a calendar that draws info from a MYSQL database and spits it out into a table.  It's a combination of several php calendars that I've found.  I do, however, need some help altering it to do more than one moth at a time.  I'd like it to show the next 6 months.

 

Here's the code:

 

<?php

$m = date(m);
$d = date(d);
$y = date(Y); 

    // get this month and this years as an int
    $thismonth = ( int ) date( "m" );
    $thisyear = date( "Y" );
    
    // find out the number of days in the month
    $numdaysinmonth = cal_days_in_month( CAL_GREGORIAN, $thismonth, $thisyear );
    
    // create a calendar object
    $jd = cal_to_jd( CAL_GREGORIAN, date( "m" ),date( 1 ), date( "Y" ) );
    
    // get the start day as an int (0 = Sunday, 1 = Monday, etc)
    $startday = jddayofweek( $jd , 0 );
    
    // get the month as a name
    $monthname = jdmonthname( $jd, 1 )

?>
<table>
    <tr>
        <td colspan="7"><div align="center"><strong><?php echo "$monthname   $thisyear"; ?></strong></div></td>
    </tr>
    <tr>
        <td><strong>Sunday</strong></td>
        <td><strong>Monday</strong></td>
        <td><strong>Tuesday</strong></td>
        <td><strong>Wednesday</strong></td>
        <td><strong>Thursday</strong></td>
        <td><strong>Friday</strong></td>
        <td><strong>Saturday</strong></td>
    </tr>
    <tr>
<?php

    // put render empty cells

    $emptycells = 0;

    for( $counter = 0; $counter <  $startday; $counter ++ ) {
    
        echo "\t\t<td>-</td>\n";
        $emptycells ++;
    
    }
    
    // renders the days
    
    $rowcounter = $emptycells;
    $numinrow = 7;
    
    for( $counter = 1; $counter <= $numdaysinmonth; $counter ++ ) {
    
        $rowcounter ++;
        
        echo "\t\t<td valign='top'>$counter<br />";
//do your normal mysql setup and connection calls  
$dbc = mysql_pconnect('xxx','xxx','xxx');  
mysql_select_db('refs',$dbc);  
//now get stuff from a table  
$sql = "SELECT * FROM calendar WHERE date='$y-$m-$counter'";      	
$rs = mysql_query($sql,$dbc);  
$matches = 0; 
while ($row = mysql_fetch_assoc($rs))  {
$matches++; 
echo "---------------------<br />";
echo "$row[time]"."  <br /><strong>$row[gender]"." $row[visitor]"." at "."$row[home]</strong><br />";
echo "$row[ref1]  "."$row[ref2]  "."$row[ref3]  "."$row[ref4]<br />";
}  
if (! $matches) { 
echo "---------------------"; } echo ""; 



	echo "</td>\n";
        
        if( $rowcounter % $numinrow == 0 ) {
        
            echo "\t</tr>\n";
            
            if( $counter < $numdaysinmonth ) {
            
                echo "\t<tr>\n";
            
            }
        
            $rowcounter = 0;
        
        }
    
    }
    
    // clean up
    $numcellsleft = $numinrow - $rowcounter;
    
    if( $numcellsleft != $numinrow ) {
    
        for( $counter = 0; $counter < $numcellsleft; $counter ++ ) {
        
            echo "\t\t<td>-</td>\n";
            $emptycells ++;
        
        }
    
    }

?>
    </tr>
</table>
</body>
</html>

 

 

Any help to send me in the right direction would be appreciated.

 

Thanks!

Link to comment
Share on other sites

I decided that changing the code to use a GET variable would be much easier than printing out the calendar for six months at a time.  Thanks for the help, though!...

 

For those that want the code:

 

<?php

    // get this month and this years as an int

$thismonth = $_GET["thismonth"];
$thisyear = $_GET["thisyear"];
    
    // find out the number of days in the month
    $numdaysinmonth = cal_days_in_month( CAL_GREGORIAN, $thismonth, $thisyear );
    
    // create a calendar object
    $jd = cal_to_jd( CAL_GREGORIAN, $thismonth, date( 1 ), $thisyear );
    
    // get the start day as an int (0 = Sunday, 1 = Monday, etc)
    $startday = jddayofweek( $jd , 0 );
    
    // get the month as a name
    $monthname = jdmonthname( $jd, 1 )

?>
<table>
    <tr>
        <td colspan="7"><div align="center"><strong>
	<?php 
if($_GET[thismonth]=='01') 
echo('January');
else if($_GET[thismonth]=='02') 
echo('February');
else if($_GET[thismonth]=='03') 
echo('March');
else if($_GET[thismonth]=='04') 
echo('April');
else if($_GET[thismonth]=='05') 
echo('May');
else if($_GET[thismonth]=='06') 
echo('June');
else if($_GET[thismonth]=='07') 
echo('July');
else if($_GET[thismonth]=='08') 
echo('August');
else if($_GET[thismonth]=='09') 
echo('September');
else if($_GET[thismonth]=='10') 
echo('October');
else if($_GET[thismonth]=='11') 
echo('November');
else if($_GET[thismonth]=='12') 
echo('December');
else echo "";

	echo "  $thisyear"; ?></strong></div></td>
    </tr>
    <tr>
        <td><strong>Sunday</strong></td>
        <td><strong>Monday</strong></td>
        <td><strong>Tuesday</strong></td>
        <td><strong>Wednesday</strong></td>
        <td><strong>Thursday</strong></td>
        <td><strong>Friday</strong></td>
        <td><strong>Saturday</strong></td>
    </tr>
    <tr>
<?php

    // put render empty cells

    $emptycells = 0;

    for( $counter = 0; $counter <  $startday; $counter ++ ) {
    
        echo "\t\t<td>-</td>\n";
        $emptycells ++;
    
    }
    
    // renders the days
    
    $rowcounter = $emptycells;
    $numinrow = 7;
    
    for( $counter = 1; $counter <= $numdaysinmonth; $counter ++ ) {
    
        $rowcounter ++;
        
        echo "\t\t<td valign='top'>$counter<br />";
//do your normal mysql setup and connection calls  
$dbc = mysql_pconnect('xxx','xxx','xxx');  
mysql_select_db('refs',$dbc);  
//now get stuff from a table  
$sql = "SELECT * FROM calendar WHERE date='$thisyear-$thismonth-$counter'";      	
$rs = mysql_query($sql,$dbc);  
$matches = 0; 
while ($row = mysql_fetch_assoc($rs))  {
$matches++; 
echo "---------------------<br />";
echo "$row[time]"."  <br /><strong>$row[gender]"." $row[visitor]"." at "."$row[home]</strong><br />";
echo "$row[ref1]  "."$row[ref2]  "."$row[ref3]  "."$row[ref4]<br />";
}  
if (! $matches) { 
echo "---------------------"; } echo ""; 



	echo "</td>\n";
        
        if( $rowcounter % $numinrow == 0 ) {
        
            echo "\t</tr>\n";
            
            if( $counter < $numdaysinmonth ) {
            
                echo "\t<tr>\n";
            
            }
        
            $rowcounter = 0;
        
        }
    
    }
    
    // clean up
    $numcellsleft = $numinrow - $rowcounter;
    
    if( $numcellsleft != $numinrow ) {
    
        for( $counter = 0; $counter < $numcellsleft; $counter ++ ) {
        
            echo "\t\t<td>-</td>\n";
            $emptycells ++;
        
        }
    
    }

?>
    </tr>
</table>
</body>
</html>

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.