View Full Version : variable scope
Osterling
10-24-2005, 12:13 AM
Well I running into a problem and I could use some advice. Let me explain how I'm setting up my site and what exactly it is that is giving me trouble.
I have a file which includes only 4 php include statements. One for the the header, nav, body and footer. Each contain part of the style and when all files are together it creates my style. Simple.
Well my body.php page is where my text is. I use an include statement to bring in file it is suppose to retrieve, and within that file is a variable which contains a value "page title".
I took the variable $titleofpage and used as an argument in my function I created.
That variable named "titleofpage" scope isn't working in my navigation where I call a function that I created on index.php file. I call the function on the nav.php file but the page title doesn't show. I am thinking it has something to do with my variable scope.
Any help would be appreciate, and I hope I made myself clear.
Guest190829
10-24-2005, 12:19 AM
try
die("$titleofpage")
on different parts of the scripts, to see where the exact problem is.
Osterling
10-24-2005, 12:44 AM
alright. i defined the variable in a html file, from there that file is called on from the body.php file in which it will show the variable. however when i use the variable in my nav.php file it won't show its value, and both body.php and nav.php are put together from include statements coming from the index.php file if that doesn't make sense let me know.
but how come it works fine for the body.php file but not nav.php?
harmor19
10-24-2005, 01:19 AM
you mentioned you're using functions?
If so use this as an example..
<?php
$titleofpage = "title of page";
function somefunction()
{
global $titleofpage;
echo "$titleofpage";
}
?>
Osterling
10-24-2005, 02:16 AM
here is a little outline of my php pages and to help give a better idea of what it is i am needing help.
index.php (contains all include statements to execute the files below in that order)
- header.php
- nav.php (when I try to call the pagetitle function here to run it, it says undefined)
- body.php (if I call the function here it will execute perfectly)
>> articles(inserted through the php include statement > article 1 (which contains the function pagetitle)
- footer.php
how can i get it so that the function i created in the article 1.html file will run and execute in nav.php
any help is always appericated.
harmor19
10-24-2005, 02:26 AM
I'm cunfused...
how can you make a function in an html file (article 1.html)?
can you make small mock ups of each script?
I'll have to test them out because I can't solve it.
sorry.
Osterling
10-24-2005, 02:33 AM
i have it inserted in <?php ?> tags. the function runs in body.php but not in nav.php but i've changed it to .php now.
it maybe a sloppy way to code, i'm just beginning my coding experince with php :-/
if you want i can post each page of code so you can get a good idea, if that is what your asking for.
index.php
<?php
include ("header.php");
include ("articles/top.php");
include ("articles/body.php");
include ("footer.php");
?>
nav.php
<TD COLSPAN=12>
<IMG SRC="images/layout_02.gif" WIDTH=605 HEIGHT=12 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=3>
<IMG SRC="images/layout_03.gif" WIDTH=48 HEIGHT=23 ALT=""></TD>
<TD>
<IMG SRC="images/layout_04.gif" WIDTH=165 HEIGHT=23 ALT=""></TD>
<TD>
<IMG SRC="images/layout_05.gif" WIDTH=12 HEIGHT=23 ALT=""></TD>
<TD COLSPAN=3>
<IMG SRC="images/layout_06.gif" WIDTH=117 HEIGHT=23 ALT=""></TD>
<TD COLSPAN=4>
<IMG SRC="images/layout_07.gif" WIDTH=263 HEIGHT=23 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=3>
<IMG SRC="images/layout_08.gif" WIDTH=48 HEIGHT=48 ALT=""></TD>
<TD COLSPAN=4>
<IMG SRC="images/layout_09.gif" WIDTH=283 HEIGHT=48 ALT=""></TD>
<TD COLSPAN=2>
<IMG SRC="images/layout_10.gif" WIDTH=177 HEIGHT=48 ALT=""></TD>
<TD COLSPAN=3>
<IMG SRC="images/layout_11.gif" WIDTH=97 HEIGHT=48 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=2>
<IMG SRC="images/layout_12.gif" WIDTH=36 HEIGHT=47 ALT=""></TD>
<TD COLSPAN=4 align=left>
<?php echo pagetitle(); ?></td>
<td colspan=4 align=right>Table Row 2</td>
<TD COLSPAN=2>
<IMG SRC="images/layout_14.gif" WIDTH=86 HEIGHT=47 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=12>
<IMG SRC="images/layout_15.gif" WIDTH=605 HEIGHT=23 ALT=""></TD></TR><tr>
body.php
<td colspan=3 background="images/layout_16.gif">
<img src="images/layout_16.gif" width=48 height=12 alt="">
</td>
<td name=body colspan=7 rowspan=2 bgcolor="#0000FF" valign="top" style="padding-left: 4; padding-right: 4; padding-top: 4; padding-bottom: 4">
<?php
if (!$_GET['article'])
{
include ('article/home.php');
}
else
{
$pagetitle=$_GET['article'];
switch($pagetitle)
{
case "home":
include ('article/home.php');
break;
case "theaccident":
include ('article/theaccident.php');
break;
default: }
}
?>
</td>
<TD COLSPAN=2 background="images/layout_18.gif">
<IMG SRC="images/layout_18.gif" WIDTH=86 HEIGHT=12 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=3 background="images/layout_19.gif">
<IMG SRC="images/layout_19.gif" WIDTH=48 HEIGHT=236 ALT=""></TD>
article.php
<?php
$titleofpage = "The Accident";
function pagetitle()
{
global $titleofpage;
echo "Where You're At: <br> <strong>Index > The Guide > ".$titleofpage."</strong>";
}
?>
<p class="MsoNormal" style="text-indent:.5in;line-height:150%">
I think we all have a moment when we see and acknowledge that we are
indeed exposed to the real possibility of something extremely bad
happening to us. That if we don?t start considering the consequences
to our actions, we may regret what happens to us as a ripple affect
from them. For me, It was never like I thought I was immune to the
bad things, I just never thought it would happen to me. It was too
unlikely in my eyes, so when the truck swerved I never could have
guessed what would be coming next. I have always learned best by
experience, and I guess this lesson was no different. </p>
<p class="MsoNormal" style="text-indent:.5in;line-height:150%">It
was Friday, and school had just let out. Instead of taking a ride
from Brain I decided to get a ride from Ben. In the truck there was
Ben, Matt, Chad and me. Ben and Matt were sitting in the front,
while Chad and I sat in the back. We pulled out onto Fifth Street
and I looked out my window. I remember seeing kids and then I felt a
jerk. I slide to the middle of the back seat, and began to look
through the windshield to see what was going on. It had been a van
that pulled out in front of us that had caused Ben to swerve. I
remember thinking to myself; I hope I make it out alright and then
time slowed down dramatically. Looking through the windshield I
could see the kids ahead, and before I had time to think ?what?s
going to happen? I felt it. It felt as if we were hitting trees and
running them over, but instead of trees they were kids. Then I felt
the kids under the truck as it rolled over on them, and I remember
hearing the screams from the people witnessing it. The sound of the
screaming made me think back to Columbine, so when the truck finally
stopped I was terrified of what just happened. I knew it was bad,
if not from what I felt, what I saw but just from what I heard.</p>
<p class="MsoNormal" style="text-indent:.5in;line-height:150%">It
was then I realized how fast things can change, and without your
control. How important it is to understand your actions may affect
someone else and how unfair that might be to them. I?ve learned
that I am mortal, that bad things can happen to me and that I need
to be aware of the dangers and do my best to prevent them.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.