Jump to content

Recommended Posts

Hi,

 

following on from a previous post, I've been using a piece of code that I found here: -

http://keithdevens.com/software/php_calendar

 

It works great and i've managed to customise it to take dates from a DB and show the dates on the calendar in different colors depending on the type of entry.

www.the-guards.org.uk/private/calendar.php

 

 

I'd really like to get this to display a whole year but I cant figure out what I need to do - the guy that wrote it shows a script containing custom functions to make it work and hints at how the functions work - but I cant figure it out.

 

I have emailed the guy asking for some help, but he hasn't responded.

 

any ideas?

 

or even suggestions for a similar calendar that I can easily use (this follows on from that fact that I found a post on here saying "dont build your own calendar - use somebody else's haha).

 

Hi,

 

following on from a previous post, I've been using a piece of code that I found here: -

http://keithdevens.com/software/php_calendar

 

It works great and i've managed to customise it to take dates from a DB and show the dates on the calendar in different colors depending on the type of entry.

www.the-guards.org.uk/private/calendar.php

 

 

I'd really like to get this to display a whole year but I cant figure out what I need to do - the guy that wrote it shows a script containing custom functions to make it work and hints at how the functions work - but I cant figure it out.

 

I have emailed the guy asking for some help, but he hasn't responded.

 

any ideas?

 

or even suggestions for a similar calendar that I can easily use (this follows on from that fact that I found a post on here saying "dont build your own calendar - use somebody else's haha).

 

Did you read the link that you gave us at all? Further down the page he gives you his exact code he uses to print an entire year... I almost feel as if I shouldn't have posted anything here.

maybe i'm being dumb, but i'm not sure how to put together the "$weblog->printCalendar()" function, I know he says roughly what the function does, I'm just unsure about how to get it going - or how I would replicate it using regular functions.

 

I'm not asking for a complete solution thatI can copy and paste here, just looking for some advice - or maybe a pointer to some different code.

 

maybe i'm being dumb, but i'm not sure how to put together the "$weblog->printCalendar()" function, I know he says roughly what the function does, I'm just unsure about how to get it going - or how I would replicate it using regular functions.

 

I'm not asking for a complete solution thatI can copy and paste here, just looking for some advice - or maybe a pointer to some different code.

 

Oh. Do you not know about classes? I apologize then if this is the case.

maybe i'm being dumb, but i'm not sure how to put together the "$weblog->printCalendar()" function, I know he says roughly what the function does, I'm just unsure about how to get it going - or how I would replicate it using regular functions.

 

I'm not asking for a complete solution thatI can copy and paste here, just looking for some advice - or maybe a pointer to some different code.

 

Oh. Do you not know about classes? I apologize then if this is the case.

 

I'll take that as "go and read about classes" haha.

;)

 

In that case you should be able to do something like this

 

 

<table style="margin: auto">
<tr>
    <?php $time = time();
               for($month=1; $month<=12; $month++){ ?>
        <td style="vertical-align: top">
            <?php echo generate_calendar(date('Y', $time), date('n', $time)); ?>
        </td>
        <?php if($month%3 == 0 and $month<12){ ?>
            </tr><tr>
        <?php } ?>
    <?php } ?>
</tr>
</table>

 

I would show you how to do it his way but without actually looking at his code I don't know what class he is using so I just replaced it with his generate calendar function

almost - that generates 12 incidents of the current month, which is an excellent starting point!

 

I just have to figure out how to make it loop through each month - I guess I just change the variables in the generate_calendar.

 

thanks!

almost - that generates 12 incidents of the current month, which is an excellent starting point!

 

I just have to figure out how to make it loop through each month - I guess I just change the variables in the generate_calendar.

 

thanks!

 

Lol sorry just change this

 

date('n', $time)

 

to this

 

$month

ok, one more question if i may.

 

I'm now scratching my head over this;

 

As It was I had a calendar showing one month which highlighted dates from a DB that was filtered to a particular month, so the query was: -

 

select day(gl_date) as 'gl_date' from giglist where month(gl_date) = '4' and year(gl_date) = '2010'

 

obviously this worked for April.

 

Now that the code generates a whole year, what do I need to do to change the $days array (and or the loop) so that actual dates are shown in the correct month??

 

If I remove the 'where' clause from the query, the calendar shows all the dates in every month because it just returns a number.

http://www.the-guards.org.uk/private/calendar.php

 

So if it returns '2' the 2nd of every month is highlighted.

 

Do I need to add the month in the $days array and then get the for loop to test for the month??

 

 

 

Sounds to me like you are on the right track :) If you create a multi-dimensional array with the first dimension being months and the second dimension being days that need to be marked. Then inside of your for loop you would do a foreach loop that would look something like this

 

foreach($selected_dates[$month] as $d) {
   /*pseudo-code here
   Select $d on the calendar
   */
}

OK, i'm pulling my hair out again!!

 

this is what i've got :-

 

The query now has 'month(gl_date) as gl_month' as well as day(gl_date) as gig_date'

 

The $days array looks like this: -

 

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{ 
$days[$row['gl_month']] = array($row['gig_date'],$link,$row['gl_color']);

}

I'm not sure that that's correct(?).

 

and the loop now looks like this: -

<table style="margin: auto; border: 1">
<tr>
    <?php $time = time();
               for($month=1; $month<=12; $month++){ ?>
        <td style="vertical-align: top";>
            <?php foreach($days['gl_month'] as $d) 
		echo generate_calendar(date('Y', $time), $month, $d);?>
        </td><td></td>
        <?php if($month%3 == 0 and $month<12){ ?>
            </tr><tr>
        <?php } ?>
    <?php } ?>
</tr>
</table>

..and I know that doesn't work!!

 

Thinking it through, i'm not sure i'm getting the methodology right:-

 

for(every month between 1 and 12){

foreach(month that has dates as $d)

generate_calendar(this year, every month, $d)

 

But $d is just going to be the month isn't it - so, the '$d' part of the generate_function shoudl be ($days where $row['gl_month'] = $d) - does that sound right, if so how do I code that?

 

You definitely don't want to put the generate calendar function inside of the foreach loop. You'll have a lot of calendars :/ I'm not sure with your system how you are supposed to select a date so I need that information first. It looks like you are supposed to have the dates in an array. Let me know what you have to do to select a date and I'll set it up for you :)

as the calendar script is originally for generating one month the date is simply a number that goes into the third element of the generate_calendar function - so: generate_calendar('2010','4', '21' will return a calendar for April 2010 with the number '21' highlighted.

 

to get the date number i'm taking the date from my database date field like this: day(gl_date) as 'gig_date' and then $days puts that into an array.

as the calendar script is originally for generating one month the date is simply a number that goes into the third element of the generate_calendar function - so: generate_calendar('2010','4', '21' will return a calendar for April 2010 with the number '21' highlighted.

 

to get the date number i'm taking the date from my database date field like this: day(gl_date) as 'gig_date' and then $days puts that into an array.

 

ok so take out the foreach loop and create a SQL query that will pull all the dates for the specified month that the loop is on denoted by $month. Then you should create a loop that will load that data into an array with code similar to the following

 

$result = mysql_query("The query here");
$arr = array();
while($row = mysql_fetch_array($result)){
   $arr[] = $row['gig_date'];
}

 

Then in your generate calendar it should be something like this

 

echo generate_calendar(date('Y', $time), $month, $arr);

 

Then after that but inside the loop you want to unset your array so that you don't just continue where you left off in the next set of the loop

 

unset($arr);

Furthermore I've been reading a little on that class. It looks like it automatically makes the 13th month, 14th month, etc into the next year. So what you could do is find out what the current month is with the php date function and start your loop on that then set the loop to go until that month + 12. Something like this

 

for($month=date('n');$month<=(date('n')+12);$month++)

 

Although if you do this you are going to have to find a different way of doing your SQL query that will change the year. Like a check to see if the month is > 12 and if it is then Query the gig dates for the month - 12 and the year + 1

no....i'm still pulling my hair out lol.

 

I've tried several things, but not got anywhere!!

 

here's the code i've got so far: -

<style type="text/css">
<!--
.gig_day {
font-weight: bolder;
color: white;
background-color: red;
text-align: center;

}
.unav_day {
font-weight: bolder;
color: white;
background-color: grey;
text-align: center;

}
-->
</style>

<?php

mysql_connect("database.com", "user", "password") or die(mysql_error()); 
mysql_select_db("the_guards_org_uk_users") or die(mysql_error()); 

$query = "
select 
month(gl_date)
as 'gig_month',
day(gl_date)
as 'gig_date',
case
when gl_unavailable = '1' then 'unav_day'
else  'gig_day'
end as 'gl_color'

from tg_gig_list 
where 
year(gl_date) = '2010' 

order by gl_date";
$result = mysql_query($query);


//$link="#";

$class="gig_day"; 
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{ 
$days[$row['gig_date']] = array($link,$row['gl_color']);

}
# PHP Calendar (version 2.3), written by Keith Devens
# http://keithdevens.com/software/php_calendar
#  see example at http://keithdevens.com/weblog
# License: http://keithdevens.com/software/license

function generate_calendar($year, $month, $days = array(), $day_name_length = 3, $month_href = NULL, $first_day = 0, $pn = array()){
$first_of_month = gmmktime(0,0,0,$month,1,$year);
#remember that mktime will automatically correct if invalid dates are entered
# for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998
# this provides a built in "rounding" feature to generate_calendar()

$day_names = array(); #generate all the day names according to the current locale
for($n=0,$t=(3+$first_day)*86400; $n<7; $n++,$t+=86400) #January 4, 1970 was a Sunday
	$day_names[$n] = ucfirst(gmstrftime('%A',$t)); #%A means full textual day name

list($month, $year, $month_name, $weekday) = explode(',',gmstrftime('%m,%Y,%B,%w',$first_of_month));
$weekday = ($weekday + 7 - $first_day) % 7; #adjust for $first_day
$title   = htmlentities(ucfirst($month_name)).' '.$year;  #note that some locales don't capitalize month and day names

#Begin calendar. Uses a real <caption>. See http://diveintomark.org/archives/2002/07/03
@list($p, $pl) = each($pn); @list($n, $nl) = each($pn); #previous and next links, if applicable
if($p) $p = '<span class="calendar-prev">'.($pl ? '<a href="'.htmlspecialchars($pl).'">'.$p.'</a>' : $p).'</span> ';
if($n) $n = ' <span class="calendar-next">'.($nl ? '<a href="'.htmlspecialchars($nl).'">'.$n.'</a>' : $n).'</span>';
$calendar = '<table class="calendar">'."\n".
	'<caption class="calendar-month">'.$p.($month_href ? '<a href="'.htmlspecialchars($month_href).'">'.$title.'</a>' : $title).$n."</caption>\n<tr>";

if($day_name_length){ #if the day names should be shown ($day_name_length > 0)
	#if day_name_length is >3, the full name of the day will be printed
	foreach($day_names as $d)
		$calendar .= '<th abbr="'.htmlentities($d).'">'.htmlentities($day_name_length < 4 ? substr($d,0,$day_name_length) : $d).'</th>';
	$calendar .= "</tr>\n<tr>";
}

if($weekday > 0) $calendar .= '<td colspan="'.$weekday.'"> </td>'; #initial 'empty' days
for($day=1,$days_in_month=gmdate('t',$first_of_month); $day<=$days_in_month; $day++,$weekday++){
	if($weekday == 7){
		$weekday   = 0; #start a new week
		$calendar .= "</tr>\n<tr>";
	}
	if(isset($days[$day]) and is_array($days[$day])){
		@list($link, $classes, $content) = $days[$day];
		if(is_null($content))  $content  = $day;
		$calendar .= '<td'.($classes ? ' class="'.htmlspecialchars($classes).'">' : '>').
			($link ? '<a href="'.htmlspecialchars($link).'">'.$content.'</a>' : $content).'</td>';
	}
	else $calendar .= "<td>$day</td>";
}
if($weekday != 7) $calendar .= '<td colspan="'.(7-$weekday).'"> </td>'; #remaining "empty" days

return $calendar."</tr>\n</table>\n";
}




$time = time(); 
$today = date('j',$time); 

//echo generate_calendar(date('Y', $time), date('n', $time), $days, 3, '/guard_admin.php'); 
?>

<table style="margin: auto; border: 1">
<tr>
    <?php $time = time();
               for($month=1; $month<=12; $month++){ ?>
        <td style="vertical-align: top";>
            <?php echo generate_calendar(date('Y', $time), $month, $days); ?>
        </td><td></td>
        <?php if($month%3 == 0 and $month<12){ ?>
            </tr><tr>
        <?php } ?>
    <?php } ?>
</tr>
</table>

 

As you can see, I've got the month coming out of the query (as gig_month), i've tried to put the month into an array with the day and the other elements that are required - which I can do, but i'm totally stumped as to how to get the dates from a certain month to echo into the generate_calendar class for every cycle of the $month loop.

 

do i need to do a foreach loop within the $month foreach loop?

 

You need to do the while loop I told you and put that inside the for loop

 

$result = mysql_query("The query here");
$arr = array();
while($row = mysql_fetch_array($result)){
   $arr[] = $row['gig_date'];
}

 

Have the query be whatever you use to access the dates per month. So this would be an example

 

mysql_query("SELECT date FROM tbl_gigs WHERE Year=" . $year . " AND Month=" . $month);

ok i've done it!!!!!

 

www.the-guards.org.uk/private/calendar.php

 

and here's the code for anyone that wants to use it: -

<style type="text/css">
<!--

a:link{ text-decoration: none;
color: white;
}

a:visited{ text-decoration: none;
color: white;
}

a:hover{ text-decoration: none;
color: white;
font-weight: bolder;

}
.gig_day {
font-weight: bolder;
color: white;
background-color: red;
text-align: center;
text-decoration: none;	
}
.unav_day {
font-weight: bolder;
color: white;
background-color: grey;
text-align: center;

}
-->
</style>

<?php

mysql_connect("database.com", "username", "password") or die(mysql_error()); 
mysql_select_db("database") or die(mysql_error()); 




$link="#";

$class="gig_day"; 

# PHP Calendar (version 2.3), written by Keith Devens
# http://keithdevens.com/software/php_calendar
#  see example at http://keithdevens.com/weblog
# License: http://keithdevens.com/software/license

function generate_calendar($year, $month, $days = array(), $day_name_length = 3, $month_href = NULL, $first_day = 0, $pn = array()){
$first_of_month = gmmktime(0,0,0,$month,1,$year);
#remember that mktime will automatically correct if invalid dates are entered
# for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998
# this provides a built in "rounding" feature to generate_calendar()

$day_names = array(); #generate all the day names according to the current locale
for($n=0,$t=(3+$first_day)*86400; $n<7; $n++,$t+=86400) #January 4, 1970 was a Sunday
	$day_names[$n] = ucfirst(gmstrftime('%A',$t)); #%A means full textual day name

list($month, $year, $month_name, $weekday) = explode(',',gmstrftime('%m,%Y,%B,%w',$first_of_month));
$weekday = ($weekday + 7 - $first_day) % 7; #adjust for $first_day
$title   = htmlentities(ucfirst($month_name)).' '.$year;  #note that some locales don't capitalize month and day names

#Begin calendar. Uses a real <caption>. See http://diveintomark.org/archives/2002/07/03
@list($p, $pl) = each($pn); @list($n, $nl) = each($pn); #previous and next links, if applicable
if($p) $p = '<span class="calendar-prev">'.($pl ? '<a href="'.htmlspecialchars($pl).'">'.$p.'</a>' : $p).'</span> ';
if($n) $n = ' <span class="calendar-next">'.($nl ? '<a href="'.htmlspecialchars($nl).'">'.$n.'</a>' : $n).'</span>';
$calendar = '<table class="calendar">'."\n".
	'<caption class="calendar-month">'.$p.($month_href ? '<a href="'.htmlspecialchars($month_href).'">'.$title.'</a>' : $title).$n."</caption>\n<tr>";

if($day_name_length){ #if the day names should be shown ($day_name_length > 0)
	#if day_name_length is >3, the full name of the day will be printed
	foreach($day_names as $d)
		$calendar .= '<th abbr="'.htmlentities($d).'">'.htmlentities($day_name_length < 4 ? substr($d,0,$day_name_length) : $d).'</th>';
	$calendar .= "</tr>\n<tr>";
}

if($weekday > 0) $calendar .= '<td colspan="'.$weekday.'"> </td>'; #initial 'empty' days
for($day=1,$days_in_month=gmdate('t',$first_of_month); $day<=$days_in_month; $day++,$weekday++){
	if($weekday == 7){
		$weekday   = 0; #start a new week
		$calendar .= "</tr>\n<tr>";
	}
	if(isset($days[$day]) and is_array($days[$day])){
		@list($link, $classes, $content) = $days[$day];
		if(is_null($content))  $content  = $day;
		$calendar .= '<td'.($classes ? ' class="'.htmlspecialchars($classes).'">' : '>').
			($link ? '<a href="'.htmlspecialchars($link).'">'.$content.'</a>' : $content).'</td>';
	}
	else $calendar .= "<td>$day</td>";
}
if($weekday != 7) $calendar .= '<td colspan="'.(7-$weekday).'"> </td>'; #remaining "empty" days

return $calendar."</tr>\n</table>\n";
}




$time = time(); 
$today = date('j',$time); 

//echo generate_calendar(date('Y', $time), date('n', $time), $days, 3, '/guard_admin.php'); 
?>

<table style="margin: auto; border: 1">
<tr>
    <?php $time = time();
               for($month=1; $month<=12; $month++){ ?>
        <td style="vertical-align: top";>

            <?php
$result = mysql_query("select gl_venue, day(gl_date) as gig_date, case
when gl_unavailable = '1' then 'unav_day'
else  'gig_day'
end as 'gl_color' from tg_gig_list 
where 
year(gl_date) = '2010' 
and month(gl_date) = $month
order by gl_date");
$arr = array();
$venue = array();
while($row = mysql_fetch_array($result)){
   $arr[$row['gig_date']] = array($link,$row['gl_color']);
  
}


		echo generate_calendar(date('Y', $time), $month, $arr);
		?>
        </td><td></td>
        <?php if($month%3 == 0 and $month<12){ ?>
            </tr><tr>
        <?php } ?>
    <?php }
unset($arr);
?>
</tr>
</table>

 

 

thanks very much for your help Aeroswat, its much appreciated and I've learned a lot!  :D

 

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.