View Full Version : Error in processing - Undefined Variable

10-08-2007, 11:10 PM
I am having a problem with some php not evaluating the way I would like. On a page that is included on many different pages, I have the following code:

<a href="http://www.albany.k12.or.us/wahs/teacherpages/welshstuart/class1/calendar/<?php if ($addLevel) {include("../../../currentmonth.php");} else {include("../../currentmonth.php");} ?>.php">Calendar</a>

And on the pages that I have the above code included, addLevel is either set to 1 or not defined at all. For a while this system worked great. Just this past weekend however some work was done on the server that I use and now I get the following error message: "Warning: undefined variable $addLevel..."

Anyone see what I am doing wrong? Here is a URL where it is being used:


Click on one of the classes and then on 'Calendar'

10-08-2007, 11:32 PM
I have figured out that if I define addLevel on all pages, but set it to 0 where I do not want the extra level, then this fixes my problem. However, this code is used on quite a few pages and it would not be as practical to define the variable on every page. If someone knows of an easier fix that would be greatly appreciated.

10-08-2007, 11:35 PM
That code seems to be missing on that page. You're getting the error because the variable isn't set before you attempt to use it. You should either pre-define it at the top of your page or use "@" to suppress the error. What exactly are you trying to do with that code? It seems like it would either produce an error or at least not the output you intend, to me..

10-09-2007, 01:05 AM
The code is used for teacher pages that I have created at my high school. In the navigation for their class, I want the month that is loaded when someone visits their calendar to be controlled dynamically. Thus I created a file outside their folders called currentmonth.php. At the end of the month, we can change this file to whatever the next month is and the month loaded will be changed for all teachers.

I came across a problem though when I was implementing this. The location of the currentmonth file is different on their calendar pages from the rest of their pages since all the files for the month are inside a 'calendar' folder. To address this I created a variable, addLevel, which is only defined on the calendar pages. In the navigation file, which is included on all of the pages for that class, the currentmonth file is included from three levels up only if addLevel exists. Otherwise, it is included from two levels up. This worked great until this weekend when some work was done on the server. Im not sure exactly what was done but the above described solution no longer works.

This may help you visualize the hierarchy:

- Main directory

-- teacherpages

--- teachername

---- class1

----- calendar

10-09-2007, 01:51 AM
Change your "if" statement to check to see if the variable is set before you use it.

if (isset($addLevel) && $addLevel) {

10-09-2007, 02:26 AM
Excellent. Thanks for your help.