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):
PHP Code:
/* draws a calendar */
function draw_calendar($month,$year,$events = array()){
/* 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"> </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>';
/* 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"> </td>';
endfor;
endif;
/* bringing the controls together */
$controls = '<form method="get">'.$select_month_control.$select_year_control.' <input type="submit" name="submit" value="Go" /> '.$previous_month_link.' '.$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;
}
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!
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.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
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?
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.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
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 Code:
<?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()){
/* 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;"> </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>';
/* bringing the controls together */
$controls = '<form method="get">'.$select_month_control.$select_year_control.' <input type="submit" name="submit" value="Go" /> '.$previous_month_link.' '.$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;
}
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?
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.
Fou-Lu,
I did print_r ($events);
and the output is:
Array ( )
Is the print_r added somewhere down here?
PHP Code:
$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).
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%'
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);
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:
PHP Code:
$event_day = $year.'-'.$month.'-'.$list_day;
Looking at $month, it's provided directly to the function, and is cast from main as:
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.