Page 1 of 1
Help printing an array in my calendar
Posted: Tue Jun 10, 2003 4:17 pm
by lloydie-t
OK, I'll admit it, I borrowed some code not realising what I was getting into. I am building a calendar based on class created by someone else. I have almost completed but I have gotten stuck, in that may calendar cannot display an array of events for the same day. Instead it will only display the last one entered. I am used to printing arrays in tables, but I am not sure where to go on this one. Please find the class that builds the calendar and point me in the right direction if you can.
Code: Select all
<?php
function build_calendar($month,$year,$dateArray) {
$daysOfWeek = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$firstDayOfMonth = mktime (0,0,0,$month,1,$year);
$numberDays = date ('t',$firstDayOfMonth);
$dateComponents = getdate($firstDayOfMonth);
$monthName = $dateComponents['month'];
$dayOfWeek = $dateComponents['wday'];
$calendar = "<table class='calendar'>";
$calendar .= "<caption>$monthName, $year</caption>";
$calendar .= "<tr>";
foreach($daysOfWeek as $day) {
$calendar .= "<th class='header'>$day</th>";
}
$currentDay = 1;
$calendar .= "</tr><tr>";
if ($dayOfWeek > 0) {
$calendar .= "<td colspan='$dayOfWeek'> </td>";
}
while ($currentDay <= $numberDays) {
if ($dayOfWeek == 7) {
$dayOfWeek = 0;
$calendar .= "</tr><tr>";
}
if (in_array($currentDay,$dateArray)) {
$date = "$year-$month-$currentDay";
include('pageconnect.php');
$connection = @mysql_connect($host, $user, $pass) or die ("Unable to connect to database");
mysql_select_db($db) or die ("Unable to select database: $db ");
$query = "SELECT cal_id, cal_desc, cal_date_start, cal_date_end
FROM cal_entry
WHERE approved='Yes' AND cal_date_start <= '$date 23:59:59' AND cal_date_end >= '$date'
GROUP by cal_date_start ";
$queryResult = mysql_query($query);
while($row = mysql_fetch_assoc($queryResult)){
$ccal_id = $row["cal_id"];
$ccal_desc = $row["cal_desc"];
$ccal_date_start = $row["cal_date_start"];
$ccal_date_end = $row["cal_date_end"];}
//this is where I am stuck
$calendar .= "<td class='linkedday' width='150' height='75'>
<a href='dwelcome.php?date=$date'
class='calendarlink'>$currentDay</a><br>Start: $ccal_date_start <br>End: $ccal_date_end<br><font size="2"><a href='dwelcome.php?date=$ccal_id'
class='calendarlink'>$ccal_desc</a></font></td>";
} else {
$monthday = "$year-$month-$currentDay";
$calendar .= "<td class='day' width='150' height='75'>
<a href='dwelcome.php?date=$monthday'
class='calendarlink'>$currentDay</a></td>";
}
$currentDay++;
$dayOfWeek++;
}
if ($dayOfWeek != 7) {
$remainingDays = 7 - $dayOfWeek;
$calendar .= "<td colspan='$remainingDays'> </td>";
}
$calendar .= "</table>";
return $calendar;
}
?>
Posted: Tue Jun 10, 2003 5:12 pm
by volka
I started indenting your code but could finish it
Code: Select all
<?php
function build_calendar($month,$year,$dateArray) {
$daysOfWeek = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$firstDayOfMonth = mktime (0,0,0,$month,1,$year);
$numberDays = date ('t',$firstDayOfMonth);
$dateComponents = getdate($firstDayOfMonth);
$monthName = $dateComponents['month'];
$dayOfWeek = $dateComponents['wday'];
$calendar = "<table class='calendar'>";
$calendar .= "<caption>$monthName, $year</caption>";
$calendar .= "<tr>";
foreach($daysOfWeek as $day) {
$calendar .= "<th class='header'>$day</th>";
}
$currentDay = 1;
$calendar .= "</tr><tr>";
if ($dayOfWeek > 0) {
$calendar .= "<td colspan='$dayOfWeek'> </td>";
}
while ($currentDay <= $numberDays) {
if ($dayOfWeek == 7) {
$dayOfWeek = 0;
$calendar .= "</tr><tr>";
}
if (in_array($currentDay,$dateArray)) {
$date = "$year-$month-$currentDay";
include('pageconnect.php');
$connection = @mysql_connect($host, $user, $pass) or die ("Unable to connect to database");
mysql_select_db($db) or die ("Unable to select database: $db ");
$query = "SELECT cal_id, cal_desc, cal_date_start, cal_date_end
FROM cal_entry
WHERE approved='Yes' AND cal_date_start <= '$date 23:59:59' AND cal_date_end >= '$date'
GROUP by cal_date_start ";
$queryResult = mysql_query($query);
while($row = mysql_fetch_assoc($queryResult)) {
$ccal_id = $row["cal_id"];
$ccal_desc = $row["cal_desc"];
$ccal_date_start = $row["cal_date_start"];
$ccal_date_end = $row["cal_date_end"];}
//this is where I am stuck
$calendar .= "<td class='linkedday' width='150' height='75'>
<a href='dwelcome.php?date=$date'
class='calendarlink'>$currentDay</a><br>Start: $ccal_date_start <br>End: $ccal_date_end<br><font size="2"><a href='dwelcome.php?date=$ccal_id'
class='calendarlink'>$ccal_desc</a></font></td>";
} else { /** warning: uh? */
$monthday = "$year-$month-$currentDay";
$calendar .= "<td class='day' width='150' height='75'>
<a href='dwelcome.php?date=$monthday'
class='calendarlink'>$currentDay</a></td>";
}
$currentDay++;
$dayOfWeek++;
}
if ($dayOfWeek != 7) {
$remainingDays = 7 - $dayOfWeek;
$calendar .= "<td colspan='$remainingDays'> </td>";
}
$calendar .= "</table>";
return $calendar;
}
?>
Posted: Tue Jun 10, 2003 5:41 pm
by lloydie-t
Thanx for having a look Volka. The code I previously posted is used as an include in a page which ascertains which dates for a month has an entry and creates an array of dates which has entries. I do not want to include to much code, but find the following.
Code: Select all
<?php
include "calendar1.php";
?>
<?php
$dateComponents = getdate();
if (isset ($_GET['date'])) {
$date_array = explode('-',$_GET['date']);
$year = $date_array[0];
$month = $date_array[1];
$day = $date_array[2];
$monthName1 = date( 'M', strtotime( "$date"));
} else {
$day = $currentDay;
$month = $dateComponents['mon'];
$year = $dateComponents['year'];
$monthName1 = date( 'M', strtotime( "$date"));
}
?>
<?php
include('pageconnect.php');
$connection = @mysql_connect($host, $user, $pass) or die ("Unable to connect to database");
mysql_select_db($db) or die ("Unable to select database: $db ");
$query = "SELECT cal_id, cal_desc, cal_date_start, cal_date_end
FROM cal_entry
WHERE year(cal_date_start)<='$year' AND month(cal_date_start)<='$month' AND year(cal_date_end)>='$year' AND month(cal_date_end)>='$month'";
$result = mysql_query ($query);
if (mysql_num_rows($result) < 1)
{ $dateArray[] = array('0000-00-00');}
else
{
while($row = mysql_fetch_assoc($result)){
$cal_id = $row["cal_id"];
$cal_desc = $row["cal_desc"];
$cal_date_start = $row["cal_date_start"];
$cal_date_end = $row["cal_date_end"];
$format='j';
$cal_array_start = explode(" ",$cal_date_start);
$cal_date_start = explode("-",$cal_array_start[0]);
$syear = $cal_date_start[0];
$smonth = $cal_date_start[1];
$sday = $cal_date_start[2];
$cal_array_end = explode(" ",$cal_date_end);
$cal_date_end = explode("-",$cal_array_end[0]);
$eyear = $cal_date_end[0];
$emonth = $cal_date_end[1];
$eday = $cal_date_end[2];
$calstart = mktime(00,00,00,$smonth,$sday,$syear);
$calend = mktime(00,00,00,$emonth,$eday,$eyear);
$dateArray[] = $sday;
$cal_idArray[] = $cal_id;
$cal_descArray[] = $cal_desc;
while($calstart < $calend) {
$calstart = $calstart + (24 * 3600);
$dateArray[] = date($format,$calstart);
$cal_idArray[] = $cal_id;
$cal_descArray[] = $cal_desc;
//$dateArray[]=$row['cal_date_start'];
$count = count($dateArray);
$min = 1;}
if ($count < $min) {
$dateArray[] = array('0000-00-00');
}}
}
echo build_calendar($month,$year,$dateArray);
if (isset ($_GET['date'])) {
$date = $_GET['date'];
} else {
$date = date("d F Y");
}
echo "</td></tr></table></p>";
?>
Posted: Tue Jun 10, 2003 8:42 pm
by volka
class='calendarlink'>$ccal_desc</a></font></td>";
} else { /** warning: uh? */
$monthday = "$year-$month-$currentDay";
this is the part I couldn't reformat because the brackets didn't match.
Posted: Wed Jun 11, 2003 1:17 am
by lloydie-t
Hi Volka, I am not sure why you are having a problem with that "else" statement. It is the code I currently use and does run (both on *nix and WIN2K)apart from the problem mentioned early. Here is part of the class I used to build it on if it helps.
Code: Select all
<?php
function build_calendar($month,$year,$dateArray) {
$daysOfWeek = array('Su','Mo','Tu','We','Th','Fr','Sa');
$firstDayOfMonth = mktime (0,0,0,$month,1,$year);
$numberDays = date ('t',$firstDayOfMonth);
$dateComponents = getdate($firstDayOfMonth);
$monthName = $dateComponents['month'];
$dayOfWeek = $dateComponents['wday'];
$calendar = "<table class='calendar'>";
//$calendar .= "<caption>$monthName, $year</caption>";
//$calendar .= "<tr>";
foreach($daysOfWeek as $day) {
$calendar .= "<th class='header'>$day</th>";
}
$currentDay = 1;
$calendar .= "</tr><tr>";
if ($dayOfWeek > 0) {
$calendar .= "<td colspan='$dayOfWeek'> </td>";
}
while ($currentDay <= $numberDays) {
if ($dayOfWeek == 7) {
$dayOfWeek = 0;
$calendar .= "</tr><tr>";
}
if (in_array($currentDay,$dateArray)) {
$date = "$year-$month-$currentDay";
$calendar .= "<td class='linkedday'>
<a href='dwelcome3.php?date=$date'
class='calendarlink'>$currentDay</a></td>";
} else {
$monthday = "$year-$month-$currentDay";
$calendar .= "<td class='day'>
<a href='eventadd.php?date=$monthday'
class='calendarlink'>$currentDay</a></td>";
}
$currentDay++;
$dayOfWeek++;
}
if ($dayOfWeek != 7) {
$remainingDays = 7 - $dayOfWeek;
$calendar .= "<td colspan='$remainingDays'> </td>";
}
$calendar .= "</table>";
return $calendar;
}
?>
thanx for your help so far
Posted: Wed Jun 11, 2003 4:44 am
by volka
ah, I had some trouble with the missing indents but now got it

Code: Select all
<?php
function build_calendar($month, $year, $dateArray)
{
$daysOfWeek = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$firstDayOfMonth = mktime (0, 0, 0, $month, 1, $year);
$numberDays = date ('t', $firstDayOfMonth);
$dateComponents = getdate($firstDayOfMonth);
$monthName = $dateComponents['month'];
$dayOfWeek = $dateComponents['wday'];
$calendar = "<table class='calendar'>";
$calendar .= "<caption>$monthName, $year</caption>";
$calendar .= "<tr>";
foreach($daysOfWeek as $day) {
$calendar .= "<th class='header'>$day</th>";
}
$currentDay = 1;
$calendar .= "</tr><tr>";
if ($dayOfWeek > 0) {
$calendar .= "<td colspan='$dayOfWeek'> </td>";
} while ($currentDay <= $numberDays) {
if ($dayOfWeek == 7) {
$dayOfWeek = 0;
$calendar .= "</tr><tr>";
}
if (in_array($currentDay, $dateArray)) {
$date = "$year-$month-$currentDay";
include('pageconnect.php');
$connection = @mysql_connect($host, $user, $pass) or die ("Unable to connect to database");
mysql_select_db($db) or die ("Unable to select database: $db ");
$query = "SELECT cal_id, cal_desc, cal_date_start, cal_date_end
FROM cal_entry
WHERE approved='Yes' AND cal_date_start <= '$date 23:59:59' AND cal_date_end >= '$date'
GROUP by cal_date_start ";
$queryResult = mysql_query($query);
while ($row = mysql_fetch_assoc($queryResult)) {
$ccal_id = $row["cal_id"];
$ccal_desc = $row["cal_desc"];
$ccal_date_start = $row["cal_date_start"];
$ccal_date_end = $row["cal_date_end"];
}
// this is where I am stuck
$calendar .= "<td class='linkedday' width='150' height='75'>
<a href='dwelcome.php?date=$date'
class='calendarlink'>$currentDay</a><br>Start: $ccal_date_start <br>End: $ccal_date_end<br><font size="2"><a href='dwelcome.php?date=$ccal_id'
class='calendarlink'>$ccal_desc</a></font></td>";
} else {
$monthday = "$year-$month-$currentDay";
$calendar .= "<td class='day' width='150' height='75'>
<a href='dwelcome.php?date=$monthday'
class='calendarlink'>$currentDay</a></td>";
}
$currentDay++;
$dayOfWeek++;
}
if ($dayOfWeek != 7) {
$remainingDays = 7 - $dayOfWeek;
$calendar .= "<td colspan='$remainingDays'> </td>";
}
$calendar .= "</table>";
return $calendar;
}
?>
queryResult = mysql_query($query);
while ($row = mysql_fetch_assoc($queryResult)) {
$ccal_id = $row["cal_id"];
$ccal_desc = $row["cal_desc"];
$ccal_date_start = $row["cal_date_start"];
$ccal_date_end = $row["cal_date_end"];
}
// this is where I am stuck
$calendar .= "<td class='linkedday' width='150' height='75'>
<a href='dwelcome.php?date=$date'
class='calendarlink'>$currentDay</a><br>Start: $ccal_date_start <br>End: $ccal_date_end<br><font size=\"2\"><a href='dwelcome.php?date=$ccal_id'
class='calendarlink'>$ccal_desc</a></font></td>";
this part you have to change. you have to start the formatting before the while loop (the table cell, date-link, start/end-date), append each recordset within the loop and end the tablecell after the while loop. Something like
Code: Select all
$queryResult = mysql_query($query);
$calendar .= "tablecell linkday current date_start date_end<br/>";
while ($row = mysql_fetch_assoc($queryResult)) {
$ccal_id = $row["cal_id"];
$ccal_desc = $row["cal_desc"];
$ccal_date_start = $row["cal_date_start"];
$ccal_date_end = $row["cal_date_end"];
$calendar .= "font link ccal_desc<br/>";
}
$calendar .= '</td>';
Posted: Wed Jun 11, 2003 1:35 pm
by lloydie-t
Thanks volka but I don't quite understand.
$calendar .= "tablecell linkday current date_start date_end<br/>";
I dont know where I get the tablecell from. I assume current, date_start, date_end would be $curentday, $ccal_date_start and $ccal_date end respectively. Any Ideas
Posted: Wed Jun 11, 2003 3:08 pm
by volka
with e.g. tablecell I meant
<td> 
You can keep the current code, but you have to split it up a bit.
The
static part of a day must be outside the loop, but the part for each entry for a day must be inside the loop
Posted: Wed Jun 11, 2003 4:18 pm
by lloydie-t
Note quite there yet. I seem to be getting T_ELSE not expected error when I run the following code and I dont understand what I am doing wrong. I think I am almost there. Can you help?
Code: Select all
if (in_array($currentDay,$dateArray)) {
$date = "$year-$month-$currentDay";
include('pageconnect.php');
$connection = @mysql_connect($host, $user, $pass) or die ("Unable to connect to database");
mysql_select_db($db) or die ("Unable to select database: $db ");
$query = "SELECT cal_id, cal_desc, cal_date_start, cal_date_end
FROM cal_entry
WHERE approved='Yes' AND cal_date_start <= '$date 23:59:59' AND cal_date_end >= '$date'
GROUP by cal_date_start ";
$queryResult = mysql_query($query);
$calendar .= "<td class='linkedday' width='150' height='75'>
<a href='dwelcome.php?date=$date'
class='calendarlink'>$currentDay</a><br>;
while($row = mysql_fetch_assoc($queryResult)){
Start: $row[cal_date_start] <br>End: $row[cal_date_end]<br><font size="2"><a href='dwelcome.php?date=$row[cal_id]'
class='calendarlink'>$row[cal_desc]</a></font></td>";}
$calendar .= "</td>";
//Going wrong on the following line
} else {
$monthday = "$year-$month-$currentDay";
$calendar .= "<td class='day' width='150' height='75'>
<a href='dwelcome.php?date=$monthday'
class='calendarlink'>$currentDay</a></td>";
}
Posted: Wed Jun 11, 2003 4:28 pm
by cactus
You can see your error in the syntax highlighting:
<snip>
Code: Select all
$calendar .= "<td class='linkedday' width='150' height='75'>
<a href='dwelcome.php?date=$date'
class='calendarlink'>$currentDay</a><br>;
</snip>
You forgot to close this variable, before the while.
Regards,
Posted: Wed Jun 11, 2003 4:30 pm
by cactus
To be more clear, sorry, you need a double-quote after the "br" tag.
Regards,
Posted: Wed Jun 11, 2003 5:08 pm
by lloydie-t
one step forward one step back. I now can get the calendar built, but they have no entries for the days events. I am being very dim today I can't see where I going wrong. Please help
Code: Select all
$calendar .= "<td class='linkedday' width='150' height='75'>
<a href='dwelcome.php?date=$date'
class='calendarlink'>$currentDay</a><br>";
while($row = mysql_fetch_assoc($queryResult)){
// no entries here
"Start: $row[cal_date_start] <br>End: $row[cal_date_end]<br><font size="2"><a href='dwelcome.php?date=$row[cal_id]'
class='calendarlink'>$row[cal_desc]</a></font></td>";}
$calendar .= "</td>";
Posted: Wed Jun 11, 2003 5:32 pm
by cactus
Now you need to assign the result of your while loop to the $calendar variable:
Code: Select all
while($row = mysql_fetch_assoc($queryResult))
{
// no entries here
$calendar .= 'Start: '.$rowїcal_date_start].' <br>End: '.$rowїcal_date_end].'<br><font size="2">';
$calendar .= '<a href="dwelcome.php?date='.$rowїcal_id].'" class="calendarlink">'.$rowїcal_desc].'</a></font></td>';
}
$calendar .= "</td>";
Regards,
Posted: Wed Jun 11, 2003 6:04 pm
by lloydie-t
Cactus, Volka. thanx for your help. Got it working with the following:
Code: Select all
<?php
function build_calendar($month,$year,$dateArray) {
$daysOfWeek = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$firstDayOfMonth = mktime (0,0,0,$month,1,$year);
$numberDays = date ('t',$firstDayOfMonth);
$dateComponents = getdate($firstDayOfMonth);
$monthName = $dateComponents['month'];
$dayOfWeek = $dateComponents['wday'];
$calendar = "<table class='calendar'>";
$calendar .= "<caption>$monthName, $year</caption>";
$calendar .= "<tr>";
foreach($daysOfWeek as $day) {
$calendar .= "<th class='header'>$day</th>";
}
$currentDay = 1;
$calendar .= "</tr><tr>";
if ($dayOfWeek > 0) {
$calendar .= "<td colspan='$dayOfWeek'> </td>";
}
while ($currentDay <= $numberDays) {
if ($dayOfWeek == 7) {
$dayOfWeek = 0;
$calendar .= "</tr><tr>";
}
if (in_array($currentDay,$dateArray)) {
$date = "$year-$month-$currentDay";
include('pageconnect.php');
$connection = @mysql_connect($host, $user, $pass) or die ("Unable to connect to database");
mysql_select_db($db) or die ("Unable to select database: $db ");
$query = "SELECT cal_id, cal_desc, cal_date_start, cal_date_end
FROM cal_entry
WHERE approved='Yes' AND cal_date_start <= '$date 23:59:59' AND cal_date_end >= '$date'
GROUP by cal_date_start ";
$queryResult = mysql_query($query);
$calendar .= "<td class='linkedday' width='150' height='75'>
<a href='dwelcome.php?date=$date'
class='calendarlink'>$currentDay</a><br>";
while($row = mysql_fetch_assoc($queryResult)){
$calendar .= 'Start: '.$row[cal_date_start].' <br>End: '.$row[cal_date_end].'<br><font size="2">';
$calendar .= '<a href="dwelcome.php?date='.$row[cal_id].'" class="calendarlink">'.$row[cal_desc].'</a></font><br>'; }
$calendar .= "</td>";
} else {
$monthday = "$year-$month-$currentDay";
$calendar .= "<td class='day' width='150' height='75'>
<a href='dwelcome.php?date=$monthday'
class='calendarlink'>$currentDay</a></td>";
}
$currentDay++;
$dayOfWeek++;
}
if ($dayOfWeek != 7) {
$remainingDays = 7 - $dayOfWeek;
$calendar .= "<td colspan='$remainingDays'> </td>";
}
$calendar .= "</table>";
return $calendar;
}
?>
praise be to you
Posted: Wed Jun 11, 2003 6:12 pm
by cactus
No worries.
Regards,