/    Sign up×
Community /Pin to ProfileBookmark

undefined variable in php script

My calendar stop appearing on the page and I dont know what its causing the probleming?

I am getting the following error messages:

[B]
Connected to MySQL
Connected to Database
Notice: Undefined variable: days_in_this_week in C:xampphtdocswebcalendar.php on line 114

Notice: Undefined variable: days_in_this_week in C:xampphtdocswebcalendar.php on line 115

Notice: Undefined variable: calendar in C:xampphtdocswebcalendar.php on line 116

Notice: Undefined variable: days_in_this_week in C:xampphtdocswebcalendar.php on line 115

Notice: Undefined variable: days_in_this_week in C:xampphtdocswebcalendar.php on line 115

Notice: Undefined variable: days_in_this_week in C:xampphtdocswebcalendar.php on line 115

Notice: Undefined variable: days_in_this_week in C:xampphtdocswebcalendar.php on line 115

Notice: Undefined variable: days_in_this_week in C:xampphtdocswebcalendar.php on line 115

Notice: Undefined variable: days_in_this_week in C:xampphtdocswebcalendar.php on line 115

Notice: Undefined variable: days_in_this_week in C:xampphtdocswebcalendar.php on line 115

Notice: Undefined variable: days_in_this_week in C:xampphtdocswebcalendar.php on line 115
[/B]

Heres my php script:

[code=php] <?php
mysql_connect(“localhost”, “root”, “”) or die(mysql_error());
echo “Connected to MySQL<br />”;
mysql_select_db(“events”) or die(mysql_error());
echo “Connected to Database”;

/* 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=”‘.$dayClass.'”>’.$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;

// check today day & the current month
$calDayClass=”calendar-day”;
$dayClass=”day-number”;
if ($list_day == date(‘j’) && $month == date(‘m’)) {
$dayClass=”today”;
$calDayClass=”today-day”;
}

$calendar.= ‘<td class=”‘.$calDayClass.'”>’;

/** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !! IF MATCHES FOUND, PRINT THEM !! **/
$calendar.= str_repeat(‘<p>&nbsp;</p>’,2);

$calendar.= ‘</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++;
}

/* 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() &#37; 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,$db_link) 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 />’;

/* Current month and year */
$year = date (‘Y’); $month_title = date (‘F’);
$month_display = date (‘n’);

/* sample usages */
echo ”.$month_title.’&nbsp;’.$year.”;
echo draw_calendar($month_display,$year);
?> [/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@salmanshafiqOct 09.2011 — can you please check your query getting any record from database or not?
Copy linkTweet thisAlerts:
@rusty813authorOct 09.2011 — the database i have a created is blank right now, but before i modified the coding by adding the mysql stuff as followed on this site http://davidwalsh.name/php-event-calendar it the calendar was showing
Copy linkTweet thisAlerts:
@NogDogOct 09.2011 — I would guess the problem is here:
[code=php]
}

/* 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;
[/code]

The problem is that the closing "}" at the start of the above code snippet closes the function definition for draw_calendar(), where those variables are defined and used. Once you are out of that function, those variables are no longer in scope, and thus are not defined. It's not clear to me at a quick glance whether the problem is that the offending lines should be part of the function definition, or if you instead need to define those variables outside of the function so that they can be used there.
Copy linkTweet thisAlerts:
@rusty813authorOct 09.2011 — I moved the bracket to end of the last coding, there are no errors now but still the calendaring isnt showing on the screen

[code=php] /* sample usages */

echo ''.$month_title.'&nbsp;'.$year.'';

echo draw_calendar($month_display,$year);
}[/code]
Copy linkTweet thisAlerts:
@rusty813authorOct 11.2011 — so any ideas on how to get the calendar showing again?
Copy linkTweet thisAlerts:
@SecarBlueOct 11.2011 — Will try to help you out, gimme a couple mins ?
Copy linkTweet thisAlerts:
@rusty813authorOct 11.2011 — thanks alot secarblue, appreciate...yea i tried contacting the owner of script on the site but no response from guess he forgot about the whole site altogether
×

Success!

Help @rusty813 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...