Page 1 of 1
Simple noob IF statement questions.
Posted: Thu Jun 18, 2009 11:17 pm
by SteveMullen
Hello, I have a mostly html static website. I have it split into a few section using php includes for ease of updating. I have some scripts that I only want to load on certain pages, how to I tell them to only load for that one page? In header.php for example, how can I say "if pageX.php - do this"
so baiscally it would look something like this:
Code: Select all
<html>
<head>
<!-- scripts for all pages --->
<script type="text/javascript" src="/js/script1.js"></script>
<script type="text/javascript" src="/js/script2.js"></script>
<!-- scripts for pageX --->
<?php if page=pageX.php{
<script type="text/javascript" src="/js/scriptForPageX.js"></script>
?>
</head>
<body>
</body>
</html>
Any help is appreciated. Thanks a lot.
-Steve
Re: Simple noob is statement questions.
Posted: Fri Jun 19, 2009 7:32 am
by jaoudestudios
Use the server array $_SERVER.
I think there is a script_name, i.e. $_SERVER['script_name']
Re: Simple noob IF statement questions.
Posted: Fri Jun 19, 2009 9:21 am
by SteveMullen
jaoudestudios, thanks for the tip! Do you have an example of this in use somewhere? I am going to need some hand holding on this one. I am not a programmer, always just been a front end guy, so this is new territory for me. An example on how you would actually write it out on the page would help a ton, or a link to examples.
Thanks again!
-Steve
Re: Simple noob IF statement questions.
Posted: Fri Jun 19, 2009 9:28 am
by jaoudestudios
Something like this...
Code: Select all
<?php
if ($_SERVER["SCRIPT_NAME"] == 'pageX.php')
{
<script type="text/javascript" src="/js/scriptForPageX.js"></script>
}
?>
Something more dynamic would be possible, where the script (page) name creates the js filename (path) instead of using conditional statements.
Re: Simple noob IF statement questions.
Posted: Fri Jun 19, 2009 9:32 am
by Loki
Try this. You can use it for whatever html/javascript content you need.
It's a little "unkempt" in terms of php, but it will be fine for what you need.
Code: Select all
<html>
<head>
<!-- scripts for all pages --->
<script type="text/javascript" src="/js/script1.js"></script>
<script type="text/javascript" src="/js/script2.js"></script>
<?php
if($_SERVER['script_name'] == "home.php") {
?>
----------- Stick whatever content you want to be on home.php here --------------------
<?php
} elseif($_SERVER['script_name'] == "members.php") {
?>
----------- Stick whatever content you want to be on members.php here --------------------
<?php
}
?>
</head>
<body>
</body>
</html>
Re: Simple noob IF statement questions.
Posted: Fri Jun 19, 2009 9:43 am
by SteveMullen
Perfect! That is what I was looking for. I know this is in no way a proper way to build a php page, but for what I am doing it works. I will try this out later today. Thanks for all the advice!