Jump to content

Recommended Posts

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"> </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> </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 < :
	for($x = 1; $x <= (8 - $days_in_this_week); $x++):
		$calendar.= '<td class="calendar-day-np"> </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 >></a>';

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


/* 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;
}

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!

 

Link to comment
https://forums.phpfreaks.com/topic/266683-events-calendar-code-not-working/
Share on other sites

Change the GET date section to this.

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

 

Thank you Drummin.

That took care of the error.

But now I'm getting:

cannot get results!

 

Any idea why that would be?

 

The above error would be the outcome if $dbcnx was not there or not working but I have it in there.

 

You need to make a database connection.  Near the top of the page add this filling in missing values.

$host = "localhost"; 
//MySQL Database user name.
$login = "";
//Password for MySQL.
$dbpass = "";
//MySQL Database name.
$dbcnx = "";
//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());

You need to make a database connection.  Near the top of the page add this filling in missing values.

$host = "localhost"; 
//MySQL Database user name.
$login = "";
//Password for MySQL.
$dbpass = "";
//MySQL Database name.
$dbcnx = "";
//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());

 

Thanks again Drummin.

I added your code, its just above the main code and this is the error I'm getting:

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.

 

Have you created a database and user with password for this DB with your hosting service?

 

It's not too hard to do if you have Cpanal on your hosting service.

Just click create database and give it a name.  (write it down)

Then create a user who has access for this DB, giving it a name and password.  (write these down)

 

These three "names" need to be added to that opening DB connection script.

 

IF your event calendar came with a SQL file to create tables then you should be able to go to your Cpanal, select your DB and click the import tab and upload the table(s) to your DB.  Otherwise you will need to create the tables adding the needed field names and types.  The table is called "events" without quotes and the obvious fields would be

 

id

int(11)

  AUTO_INCREMENT

 

title

  varchar(25)

[/td]

 

event_date

  varchar(10)

[td]

 

After adding the table has been added, see about running your script again.

 

Note the script really should have come with documentation, needed files and an example.

Depending on how you are saving the date in the DB you might need to change the format in the query.  I would probably use YYYY-mm-dd in which case the query should be.

$query = "SELECT title, DATE_FORMAT(event_date,'%Y-%m-%d') AS event_date FROM events WHERE event_date LIKE '$year-$month%'";

Have you created a database and user with password for this DB with your hosting service?

 

It's not too hard to do if you have Cpanal on your hosting service.

Just click create database and give it a name.  (write it down)

Then create a user who has access for this DB, giving it a name and password.  (write these down)

 

These three "names" need to be added to that opening DB connection script.

 

IF your event calendar came with a SQL file to create tables then you should be able to go to your Cpanal, select your DB and click the import tab and upload the table(s) to your DB.  Otherwise you will need to create the tables adding the needed field names and types.  The table is called "events" without quotes and the obvious fields would be

 

id

int(11)

  AUTO_INCREMENT

 

title

  varchar(25)

[/td]

 

event_date

  varchar(10)

[td]

 

After adding the table has been added, see about running your script again.

 

Note the script really should have come with documentation, needed files and an example.

 

Hi Drummin,

Yes, the DB was created with those fields.

Finally it was the hosting company's problem. They had to add the rights for me to use the DB although I had created a DB user and added that in cPanel as well.

But anyway, the previous error is gone.

The PHP script didn't come with a SQL script but the author had made a statement in his post that one needs to create the SQL Table with a couple of fields title (which I have kept as text) and event_date needs to be set as DATE or DATETIME, I used DATE

And I went a step ahead and added id with auto increment.

 

Have you created a database and user with password for this DB with your hosting service?

 

It's not too hard to do if you have Cpanal on your hosting service.

Just click create database and give it a name.  (write it down)

Then create a user who has access for this DB, giving it a name and password.  (write these down)

 

These three "names" need to be added to that opening DB connection script.

 

IF your event calendar came with a SQL file to create tables then you should be able to go to your Cpanal, select your DB and click the import tab and upload the table(s) to your DB.  Otherwise you will need to create the tables adding the needed field names and types.  The table is called "events" without quotes and the obvious fields would be

 

id

int(11)

  AUTO_INCREMENT

 

title

  varchar(25)

[/td]

 

event_date

  varchar(10)

[td]

 

After adding the table has been added, see about running your script again.

 

Note the script really should have come with documentation, needed files and an example.

 

Changed event_date to VARCHAR(10)

Wow! It worked like a charm, Drummin

Changed those two lines and the calendar is showing.

Just one question ...

In this line:

$result = mysql_query($query) or die('cannot get results!');

Can I put something like this:

$result = mysql_query($query) or die('cannot get results! .mysql_error()');

So if there's an error then I would know the exact error.

I really liked your use of that mysql_error() in your DB Login example, that's what helped the hosting people find the problem so easily (by telling us openly that the user didn't have enough rights).

 

You want to quote the "text" not the mysql_error().

$result = mysql_query($query) or die("cannot get results!" .mysql_error());

 

Edit:  I made my own event calendar not long ago.  Image attached.

 

Thanks a lot, Drummin! Done :)

 

Your calendar looks nice and clean. Would you mind sharing your code?

But if not, its ok, I understand some people don't like making their code public.

 

Also, one more question, hopefully last:

Is there an easy way to add the week number in the code we have above?

Or would that require a lot of programming?

 

Kind Regards,

Xeirus.

  • 1 month later...

Hi Drummin and PFMaBiSmAd,

I've been breaking my head with this for over a month but I can't get the code to display the events I have entered in the DB.

I've also tried changing event_date to DATE but still, it doesn't pull out the events.

Please help!

Quick run through of what is needed:

  • Do you have error reporting turned on? If not, turn it on.
  • Do you get any error messages? If so, please post them here.
  • What's the result from the query/code? Please post what you get, and what you expect to get.
  • How does your code look like now? The relevant parts, properly intended and in code blocks.

If you go through that list you might be able to figure out the problem yourself, if not it'll at least help us immensely in helping you figure it out.

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.