...

View Full Version : Events Calendar Code not working.



xeirus
08-04-2012, 07:21 PM
Hi!
I obtained this code from a site which helps create an Events Calendar but it keeps giving me an error.
This is the code (and below that is the error):


/* draws a calendar */
function draw_calendar($month,$year,$events = array()){

/* draw table */
$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

/* table headings */
$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

/* days and weeks vars now ... */
$running_day = date('w',mktime(0,0,0,$month,1,$year));
$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();

/* row for week one */
$calendar.= '<tr class="calendar-row">';

/* print "blank" days until the first of the current week */
for($x = 0; $x < $running_day; $x++):
$calendar.= '<td class="calendar-day-np">&nbsp;</td>';
$days_in_this_week++;
endfor;

/* keep going with days.... */
for($list_day = 1; $list_day <= $days_in_month; $list_day++):
$calendar.= '<td class="calendar-day"><div style="position:relative;height:100px;">';
/* add in the day number */
$calendar.= '<div class="day-number">'.$list_day.'</div>';

$event_day = $year.'-'.$month.'-'.$list_day;
if(isset($events[$event_day])) {
foreach($events[$event_day] as $event) {
$calendar.= '<div class="event">'.$event['title'].'</div>';
}
}
else {
$calendar.= str_repeat('<p>&nbsp;</p>',2);
}
$calendar.= '</div></td>';
if($running_day == 6):
$calendar.= '</tr>';
if(($day_counter+1) != $days_in_month):
$calendar.= '<tr class="calendar-row">';
endif;
$running_day = -1;
$days_in_this_week = 0;
endif;
$days_in_this_week++; $running_day++; $day_counter++;
endfor;

/* finish the rest of the days in the week */
if($days_in_this_week < 8):
for($x = 1; $x <= (8 - $days_in_this_week); $x++):
$calendar.= '<td class="calendar-day-np">&nbsp;</td>';
endfor;
endif;

/* final row */
$calendar.= '</tr>';


/* end the table */
$calendar.= '</table>';

/** DEBUG **/
$calendar = str_replace('</td>','</td>'."\n",$calendar);
$calendar = str_replace('</tr>','</tr>'."\n",$calendar);

/* all done, return result */
return $calendar;
}

function random_number() {
srand(time());
return (rand() % 7);
}

/* date settings */
$month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));
$year = (int) ($_GET['year'] ? $_GET['year'] : date('Y'));

/* select month control */
$select_month_control = '<select name="month" id="month">';
for($x = 1; $x <= 12; $x++) {
$select_month_control.= '<option value="'.$x.'"'.($x != $month ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
}
$select_month_control.= '</select>';

/* select year control */
$year_range = 7;
$select_year_control = '<select name="year" id="year">';
for($x = ($year-floor($year_range/2)); $x <= ($year+floor($year_range/2)); $x++) {
$select_year_control.= '<option value="'.$x.'"'.($x != $year ? '' : ' selected="selected"').'>'.$x.'</option>';
}
$select_year_control.= '</select>';

/* "next month" control */
$next_month_link = '<a href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'" class="control">Next Month &gt;&gt;</a>';

/* "previous month" control */
$previous_month_link = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'" class="control">&lt;&lt; Previous Month</a>';


/* bringing the controls together */
$controls = '<form method="get">'.$select_month_control.$select_year_control.'&nbsp;<input type="submit" name="submit" value="Go" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$previous_month_link.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$next_month_link.' </form>';

/* get all events for the given month */
$events = array();
$query = "SELECT title, DATE_FORMAT(event_date,'%Y-%m-%D') AS event_date FROM events WHERE event_date LIKE '$year-$month%'";
$result = mysql_query($query,$dbcnx) or die('cannot get results!');
while($row = mysql_fetch_assoc($result)) {
$events[$row['event_date']][] = $row;
}

echo '<h2 style="float:left; padding-right:30px;">'.date('F',mktime(0,0,0,$month,1,$year)).' '.$year.'</h2>';
echo '<div style="float:left;">'.$controls.'</div>';
echo '<div style="clear:both;"></div>';
echo draw_calendar($month,$year,$events);
echo '<br /><br />';



Error I'm getting:
Notice: Undefined index: month in /home/user/public_html/filename.php on line 188
Notice: Undefined index: year in /home/user/public_html/filename.php on line 189
cannot get results!

Can someone please help?
Thank you very much!

AndrewGSW
08-04-2012, 08:29 PM
I assume it's this line:


$month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));

Try


if (isset($_GET['month'] and is_numeric($_GET['month'])) {
$month = (int) $_GET['month'];
} else {
$month = (int) date('m');
}

so that you are first checking the existence of the GET variable before using it.

Do something similar for $year.

xeirus
08-04-2012, 10:01 PM
I assume it's this line:


$month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));

Try


if (isset($_GET['month'] and is_numeric($_GET['month'])) {
$month = (int) $_GET['month'];
} else {
$month = (int) date('m');
}

so that you are first checking the existence of the GET variable before using it.

Do something similar for $year.

Thank you Andrew.
That was the first problem, now corrected.
And now I'm facing another problem.
I'm getting the following error:
Access denied for user 'db_username_here'@'localhost' to database 'db_name_here'

Do you think this has something to do with my hosting company?

Please advice.

AndrewGSW
08-04-2012, 10:10 PM
I assume 'localhost' is not appropriate for your live site. Your web host should have the correct host information.

Otherwise, the username is incorrect, or this user doesn't have the correct permissions for the database.

xeirus
08-04-2012, 10:29 PM
I assume 'localhost' is not appropriate for your live site. Your web host should have the correct host information.

Otherwise, the username is incorrect, or this user doesn't have the correct permissions for the database.

Ok Andrew. Thanks.
I will contact them and get back to let you know the conclusion.

xeirus
09-18-2012, 06:55 PM
Hi guys, its been over a month and I'm still struggling with this code.
The code now stands as shown below. The calendar is working.
BUT the events are not showing.
Can someone please help?
Thanks!



<?php
$host = "localhost";
//MySQL Database user name.
$login = "dbuser";
//Password for MySQL.
$dbpass = "dbpass";
//MySQL Database name.
$dbcnx = "dbname";
//Make connection to DB
$oConn=mysql_connect("$host","$login","$dbpass") OR DIE
("There is a problem with the system. Please notify your system administrator." .mysql_error());

mysql_select_db($dbcnx,$oConn) OR DIE
("There is a problem with the system. Please notify your system administrator." .mysql_error());
/* draws a calendar */
function draw_calendar($month,$year,$events = array()){

/* draw table */
$calendar = '<table cellpadding="0" cellspacing="0">';

/* table headings */
$headings = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
$calendar.= '<tr style="background: url(images/button_event_calendar_days_bar.jpg) no-repeat;height:20px;"><td class="calendar-day-head" style="font-family: Verdana, Arial, Helvetica, Tahoma, sans-serif;font-size: 9pt;font-weight: bold;" align="center">'.implode('</td><td class="calendar-day-head" style="font-family: Verdana, Arial, Helvetica, Tahoma, sans-serif;font-size: 9pt;font-weight: bold;" align="center">',$headings).'</td></tr>';

/* days and weeks vars now ... */
$running_day = date('w',mktime(0,0,0,$month,1,$year));
$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();

/* row for week one */
$calendar.= '<tr class="calendar-row">';

/* print "blank" days until the first of the current week */
for($x = 0; $x < $running_day; $x++):
$calendar.= '<td class="calendar-day-np" style="position:relative;height:33px;width:33px;background: url(images/button_event_calendar_empty_day_bg.png) no-repeat;">&nbsp;</td>';
$days_in_this_week++;
endfor;

/* keep going with days.... */
for($list_day = 1; $list_day <= $days_in_month; $list_day++):
$calendar.= '<td class="calendar-day" align="center" valign="middle"><div style="position:relative;height:33px;width:33px;background: url(images/button_event_calendar_day_bg.png) no-repeat;">';
/* add in the day number */
$calendar.= '<div class="day-number">'.$list_day.'</div>';

$event_day = $year.'-'.$month.'-'.$list_day;
if(isset($events[$event_day])) {
foreach($events[$event_day] as $event) {
$calendar.= '<div class="event" style="position:relative;height:33px;width:33px;background: url(images/button_event_calendar_events_day_bg.png) no-repeat;">'.$event['title'].'</div>';
}
}
else {
$calendar.= str_repeat('<p>&nbsp;</p>',2);
}
$calendar.= '</div></td>';
if($running_day == 6):
$calendar.= '</tr>';
if(($day_counter+1) != $days_in_month):
$calendar.= '<tr class="calendar-row">';
endif;
$running_day = -1;
$days_in_this_week = 0;
endif;
$days_in_this_week++; $running_day++; $day_counter++;
endfor;

/* finish the rest of the days in the week */
if($days_in_this_week < 8):
for($x = 1; $x <= (8 - $days_in_this_week); $x++):
$calendar.= '<td class="calendar-day-np" style="position:relative;height:33px;width:33px;background: url(images/button_event_calendar_empty_day_bg.png) no-repeat;">&nbsp;</td>';
endfor;
endif;

/* final row */
$calendar.= '</tr>';


/* end the table */
$calendar.= '</table>';

/** DEBUG **/
$calendar = str_replace('</td>','</td>'."\n",$calendar);
$calendar = str_replace('</tr>','</tr>'."\n",$calendar);

/* all done, return result */
return $calendar;
}

function random_number() {
srand(time());
return (rand() % 7);
}

/* date settings */
//$month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));
//$year = (int) ($_GET['year'] ? $_GET['year'] : date('Y'));
$month = (int) (isset($_GET['month']) ? $_GET['month'] : date('m'));
$year = (int) (isset($_GET['year']) ? $_GET['year'] : date('Y'));

/* select month control */
$select_month_control = '<select name="month" id="month">';
for($x = 1; $x <= 12; $x++) {
$select_month_control.= '<option value="'.$x.'"'.($x != $month ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
}
$select_month_control.= '</select>';

/* select year control */
$year_range = 7;
$select_year_control = '<select name="year" id="year">';
for($x = ($year-floor($year_range/2)); $x <= ($year+floor($year_range/2)); $x++) {
$select_year_control.= '<option value="'.$x.'"'.($x != $year ? '' : ' selected="selected"').'>'.$x.'</option>';
}
$select_year_control.= '</select>';

/* "next month" control */
$next_month_link = '<a href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'" class="control">Next Month &gt;&gt;</a>';

/* "previous month" control */
$previous_month_link = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'" class="control">&lt;&lt; Previous Month</a>';


/* bringing the controls together */
$controls = '<form method="get">'.$select_month_control.$select_year_control.'&nbsp;<input type="submit" name="submit" value="Go" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$previous_month_link.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$next_month_link.' </form>';

/* get all events for the given month */
$events = array();
//$query = "SELECT title, DATE_FORMAT(event_date,'%Y-%m-%D') AS event_date FROM events WHERE event_date LIKE '$year-$month%'";
$query = "SELECT title, DATE_FORMAT(event_date,'%Y-%m-%d') AS event_date FROM events WHERE event_date LIKE '$year-$month%'";
//$result = mysql_query($query,$dbcnx) or die('cannot get results!');
$result = mysql_query($query) or die('cannot get results!');
while($row = mysql_fetch_assoc($result)) {
$events[$row['event_date']][] = $row;
}

echo '<h2 style="float:left; padding-right:30px;">'.date('F',mktime(0,0,0,$month,1,$year)).' '.$year.'</h2>';
echo '<div style="float:left;">'.$controls.'</div>';
echo '<div style="clear:both;"></div>';
echo draw_calendar($month,$year,$events);
echo '<br /><br />';

?>

Fou-Lu
09-18-2012, 07:12 PM
This is difficult to follow since you keep switching back and forth between different branch syntax. Choose either :/endX or {} syntax; don't combine both.

print_r or var_dump the $events before passing it into the function. Do you have data to work with?

xeirus
09-18-2012, 07:28 PM
This is difficult to follow since you keep switching back and forth between different branch syntax. Choose either :/endX or {} syntax; don't combine both.

print_r or var_dump the $events before passing it into the function. Do you have data to work with?

Hi Fou-Lu,
So would it be:
print_r $events;

Yes, the db is created and 2 events posted in the events table.

xeirus
09-18-2012, 07:39 PM
Fou-Lu,
I did print_r ($events);
and the output is:
Array ( )

Fou-Lu
09-18-2012, 08:14 PM
Fou-Lu,
I did print_r ($events);
and the output is:
Array ( )

Is the print_r added somewhere down here?


$result = mysql_query($query) or die('cannot get results!');
while($row = mysql_fetch_assoc($result)) {
$events[$row['event_date']][] = $row;
}
print_r($events);

For example? If so, that indicates that you've fetched no matching results for the WHERE clause in your query.
Are these actually date/time datatype columns for the event_date? If so, it may be easier to compare a format of the datetype or pull just month/year (the SQL guys can suggest, typically converting a column isn't the best solution, but as a date I'm not sure what type of overhead would be involved in fetching the month and year separately).

You can also print the query out to display what the parsed query will be. That can then be taken and executed directly in the mysql client or using PHPMyAdmin or whatever to verify that it works (which it looks like it does not so long as the print_r is where I think you put it).

xeirus
09-18-2012, 09:34 PM
I had added print_r($events); at the very bottom of the code.
However, I've tried adding the print_r($events); also where you mentioned.
Same results.
The table "events" looks like this:
id int(11)
title varchar(100)
event_date date

Here are the two records I have in the above table:
1 Fun Fridays 2012-09-27
2 Sunny Sundays 2012-09-25

Here's what I get with print_r($query);
SELECT title, DATE_FORMAT(event_date,'%Y-%m-%d') AS event_date FROM events WHERE event_date LIKE '2012-9%'

Fou-Lu
09-18-2012, 09:50 PM
Offhand, I don't know if you can do this: LIKE '2012-9%' . It may consider it only as a string value, which cannot logically compare to a date.
Try it as so: WHERE MONTH(event_date) = $month AND YEAR(event_date) = $year. If that works, then it simply doesn't like treating date as a string (alternatively you can format it). If that does work, I'd check with the SQL guys as to what the best approach is to keep it running as optimal as possible.
BTW, a perhaps better approach which wouldn't require the use of the conversion at all would be to give it a full date and as for a BETWEEN: event_date BETWEEN '2012-09-01' AND '2012-09-30'. There are several ways to get the first and last day of a given month (manually, using strtotime, etc). I'd suggest strtotime is the easiest as you can do $lastDay = strtotime('+1 month', $firstDay);

xeirus
09-18-2012, 10:03 PM
Ok now I'm getting these:

Array ( [2012-09-27] => Array ( [0] => Array ( [title] => Fun Fridays [event_date] => 2012-09-27 ) ) [2012-09-25] => Array ( [0] => Array ( [title] => Sunny Sundays [event_date] => 2012-09-25 ) ) )

SELECT title, DATE_FORMAT(event_date,'%Y-%m-%d') AS event_date FROM events WHERE MONTH(event_date) = 9 AND YEAR(event_date) = 2012

So its better than previous approach.

Now what should I do?

I'm seeing your other suggestions but frankly, I don't know how to make use of them. I'm just a newbie at PHP. Sorry.

Fou-Lu
09-19-2012, 12:19 AM
You can make the month and year with a simple mktime:


$firstDay = mktime(0, 0, 0, $month, 1, $year);
$lastDay = strtotime('+1 month', $firstDay);

Those can be used for the BETWEEN values, using BETWEEN '$firstDay' AND '$lastDay'. IF that works, you should use that.

So with what you have now, the data is good. Now its the functions fault.
I'm a bit busy today to go through this step by step. But this here stands out:


$event_day = $year.'-'.$month.'-'.$list_day;

Looking at $month, it's provided directly to the function, and is cast from main as:


/* date settings */
$month = (int) (isset($_GET['month']) ? $_GET['month'] : date('m'));
//...
echo draw_calendar($month,$year,$events);

Integers are NEVER 0 lead.

Change this line in the function: $event_day = $year.'-'.$month.'-'.$list_day; to $event_day = date('Y-m-d', mktime(0, 0, 0, $month, $list_day, $year));. You may want to print the $event_day before trying to fetch it as well to make sure it comes out looking correct.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum