Jump to content

Recommended Posts

Hello all

 

This may seem stupid coming in here with absolutely no code to show. I am in planning of a php event calender for my work place and I am a very PHP rookie  basically i am asking for directions to go with this.

 

I have written scripts before but not like this. I  currently have a database set up for the events and about to write to that  databases table. I have date as a primary key and in format of date (2012-05-14). what i am  trying to figure out is how do i take this date have it find the correct day on the calender?

 

I hope someone can help me a little here i know i have asked a lot  but  could use a point in the right direction.

 

Thank you,

Nis

Link to comment
https://forums.phpfreaks.com/topic/262543-event-calender/
Share on other sites

Given what all you'd have to do to display a calendar on a page, your question doesn't make sense.

You execute a query to pull everything during the month (or whatever time range) being displayed; as you're printing the calendar you're also walking through the list of events. When you're showing a day then you'll also know if there are any events for that day and, if so, what they are.

Link to comment
https://forums.phpfreaks.com/topic/262543-event-calender/#findComment-1345508
Share on other sites

I have tried to write it in and for some reason it  writes in one day but not a seconds day

 

<?php require_once('Connections/TestServer.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_TestServer, $TestServer);
$query_Recordset1 = "SELECT * FROM calendar";
$Recordset1 = mysql_query($query_Recordset1, $TestServer) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<html>
<head>
<style type="text/css">
a { color: #000; text-decoration: none; }
a:hover { color: #000; text-decoration: underline; }

.cmonth { background-color: #999; color:#000; font-size: 24px; font-family: Verdana, Geneva, sans-serif; font-weight: bold; text-align: center; padding: 5px 0 5px 0; }
.cday { background-color: #ccc; color:#000; font-size: 12px; font-family: Verdana, Geneva, sans-serif; font-weight: lighter; text-align: center; padding: 2px 0 2px 0; width:100px; }
.cdate { background:#999; width: 25px; padding: 2px; text-align: center; vertical-align: middle; }
.cnote { font-size: 9px; }

table.dates { border-collapse: collapse; }
table.dates, td { border: 1px solid #CCC; text-align: left; vertical-align: top; height: 85px;}

table.footer { background-color: #999; font-weight: bold; padding: 5px 0 5px 0; }
.prev { width: 50%; text-align: center; }


</style>
</head>

<body>

<?php
	$monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

	if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
	if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");

	$cMonth = $_REQUEST["month"];
	$cYear = $_REQUEST["year"];

	$prev_year = $cYear;
	$next_year = $cYear;
	$prev_month = $cMonth-1;
	$next_month = $cMonth+1;

	if ($prev_month == 0 ) {
    		$prev_month = 12;
    		$prev_year = $cYear - 1;
	}

	if ($next_month == 13 ) {
   		 $next_month = 1;
    		$next_year = $cYear + 1;
	}
?>

<table>
	<!--- Month --->
   	<table width="700" class="header" cellpadding="0" cellspacing="0" border="0">
    		<tr>
        		<th class="cmonth" colspan="7"><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></th>
    		</tr>
     </table>
      
       <!--- Days of the Week --->
       <table width="700" class="middle" cellpadding="0" cellspacing="0" border="0">
	   <tr>
	    <th class="cday">Sunday</th>
	    <th class="cday">Monday</th>
	    <th class="cday">Tuesday</th>
	    <th class="cday">Wednesday</th>
    		 <th class="cday">Thursday</th>
	    <th class="cday">Friday</th>
	    <th class="cday">Saturday</th>
		</tr>
       </table>
       
       <!--- Days of the Week --->
       <table width="700" class="dates">
       <?php 
			$ddate = $row_Recordset1['date'];
		$parts = explode("-", $ddate);

		if ($parts[0] == $cYear && $parts[2] == $cMonth) echo "yes";


    		$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
    		$maxday = date("t",$timestamp);
   		$thismonth = getdate ($timestamp);
    		$startday = $thismonth['wday'];
    		for ($i=0; $i<($maxday+$startday); $i++) {
			// Display a blank cell until 1st of month matched appropriate day,
        		if(($i % 7) == 0 ) echo "<tr>";
			if($i < $startday) echo "<td></td>";

			elseif ($parts[0] == $cYear && $parts[1] == $cMonth && $parts[2] == ($i-1)) echo "<td><div class='cdate'>" . ($i - $startday + 1) . "</div><div class='cnote'>" . $row_Recordset1['note1'] . "</td>";

			// and display the day number when it does.
        		else echo "<td><div class='cdate'>" . ($i - $startday + 1) . "</div></td>";
        			if(($i % 7) == 6 ) echo "</tr>";
        		}
       ?>
       </table>
       
   	 <!--- Prev/Next --->
       <table width="700" class="footer" cellpadding="0" cellspacing="0" border="0">
    		<tr>
        		<th class="prev"> <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>">Previous</a></th>
       		<th class="prev"> <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>">Next</a></th>
    		</tr>
       </table>
   
</table>          

</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

 

anything help would be greatly appreciated

Link to comment
https://forums.phpfreaks.com/topic/262543-event-calender/#findComment-1346503
Share on other sites

Well yeah. All your code does is get one single row. And it might not even be in the time range you're displaying.

You need the right query, one that only pulls events from the database if they're on the calendar you're trying to show, and then you need some logic in the for loop to advance through the rows as you advance through the days.

 

So what query do you think you should use? It needs a WHERE and an ORDER BY at a minimum.

Link to comment
https://forums.phpfreaks.com/topic/262543-event-calender/#findComment-1346520
Share on other sites

i am trying to match a date as it is read with a date of the database to pull the data but nothing appears to work. i have never used date as a primary key before always used 123 .... I do not know where to look here any more. calling it a night and ill start all over tomorrow

Link to comment
https://forums.phpfreaks.com/topic/262543-event-calender/#findComment-1346530
Share on other sites

That's an okay query if you get one day's events at a time. But it means ~30 queries just for that.

SELECT * FROM calendar WHERE `date` >= first of the month AND `date` 

That will get you everything in one query.
 
Inside your for loop you need to iterate over that resultset. Keeping the
[code=php:0]$row_Recordset1 = mysql_fetch_assoc($Recordset1);
Link to comment
https://forums.phpfreaks.com/topic/262543-event-calender/#findComment-1346544
Share on other sites

Had to take a break from this a my router needed a replacement.

 

As for the query I understand what you are  taking about but the problem I see is  with prev/next month. I would have to rewrite the code just for that would i not?

 

As for the bringing in the events ill paste  code to see if it is what you mean. not  sure if that was what you meant or if it should have gone in after the 'for'

 

 

<?php require_once('Connections/TestServer.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_TestServer, $TestServer);
$query_CalQuery = "SELECT * FROM calendar WHERE `date` >= 2012-05-01 AND `date` < 2012-06-01 ORDER BY `date` ASC";
$CalQuery = mysql_query($query_CalQuery, $TestServer) or die(mysql_error());
$row_CalQuery = mysql_fetch_assoc($CalQuery);
$totalRows_CalQuery = mysql_num_rows($CalQuery);
?>
<html>
<head>
<style type="text/css">
a { color: #000; text-decoration: none; }
a:hover { color: #000; text-decoration: underline; }

.cmonth { background-color: #999; color:#000; font-size: 24px; font-family: Verdana, Geneva, sans-serif; font-weight: bold; text-align: center; padding: 5px 0 5px 0; }
.cday { background-color: #ccc; color:#000; font-size: 12px; font-family: Verdana, Geneva, sans-serif; font-weight: lighter; text-align: center; padding: 2px 0 2px 0; width:100px; }
.cdate { background:#999; width: 25px; padding: 2px; text-align: center; vertical-align: middle; }
.cnote { font-size: 9px; }

table.dates { border-collapse: collapse; }
table.dates, td { border: 1px solid #CCC; text-align: right; vertical-align: top; height: 85px;}

table.footer { background-color: #999; font-weight: bold; padding: 5px 0 5px 0; }
.prev { width: 50%; text-align: center; }


</style>
</head>

<body>

<?php
	$monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

	if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
	if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");

	$cMonth = $_REQUEST["month"];
	$cYear = $_REQUEST["year"];

	$prev_year = $cYear;
	$next_year = $cYear;
	$prev_month = $cMonth-1;
	$next_month = $cMonth+1;

	if ($prev_month == 0 ) {
    		$prev_month = 12;
    		$prev_year = $cYear - 1;
	}

	if ($next_month == 13 ) {
   		 $next_month = 1;
    		$next_year = $cYear + 1;
	}
?>

<table>
	<!--- Month --->
   	<table width="700" cellpadding="0" cellspacing="0" border="0">
    		<tr>
        		<th class="cmonth" colspan="7"><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></th>
    		</tr>
     </table>
      
       <!--- Days of the Week --->
       <table width="700" cellpadding="0" cellspacing="0" border="0">
	   <tr>
	    <th class="cday">Sunday</th>
	    <th class="cday">Monday</th>
	    <th class="cday">Tuesday</th>
	    <th class="cday">Wednesday</th>
    		 <th class="cday">Thursday</th>
	    <th class="cday">Friday</th>
	    <th class="cday">Saturday</th>
		</tr>
       </table>
       
       <!--- Days of the Week --->
       <table width="700" class="dates">
       <?php 


    		$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
    		$maxday = date("t",$timestamp);
   		$thismonth = getdate($timestamp);
    		$startday = $thismonth['wday'];
    		for ($i=0; $i<($maxday+$startday); $i++) {
			// Display a blank cell until 1st of month matched appropriate day,
        		if(($i % 7) == 0 ) echo "<tr>";
			if($i < $startday) echo "<td></td>";

			// and display the day number when it does.

        		else 
				echo "<td><div class='cdate'>" . ($i - $startday + 1) . "</div></td>";
				while ($row_CalQuery && $row_CalQuery["date"] == $date) {
        			// print the event information for $row_Recordset1
				echo $row_CalQuery['note1'];
				$row_CalQuery = mysql_fetch_assoc($CalQuery);

    				}

				if(($i % 7) == 6 ) echo "</tr>";
        		}
       ?>
       </table>
       
   	 <!--- Prev/Next --->
       <table width="700" class="footer" cellpadding="0" cellspacing="0" border="0">
    		<tr>
        		<th class="prev"> <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>">Previous</a></th>
       		<th class="prev"> <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>">Next</a></th>
    		</tr>
       </table>
   
</table>          

</body>
</html>
<?php
mysql_free_result($CalQuery);
?>

Link to comment
https://forums.phpfreaks.com/topic/262543-event-calender/#findComment-1346904
Share on other sites

Your code is setting $cMonth and $cYear to either the current month/year or the month/year that was submitted with the request for the page. Everything that is dependent on the $cMonth and $cYear value should be after the point where those two variables are set at.

 

Since you have the month and year you want the page to operate on, your query to retrieve the data should use those values -

 

$query_CalQuery = "SELECT * FROM calendar WHERE MONTH(`date`) = $cMonth AND YEAR(`date`) = $cYear ORDER BY `date` ASC";

 

Edit: Here is some conditioning/filtering that you should do on the $cMonth and $cYear values to prevent sql injection (replace your two existing lines of code with the following) -

 

$cMonth = intval($_REQUEST["month"]);
$cYear = intval($_REQUEST["year"]);

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/262543-event-calender/#findComment-1347062
Share on other sites

PFMaBiSmAD I tested  your query in phpMyAdmin and it worked so i applied it and did  what you said on top of what requinix said and well oddly no errors but no data pulled and placed on calender :/

 

here is code:

 

<?php require_once('Connections/TestServer.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_TestServer, $TestServer);
$query_CalQuery = "SELECT * FROM calendar WHERE `date` >= 2012-05-01 AND `date` < 2012-06-01 ORDER BY `date` ASC";
$CalQuery = mysql_query($query_CalQuery, $TestServer) or die(mysql_error());
$row_CalQuery = mysql_fetch_assoc($CalQuery);
$totalRows_CalQuery = mysql_num_rows($CalQuery);
?>
<html>
<head>
<style type="text/css">
a { color: #000; text-decoration: none; }
a:hover { color: #000; text-decoration: underline; }

.cmonth { background-color: #999; color:#000; font-size: 24px; font-family: Verdana, Geneva, sans-serif; font-weight: bold; text-align: center; padding: 5px 0 5px 0; }
.cday { background-color: #ccc; color:#000; font-size: 12px; font-family: Verdana, Geneva, sans-serif; font-weight: lighter; text-align: center; padding: 2px 0 2px 0; width:100px; }
.cdate { background:#999; width: 25px; padding: 2px; text-align: center; vertical-align: middle; }
.cnote { font-size: 9px; }

table.dates { border-collapse: collapse; }
table.dates, td { border: 1px solid #CCC; text-align: right; vertical-align: top; height: 85px;}

table.footer { background-color: #999; font-weight: bold; padding: 5px 0 5px 0; }
.prev { width: 50%; text-align: center; }


</style>
</head>

<body>

<?php
	$monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

	if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
	if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");

	$cMonth = intval($_REQUEST["month"]);
	$cYear = intval($_REQUEST["year"]);

	$prev_year = $cYear;
	$next_year = $cYear;
	$prev_month = $cMonth-1;
	$next_month = $cMonth+1;

	if ($prev_month == 0 ) {
    		$prev_month = 12;
    		$prev_year = $cYear - 1;
	}

	if ($next_month == 13 ) {
   		 $next_month = 1;
    		$next_year = $cYear + 1;
	}
?>

<table>
	<!--- Month --->
   	<table width="700" cellpadding="0" cellspacing="0" border="0">
    		<tr>
        		<th class="cmonth" colspan="7"><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></th>
    		</tr>
     </table>
      
       <!--- Days of the Week --->
       <table width="700" cellpadding="0" cellspacing="0" border="0">
	   <tr>
	    <th class="cday">Sunday</th>
	    <th class="cday">Monday</th>
	    <th class="cday">Tuesday</th>
	    <th class="cday">Wednesday</th>
    		 <th class="cday">Thursday</th>
	    <th class="cday">Friday</th>
	    <th class="cday">Saturday</th>
		</tr>
       </table>
       
       <!--- Days of the Week --->
       <table width="700" class="dates">
       <?php 


    		$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
    		$maxday = date("t",$timestamp);
   		$thismonth = getdate($timestamp);
    		$startday = $thismonth['wday'];
		$query_CalQuery = "SELECT * FROM calendar WHERE MONTH(`date`) = $cMonth AND YEAR(`date`) = $cYear ORDER BY `date` ASC";

		for ($i=0; $i<($maxday+$startday); $i++) {
			// Display a blank cell until 1st of month matched appropriate day,
        		if(($i % 7) == 0 ) echo "<tr>";
			if($i < $startday) echo "<td></td>";

			// and display the day number when it does.
        		else 

				echo "<td><div class='cdate'>" . ($i - $startday + 1) . "</div></td>";

				while ($row_CalQuery && $row_CalQuery["date"] == $date) {
        				// print the event information for $row_Recordset1
					echo $row_CalQuery['note1'];
					$row_CalQuery = mysql_fetch_assoc($CalQuery);
				}

				if(($i % 7) == 6 ) echo "</tr>";
        		}
       ?>
       
       </table>
       
   	 <!--- Prev/Next --->
       <table width="700" class="footer" cellpadding="0" cellspacing="0" border="0">
    		<tr>
        		<th class="prev"> <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>">Previous</a></th>
       		<th class="prev"> <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>">Next</a></th>
    		</tr>
       </table>
   
</table>          

</body>
</html>
<?php
mysql_free_result($CalQuery);
?>

Link to comment
https://forums.phpfreaks.com/topic/262543-event-calender/#findComment-1347351
Share on other sites

The following is one way of doing this (it replaces your block of php code that starts with the mktime() statement through to the closing ?> tag for that block) -

 

<?php
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate($timestamp);
$startday = $thismonth['wday'];

mysql_select_db($database_TestServer, $TestServer);
$query_CalQuery = "SELECT *, DAY(date) as day FROM calendar WHERE MONTH(`date`) = $cMonth AND YEAR(`date`) = $cYear ORDER BY `date` ASC";
$CalQuery = mysql_query($query_CalQuery, $TestServer) or die(mysql_error());
// retrieve and store the data in an array so that it can be accessed when looping over the days in the month
$data = array();
while($row = mysql_fetch_assoc($CalQuery)){
$data[$row['day']] = $row; // save data using the day number as the index/key
}

for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0 ) echo "<tr>";
// Display a blank cell until 1st of month matched appropriate day,
if($i < $startday){
	echo "<td></td>";
} else {
	// and display the day number when it does.
	$day = $i - $startday + 1;
	echo "<td><div class='cdate'>$day</div>";
	// check if there is event information for the current day
	if(isset($data[$day])){
		echo $data[$day]['note1'];
		// Note: if there can be more than one event on any day, you will need to make $data[$row['day']] an array of arrays and loop over the $data[$day] array.
	}
	echo "</td>";
}
if(($i % 7) == 6 ) echo "</tr>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/262543-event-calender/#findComment-1347355
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.