PHP shortcuts for the non DEV ("ELSEIF")

PHP-shortcuts

Previously - I showed you how to do a simple include file in php.

<?php include ('folder/filename.php'); ?>

Now - we are going to do something a little more advanced.

Let's create a function that determines what page we are on and places a special class on the navigation element to show an active state.

I'm not going to complicate this post by doing a lot of CSS and HTML - I am going to assume you know these things and are just looking for ways to make your life easier.

Let's get started.

function pageName() {
 return
 substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}

So - again - without explaining a bunch of PHP, the above string of code essentially creates a function called "pageName" and puts the name of the current page into the function.

To test that... You could create a page called test.php

In the body tags of the page include the above lines of code followed by this line of code.

echo "The current page name is ".pageName();

The above line of code uses the word "echo" to mean that you want to show the copy (inside the quotes) on your webpage.

The .pageName(); Is how you call your function. the period before is concatenating what ever was in your function to the end of the statement shown in quotes.

If you were to run this page - It would simply say:

The current page name is file-name.php

Where file-name.php represents whatever you elected to save the page as.

OK - Let's get back to it.

Say you have three pages in another html file -

  • index.php 
  • about.php
  • contact.php
<ul class="navbar-nav">

  <li class="active"><a href="index.php">Home</a></li>

  <li><a href="about.php">About</a></li>

  <li><a href="contact.php">Contact</a></li>

</ul>

Your navigation looks something like this in your html page:

We are going to change the class based on what page you are on:
back to your php file we are going to add an if/elseif statement.

It looks like this:This statement tells the computer. If the page we are on is called 'index.php' write this line of code.

if (pageName() == 'index.php'):
    echo 'THIS IS WHERE WE PLACE WHAT HAPPENS';

So the first part, using our example - looks like this:

if (pageName() == 'index.php'):

    echo '

<ul class="navbar-nav">

  <li class="active"><a href="index.php">Home</a></li>

  <li><a href="about.php">About</a></li>

  <li><a href="contact.php">Contact</a></li>

</ul>

';

So we are saying here - If the current page name is equal to index.php,

if (pageName() == 'index.php'):

do this

echo '';

In this case we are going to show the html for the navigation with an active class on the "HOME" section of our navigation.

(IMPORTANT - You have to open and close the echo statement with single quotes in this instance otherwise your file will read the first first double quote in your html code and think you are done. double quotes work in other instances...but I'm not going to complicate this by running you through that. )

Next we are going to include what happens for the other pages by writing the "elseif" part of the statement.

elseif(pageName() == 'about.php'):
    echo 'THIS IS WHAT HAPPENS if we are on the about.php page';

SEE?!?! this is essentially the same as above.

If the page is "index.php" then we do xyz
elseif the page is "something else" we do other things....

In the name of time and space - I am going to finish this statement and I want you to see if it makes sense to you.

The whole statement is going to look like this:

<?phpfunction curPageName() { return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);}
if(curPageName() == 'index.php'):
    echo '
<ul class="navbar-nav">
  <li class="active"><a href="index.php">Home</a></li>
  <li><a href="about.php">About</a></li>
  <li><a href="contact.php">Contact</a></li>
</ul>
';
elseif(pageName() == 'about.php'):
    echo '
<ul class="navbar-nav">
  <li><a href="index.php">Home</a></li>
  <li class="active"><a href="about.php">About</a></li>
  <li><a href="contact.php">Contact</a></li>
</ul>
';
elseif(pageName() == 'contact.php'):
    echo '
<ul class="navbar-nav">
  <li><a href="index.php">Home</a></li>
  <li><a href="about.php">About</a></li>
  <li class="active"><a href="contact.php">Contact</a></li>
</ul>
';
endif;
?>

It might look a little intimidating but it really isn't - We basically say

If the page is the index.php page...We show the navigation that has an active class on index.php

If the page is the about.php page...We show the navigation that has an active class on about.php

If the page is the contact.php page...We show the navigation that has an active class on contact.php

and then we end it by saying... endif; and then we close the php statement.

?>

Now you have a php file you can save as ...for instance... "navInc.php" in your include folder.

Do you remember how to call that file?

<?php include ('includes/navInc.php'); ?>

so your html page should look like this now.

<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<?php include ('incldues/navInc.php'); ?>
</body>

</html>

Give this a try and let me know if you have problems