home > web

include vs. include_once in PHP

posted on 2014-10-08 by Spencer Bravo

A few months ago I was building this site and while looking up how to do something, I noticed the person whose code I was looking at used:

include_once($_SERVER['DOCUMENT_ROOT']."/file.php");

Rather than what I was using:

include($_SERVER['DOCUMENT_ROOT']."/file.php");

I wasn't really sure what the difference was so I did the research. When you use "include" you tell the document just to include the file right there no matter what. When you use "include_once" if the file has been already included in that page it will skip over that command. For example: if you're workin gon a site and you have a seperate header file which you "include" into both your "config" and "index" page that means that if you include the "config" file in the "index" page it will "include" the header twice. Though if you use "include_once" it will understand that the header has already been included and skip it.

I hope this post is useful! If you have any questions or comments please don't hesitate to email us at CSTeam@CodeShrub.com

posted in web

Active Tab Navbar In PHP

posted on 2014-09-21 by Spencer Bravo

This post is about how I set up my navbars for websites. It enables the navbar to be highlighted based on the page you're on and doesn't require you to put a different <nav> section on every page. This setup also gives the ability to create the header on a completely different file (header.inc.php), while still making the navbar active based on the page.

Now to the code:

<?php
$category_explode = explode("/",$_SERVER['REQUEST_URI']);
$site = $category_explode[0];
$page = $category_explode[1];
$post = $category_explode[2];
$nnav = " ";
$nav = '<ul class="nav navbar-nav">
           <li class="inactive"><a href="/">Home</a></li>
           <li class="inactive"><a href="/bash/">Bash</a></li>
           <li class="inactive"><a href="/viml/">VimL</a></li>
           <li class="inactive"><a href="/web/">Web</a></li>
           <li class="inactive"><a href="/about/">About</a></li>
        </ul>';
switch ($page) {
    case "":
        $nnav = str_replace("<li class=\"inactive\"><a href=\"/\">Home</a></li>","<li class=\"active\"><a href=\"/\">Home</a></li>",$nav);
    break;
    case "bash":
        $nnav = str_replace("<li class=\"inactive\"><a href=\"/bash/\">Bash</a></li>","<li class=\"active\"><a href=\"/bash/\">Bash</a></li>",$nav);
    break;
    case "viml":
        $nnav = str_replace("<li class=\"inactive\"><a href=\"/viml/\">VimL</a></li>","<li class=\"active\"><a href=\"/viml/\">VimL</a></li>",$nav);
    break;
    case "web":
        $nnav = str_replace("<li class=\"inactive\"><a href=\"/web/\">Web</a></li>","<li class=\"active\"><a href=\"/web/\">Web</a></li>",$nav);
    break;
    case "about":
        $nnav = str_replace("<li class=\"inactive\"><a href=\"/about/\">About</a></li>","<li class=\"active\"><a href=\"/about/\">About</a></li>",$nav);
    break;
    default:
        $nnav = $nav;
    break;
}
?>

You can view the most current version of this code at https://github.com/spravo13/active-tab-navbar-codeshrub

All I did was create a variable ($nav) to store the basic navigation and then based on what page the user is on I used a switch statement to edit $nav and store the changes in the variable $nnav.

Then all you have to do is include the variable "$nnav" where you want your navigation. Heres an example of using it(this example is later on in the same file, if you don't include your navigation in a seperate file, just include the file with this code in the file you want.

<div class="nav">
    <?php
        echo $nnav;
    ?>
</div>

I hope this post is useful! If you have any questions or comments please don't hesitate to email us at CSTeam@CodeShrub.com

posted in web

Connection to MySQL Database in PHP

posted on 2014-09-15 by Spencer Bravo

This post explains how I connect to MySQL Databases when writing in PHP. This post will mostly be used as a post I will refer to in later MySQL posts, so I won't need to include in those posts how to connect and I can get to the point. For those of you who will need this post at some place in those tutorials I will be sure to include a link to this post.

Now to the code! So when I set up my connection I acctually include it in a seperate file in a seperate directory. The directory is /inc in which I place all the files I will use by including them in other pages. In this directory I include files such as my header, footer, and my config file (in which I include the MySQL database connect setup). This file is named "config.inc.php". This is what the database connect part looks like:

<php
$db_host = ' '; // location of your database(localhost ussualy works)
$db_user = ' '; // your MySQL database username
$db_password = ' '; // your MySQL database password
$database = ' '; // name of your MySQL database

$db_connect = mysql_connect($db_host, $db_user, $db_password);
$db = mysql_select_db($database,$db_connect);
?>

You can view the most current version of this code at https://github.com/spravo13/mysql-db-connect-php-codeshrub

Then all you have to do is include this file anywhere you want to use the database and your variables will be there. Heres an example of using these variables. Note: I have already included the config file earlier on in this following code.

<php
$post_select = "SELECT * FROM post WHERE name = 'phil' LIMIT 1";
$post_query = mysql_query($post_select,$db_connect) or die($db_connect);
while ($post_row = mysql_fetch_assoc($post_query)){
}
?>

Here it is looking for a post named phil. As you see we use the variable "db_connect".

I hope this post is useful! If you have any questions or comments please don't hesitate to email us at CSTeam@CodeShrub.com

posted in web