Simple noob IF statement questions.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SteveMullen
Forum Newbie
Posts: 3
Joined: Thu Jun 18, 2009 11:01 pm
Location: Tulsa, Oklahoma

Simple noob IF statement questions.

Post 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
Last edited by SteveMullen on Fri Jun 19, 2009 8:44 am, edited 1 time in total.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Simple noob is statement questions.

Post by jaoudestudios »

Use the server array $_SERVER.

I think there is a script_name, i.e. $_SERVER['script_name']
SteveMullen
Forum Newbie
Posts: 3
Joined: Thu Jun 18, 2009 11:01 pm
Location: Tulsa, Oklahoma

Re: Simple noob IF statement questions.

Post 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
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Simple noob IF statement questions.

Post 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.
Loki
Forum Commoner
Posts: 25
Joined: Wed Jun 03, 2009 9:23 pm

Re: Simple noob IF statement questions.

Post 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>
 
Last edited by Loki on Fri Jun 19, 2009 9:58 am, edited 1 time in total.
SteveMullen
Forum Newbie
Posts: 3
Joined: Thu Jun 18, 2009 11:01 pm
Location: Tulsa, Oklahoma

Re: Simple noob IF statement questions.

Post 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!
Post Reply