Jump to content

[SOLVED] Calendar in php/mysql


rockindano30

Recommended Posts

Hello all,

 

i'm working on a calendar actually a event calendar. a calendar that when you hover over a date that has an event displays a hovering box with event info. now i have it working to where it displays the calendar and grabs the info from db and when hover over a date box appears but, my problem is all days are links and box has all the events in the db for every day.

 

this is my code for any suggestions or help

 

        <h3 style="font-family:palatino linotype; color:#9e865f">Calendar</h3><br />
<?php 
        // Get values from query string
        $day = $_GET["day"];
        $month = $_GET["month"];
        $year = $_GET["year"];
        $sel = $_GET["sel"];
        $what = $_GET["what"]; 
        
        
        if($day == "")
        $day = date("j");
        
        if($month == "")
        $month = date("m");
        
        if($year == "")
        $year = date("Y"); 
        
        $currentTimeStamp = strtotime("$year-$month-$day");
        $monthName = date("F", $currentTimeStamp);
        $numDays = date("t", $currentTimeStamp);
        $counter = 0;
        $numEventsThisMonth = 0;
        $hasEvent = false;
        $todaysEvents = ""; 
        ?>
        <table width="100%" border='0' cellspacing='0' cellpadding='0'>
          <tr align="center">
            <td width="87" colspan='1' align="center"><input type='button' value='<<' onClick='goLastMonth(<?php echo $month . ", " . $year; ?>)'> 
            </td>
            <td colspan='5' align="center"><span class='title'><?php echo $monthName . " " . $year; ?></span><br>
            </td>
            <td width="87" colspan='1' align='center'><input type='button' value='>>' onClick='goNextMonth(<?php echo $month . ", " . $year; ?>)'>
             </td>
          </tr>
          <tr>
            <td class='style2'>S</td>
            <td width='80' class='style2'>M</td>
            <td width='80' class='style2'>T</td>
            <td width='80' class='style2'>W</td>
            <td width='80' class='style2'>T</td>
            <td width='80' class='style2'>F</td>
            <td class='style2'>S</td>
          </tr>
          <?php
        $numDays = date("t", $currentTimeStamp); 
        for($i = 1; $i < $numDays+1; $i++, $counter++)
        {
        $timeStamp = strtotime("$year-$month-$i");
        if($i == 1)
        {
        // Workout when the first day of the month is
        $firstDay = date("w", $timeStamp);
        
        for($j = 0; $j < $firstDay; $j++, $counter++)
        echo "<td align='center'> </td>"; 
        
        } //end if 
        if(!($db = @ mysql_connect('localhost', 'user', 'pass')))
                        {
                            echo 'Error: Could not connect to our database sorry for any inconvience.<br /> Please try at a later time.';
                            exit;
                        }
                        //select which database you want to edit
                        mysql_select_db("db_name_here");

//                        $event_id = $_GET["event_id"];
					$eventdate=$_GET["dateevent"]; 
                        
                        
                        if(!isset($eventdate))
                        {
                                    $query = "SELECT group_concat(title separator ' - ') as title FROM calendar";
                                    $result = mysql_query($query);

                                    while($r=mysql_fetch_array($result))
                                    {	
								   //the format is $variable = $r["nameofmysqlcolumn"];
									$event_id=$r["event_id"];
									$date=$r["dateevent"];
									$title=$r["title"];

									$eventdate = strtotime($date);
						?>
           <?php                        
           //prints out all days here...................                     
        if($counter % 7 == 0)
        print "</tr><tr align='center'>";
        if(date("w", $timeStamp) == 0 || date("w", $timeStamp) == 6)
	print "<td width='50' class='weekend' align='left'>$i</td>";
        else
        if($i == date("d") && $month == date("m") && $year == date("Y"))
        print "<td width='50' class='today' align='left'><div id=\"links\"><a href\"\">$i<span>$title</span></a></div></td>";
        else		
        print "<td width='50' class='normal' align='left'><div id=\"links\"><a href\"\">$i<span>$title</span></a></div></td>";  
							}
                            }//end if///////////////////////////
        }//end of for loop
        ?>

 

any help???

Link to comment
Share on other sites

Within your for loop that is iterating through all the days of the month, for each day you are fetching all the events from the database and outputting them.

 

Try replacing your for loop that is currently iterating through the days of the month with something like this (NOTE: this code has not been tested at all and might fail miserably or require major adjustments!):

if(!($db = @mysql_connect('localhost', 'user', 'pass')))
{
echo 'Error: Could not connect to our database sorry for any inconvience.<br /> Please try at a later time.';
exit;
}
mysql_select_db("db_name_here");
for($i = 1; $i < $numDays+1; $i++, $counter++) //iterate through days
{
$timeStamp = strtotime("$year-$month-$i");
if($i == 1)
{
	// Workout when the first day of the month is
	$firstDay = date("w", $timeStamp);
        	for($j = 0; $j < $firstDay; $j++, $counter++)
		echo "<td align='center'> </td>";
} //end if 
if($counter % 7 == 0)
	print "</tr><tr align='center'>";
$weekend = "";
if(date("w", $timeStamp) == 0 || date("w", $timeStamp) == 6)
	$weekend = " weekend";
//find events for current day
$query = "SELECT event_id, title FROM calendar WHERE dateevent = '$year-$month-$i'";
$result = mysql_query($query);
if (mysql_num_rows($result)) //checks whether any events found for this day
{
	if($i == date("d") && $month == date("m") && $year == date("Y"))
		print "<td width='50' class='today$weekend' align='left'><div id=\"links\"><a href\"\">$i<span>";
	else		
		print "<td width='50' class='normal$weekend' align='left'><div id=\"links\"><a href\"\">$i<span>";
	$date = "$year-$month-$i";
	//this code is outside the following while loop to prevent a ' - ' in front of the first event title:
	$r=mysql_fetch_array($result);
	$event_id=$r["event_id"];
	$title=$r["title"];	
	print $title;
	while($r=mysql_fetch_array($result))
	{
		print " - ";
		$event_id=$r["event_id"];
		$title=$r["title"];	
		print $title;
	}
	print "</span></a></div></td>";
}
else //no events for this day
{
	if($i == date("d") && $month == date("m") && $year == date("Y"))
		print "<td width='50' class='today$weekend' align='left'>$i</td>";
	else		
		print "<td width='50' class='normal$weekend' align='left'>$i</td>";
}
}

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.