My History of Shell Prompts
posted on 2014-12-08 by Spencer Bravo
As odd as it might seem, until recently I was using the basic, out of the box, shell prompt. The code for this, in my bashrc, was just the basic:
PS1='\u@\h \w \$'
If you don't know what PS1 or bash is then this is probably the setup you have. This produces a shell prompt such as: spencer@plutonium ~/dotfiles $
This prompt works just fine. But its so boring! I kept this setup for probably the first 6 months I started using linux. But then I wanted to start to spice things up with my shell prompt. So I downloaded Ed's colors.sh file from his dotfiles. To utilize this all you have to do is include it in your bashrc(see my dotfiles for an example). I went through his colors and decided to make my prompt purple. So all I had to do was change my prompt to this: PS1='${Purple}\u@\h \w \$'
I kept this setup for about a month but then thought "why do I bother keeping my hostname? It just takes up space." So I changed it to PS1='${Purple}\u: \w \$'
I kept this configuration for quite some time but then I saw something cool that Ed did. He created a small script that checks to see if the command was succesful or not and if it was succesful it would place a green smiley, but if it wasn't it would give back a red frown. You can find that script: here. Then I just changed my PS1 to: PS1='${Purple}\u: \w [$(checkium_color)\]$(checkium_random_face) \$'
At this point I had a prompt like this(note:colors not included): spencer: ~/dotfiles :) $
I kept this configuration for probably about two months but then noticed that I had an issue. Some directories were very long and took up a lot of space so my prompt looked something like this: spencer: ~/Dropbox/codeshrub/posts/code :) $
This took up way too much space so I changed the w in my PS1 to W to only show the latest directory: PS1='${Purple}\u: \W [$(checkium_color)\]$(checkium_random_face) \$'
So now that long directory is just spencer: code :) $
Much easier to read, takes up less space, and it's not likely I'll forget my earlier directory path. I made one last change to my prompt. I removed the external script to check for a succesful command and place a smiley face and to replace it, I added this to my bash profile: cmd_check() {
if [[ $? = 0 ]]; then
echo " ${Green}✓";
else
echo " ${Red}❌";
fi;
And also changed my PS1 to: PS1='${BIGreen}\u: ${IWhite}\W $(cmd_check) \$${Color_Off} '
This changes my colors, removes the smileys and adds the x and check, and also makes sure colors are off at the end of the prompt. This is my final product(on the actual one there are the pretty colors :) ):
spencer: ~ ✓ $
I hope you enjoyed this post and I hope it helps you on your PS1 endevors.
posted in bash
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