Linux Grub Change Boot Order

> sudo vi /etc/default/grub

- Change GRUB_DEFAULT=4
(4 is usually Windows boot)

> sudo update-grub

PHP Show all Error

Add these lines to the beginning of the php script:
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );

PHP Send Email via Google SMTP Server

To use the following script, you need:
1. Install PEAR Mail (See: Install PHP PEAR)
2. Add php_openssl.dll extension in php.ini
3. A google account

Function:
require_once( 'Mail.php' );

// Before use:
// 1. Add a php extension in php.ini: php_openssl.dll
// 2. Set $smtpUsername and $smtpPassword (Use a google account)
function sendmail( $from, $to, $subject, $body ) {
    $smtpHost = 'ssl://smtp.gmail.com';
    $smtpPort = '465';
    $smtpUsername = 'example@gmail.com';
    $smtpPassword = 'password';
    $headers = array(
        'From' => $from,
        'To' => $to,
        'Subject' => $subject
    );
    $smtp = @Mail::factory('smtp', array(
            'host' => $smtpHost,
            'port' => $smtpPort,
            'auth' => true,
            'username' => $smtpUsername,
            'password' => $smtpPassword
        ));
    $mail = @$smtp->send($to, $headers, $body);

    if ( @PEAR::isError($mail) ) {
        return false;
    } else {
        return true;
    }
}

Usage:
$from = 'from@gmail.com';
$to = 'to@gmail.com';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
var_dump( sendmail( $from, $to, $subject, $body ) );

Install PHP PEAR

1. Download go-pear.phar

2. Go to the directory you want to install pear (It should NOT be inside www directory, I will install in \path\to\php\pear)

3. Copy and run go-pear.phar at the pear path:
> php go-pear.phar

4. Press Enter (to select system)

5. Update: 11. Name of configuration file (Where to create the pear.ini)

6. Update: 12. Path to CLI php.exe (Tell the pear where is php.exe)

7. Press Enter to start installation

8. Edit php.ini, add/edit the include_path:
include_path=".;\to\pear\path"
9. Restart PHP service

10. Done.

Bonus:
To install a package:
- Go to pear path, run:
> pear install package_name

jQuery Validation Plugin Template

jQuery Validation Plugin makes html form validation easier. You need to setup jQuery and jQuery Validation Plugin to make it works.

Header:
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>

Script:
<script>
$(function() {
    $( "#formId" ).validate({
        errorClass: 'alertText smallText',
        rules: {
            email: {
                required: true,
                email: true
            },
            username: {
                required: true
            },
            password: {
                required: true
            }
        },
        messages: {
            email: {
                required: 'Please input email',
                email: 'The email format is invalid'
            },
            username: 'Please input username',
            password: 'Please input password'
        }
    });
});
</script>

Form:
<form id="fromId" action="" method="post">
    <fieldset>
        <p>
            <input type="text" name="email" placeholder="email" value="" />
        </p>
        <p>
            <input type="text" name="username" placeholder="username" value="" />
        </p>
        <p>
            <input type="text" name="password" placeholder="password" value="" />
        </p>
        <p>
            <input type="submit" name="submit" value="Go" />
        </p>
    </fieldset>
</form>

PHP Validate Email

Note: PHP version >= 5.2.0
if ( filter_var( $_POST[ 'email' ] , FILTER_VALIDATE_EMAIL ) ) {
    echo 'Email ok';
}

Shell Script Send a Mail

You need to install mail for this script:
#!/bin/bash
echo "Mail content" | mail -s "subject" -a "attachment.zip" toMail@example.com

Install mail
> sudo yum install mail

Mysql Commands

> mysql -u root -p

mysql> show databases;

mysql> create database new_database;

mysql> select user, host from mysql.user;

mysql> create user 'newuser'@'localhost' identified by 'password';

mysql> drop user 'newuser'@'localhost';

mysql> grant all privileges on new_database.* to 'newuser'@'localhost';

mysql> revoke all privileges, grant option from 'newuser'@'localhost';

mysql> show grants for 'newuser'@'localhost';

If privilege change is not in effect, you need to reload the privileges:
mysql> flush privileges;

Git Commands

Help command:
> git help


Initial empty Git repository (Create .git directory)
> git init

Set user name and email
> git config user.name "peter"
> git config user.email skybonep@gmail.com

List config variable
> git config -l


Add one file to repository (File must exist), it only let the repository knows the file, it has not committed yet. Every time you change the file content, you need to add again
> git add hello.txt

Add all files and subdirectory to Git repository
> git add .

Remove a file from repository (The file will be removed from the working directory), but it has not committed.
> git rm hello.txt

Rename a file, but it has not committed.
> git mv hello_old.txt hello_new.txt

Show the working status
> git status


Create branch
> git branch branchName

Switch between branches
> git checkout branchName


Commit to update the changes to the repository
> git commit -m "Initial content" --author="peter "
> git commit hello.txt
(You have to input a message about this commit action in the default text editor)
> git commit -a
Note: The above command is doing 2 things:
> git add .
> git commit



Show commit log
> git log

Show commit log from remote repository
> git log origin/master

Show commit details
(show last commit action)
> git show
([hash code] = hash code from git log command)
> git show [hash code]


Copy a new repository (the dir_old directory is the one containing .git, i.e. structure: dir_old/.git/)
> git clone dir_old dir_new

Add a new remote repository reference:
> git remote add origin "file:///c:\remote.git"

Show remote repository location:
> git remote -v

Send update to remote repository:
> git push origin

Get update from remote repostiory and merge:
> git pull origin master

See also:
- Install Git in Windows
- Set up Git Remote Repository with Dropbox

Git with Eclipse

Submit (Push) to remote repository:

1. Work work work... coding in the working directory...
2. Finished something and ready to submit works to the remote repository
3. Select the Project -> right click -> Team -> Commit
4. Input the Commit message
5. Click "Commit and Push"
Note: If you click "Commit", it will only update the local repository
6. If the file(s) is modified by others in remote repository (i.e. conflicts), the Push will be rejected.
7. In this case, you need to Pull the files from the remote repository, solve the conflict locally, and then Push the new version to the remote. (See below)

Get from remote repository:

1. Select the Project -> right click -> Team -> Remote -> Fetch From...
2. Click "Finish"
Note: This only update the local Git repository, NOT the working directory
3. Select the Project -> right click -> Team -> Pull
4. Done. The working directory is also updated!

If there is conflict between local and remote repository:

1. Select the Project -> right click -> Team -> Remote -> Fetch From... (Fetch the files and you will find there is a conflict)
2. Select the Project -> right click -> Team -> Merge... (It will mark and show you the conflict files)
3. You will find a red icon on the conflict files
4. Open the conflict file to solve the problem manually, or
5. You may use the merge tool to solve the problem: Right click the file -> Team -> Merge Tool
6. After the conflict solved. File -> right click -> Team -> Add to Index
7. Select the Project -> right click -> Team -> Commit
8. Input the Commit message
9. Click "Commit and Push"
10. Done! The remote repository is updated

Set up Git Remote Repository with Dropbox

Assume: Working in Windows platform
Assume: You have installed Git Gui

1. Open Git Bash

2. Create a repository remotely:
In the Git Bash box:
> cd "yourDropboxDir\YourProject\Git"
> git init --bare yourProject.git
(it creates directory yourProject.git)

3. In the Git Bash box, go to the project directory (e.g. c:\project)
> cd "c:\project"

4. Create a repository locally:
> git init
> git add .
> git commit -m "New Git repository"

5. Push from local to remote:
> git remote add origin "yourDropboxDir\YourProject\Git\yourProject.git"
Note: yourProject.git at the end
> git push origin master

6. Done. Remote Git Repository is ready to use! (Also, you can start your development in "c:\project" directory)

Bouns: How to start a new Git development environment from a remote repository?
7. Pull from remote repository to a new working directory:
- Create a new folder for the project (e.g. c:\working)
- Start Git Bash (by right click in the explorer)
> cd "c:\working"
> git clone "yourDropboxDir\YourProject\Git\yourProject.git"

Import an Eclipse project from Git

Assume: The remote Git repository is in the file system. (I put it in Dropbox)
Reference: Set up remote Git repository with Dropbox (working)
Reference: Install Eclipse in Windows

1. Start Eclipse
2. Help -> Eclipse Marketplace
3. Install: EGit - Git Team Provider (EGit may be already installed in your eclipse by default)
4. File -> Import...
5. Select "Git -> Projects from Git", and Next
6. Select "Clone URI", and Next
7. Click "Local File..."
8. Select the remote repository directory, and Next
9. Select "master" (or the branch you need), and Next
10. Input the Destination -> Directory (your working directory), and Next
11. Select "Import existing projects", and Next
12. Click "Finish"
13. Done!

Install Eclipse in Windows

Assume: You use eclipse to develop in Java. Choose a suitable "Package Solutions" in Step 2 otherwise.

0. You need to install Java SE Development Kit first
1. Goto http://www.eclipse.org/downloads/
2. Click from Package Solutions, "Eclipse IDE for Java Developers"
3. Download the .zip file and unzip
4. Run eclipse.exe in the eclipse directory to start!
5. Done!

Install Java JDK in Windows

Note: the version number 7u45 is subjected to be changed.

1. Goto http://www.oracle.com/technetwork/java/javase/downloads/index.html
2. Click "download Java Platform (JDK) 7u45"
3. Select "Accept License Agreement"
4. Click "Java SE Development Kit 7u45" -> Windows x86 -> jdk-7u45-windows-i586.exe (or Windows x64 if you are 64-bits system)
5. After download, run the .exe
6. Goto Control Panel -> System -> Advance -> Environment Variable
7. Select System Variable -> Path
8. Click "Edit"
9. Add a semicolon(;) and the JDK bin path at the end of the Path Value (e.g. ...PATH...;C:\Program Files\Java\jdk1.7.0_45\bin)
10. Click "OK"
11. Open a NEW Command Prompt window, type "javac" to test if everything is ok or not
12. Done!

Install Git in Windows

Git is a version control system.

1. Goto http://git-scm.com/download/win
2. Download and run the .exe file
3. Done!
4. You can now open the explorer and right click to get the Git Bash and other features

You can also set the global user name and email:
> git config --global user.name "peter"
> git config --global user.email skybonep@gmail.com

See Also:
- Git command line User Guide

Java HashMap

import java.util.HashMap;

public class prnote {
    public static void main( String[] args ) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put( "1", "one" );
        map.put( "2", "two" );
        System.out.println( map.get( "1" ) );
    }
}

Java LinkedList

import java.util.*;

public class prnote {
    public static void main( String[] args ) {
        LinkedList<String> list = new LinkedList<String>();
        list.add( "two" );
        list.addFirst( "one" );
        Iterator<String> iter = list.iterator();
        while ( iter.hasNext() ) {
            System.out.println( iter.next() );
        }
    }
}

Java ArrayList vs Vector

In short, normally, use ArrayList. The difference between two:
  • ArrayList is faster
  • Vector is synchronized, any method that touches the Vector's content is thread safe. But ArrayList is not
  • Both ArrayList and Vector internally store data using Array. When it need to grow (by adding element), ArrayList increases 50% of the original size, Vector doubles the size of the array, and Vector can set the increment value.

Reference:
Vector or ArrayList -- which is better?

ArrayList Usage:
import java.util.ArrayList;

public class prnote {
    public static void main( String[] args ) {
        ArrayList<String> arr = new ArrayList<String>();
        arr.add( "one" );
        arr.add( "two" );
        System.out.println( arr.get( 0 ) );
    }
}

Vector Usage:
import java.util.Vector;

public class prnote {
    public static void main( String[] args ) {
        Vector<String> v = new Vector<String>();
        v.add( "one" );
        v.add( "two" );
        System.out.println( v.get( 0 ) );
    }
}

Apache Virtual Host Setup

Apache Virtual Host makes one web server hosts more than one web service virtually.

Add this line at the end of the httpd.conf: (in /etc/httpd/conf/)
NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot /var/www/html/example1
    ServerName example1.com
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/html/example2
    ServerName example2.com
</VirtualHost>
Don't forget to restart the apache server.
sudo service httpd restart

HTML Drop-down Menu

<style>
.menu {
    position: relative;
    top: 0;
    left: 0;
    text-align: center;
}

.menu a {
    background-color: #cccccc;
    display: block;
    float: left;
    width: 120px;
    line-height: 2em;
    text-decoration: none;
}

.menu a:hover {
    background-color: #bbbbbb;
}

#menu1 {
    position: absolute;
    left: 0;
    top: 0;
}

#menu2 {
    position: absolute;
    left: 121px;
    top: 0;
}

.submenu {
    visibility: hidden;
}

.submenu a {
    background-color: #aaaaaa;
    border-top: solid 1px #ffffff;
    display: block;
    float: none;
}
</style>

<script type="text/javascript">
<!--
function MenuOn( x ) {
    document.getElementById( "submenu" + x ).style.visibility = "visible";
}

function MenuOff( x ) {
    document.getElementById( "submenu" + x ).style.visibility = "hidden";
}
-->
</script>

<div class="menu">

<div id="menu1" onMouseOver="MenuOn(1)" onMouseOut="MenuOff(1)" >
    <a href="" class="menuhead">Menu 1</a>
    <div class="submenu" id="submenu1">
        <a href="">Topic 1-1</a>
        <a href="">Topic 1-2</a>
        <a href="">Topic 1-3</a>
    </div>
</div>

<div id="menu2" onMouseOver="MenuOn(2)" onMouseOut="MenuOff(2)" >
    <a href="" class="menuhead">Menu 2</a>
    <div class="submenu" id="submenu2">
        <a href="">Topic 2-1</a>
        <a href="">Topic 2-2</a>
        <a href="">Topic 2-3</a>
        <a href="">Topic 2-4</a>
    </div>
</div>

HTML Hello World

hello.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="description" content="Hello World Template">
<meta name="keywords" content="html, hello world, template">
<title>HTML Hello World</title>
<link rel="stylesheet" type="text/css" href="common.css">
</head>
<body>

<div class="content">
<h1>Hello Title</h1>

<div class="section">
<h2>H2 title</h2>
<p>
Hello world content... <em>Emphasized Text</em> <strong>Strong Test</strong> Longer content, Longer content, Longer content, Longer content, Longer content, Longer content, Longer content, Longer content, Longer content, Longer content, Longer content, Longer content, Longer content, Longer content, Longer content, Longer content, Longer content.
</p>
</div>

<div class="section">
<h2>H2 title more</h2>
<p>
1em = 1 standard font size = 16px = h4
</p>
<p>
padding: top right bottom left;
</p>
</div>

<address>
Footer. Copyright (C)
</address>
</div> <!-- class="content" -->

</body>
</html>

common.css:
body {
    text-align: center;
}

div.content {
    border: solid 1px #5e8eab;
    width: 650px;
    margin-left: auto;
    margin-right: auto;
}

h1 {
    text-align: center;
    background-color: #c5e1ed;
    padding-top: 0.5em;
    padding-bottom: 0.5em;
    color: #2d444f;
    margin-top: 0;
}

h2{
    border-left: solid 1.2em #5e8eab;
    padding-left: 0.3em;
    margin-bottom: 1em;
}

p {
    line-height: 1.5em;
    color: #555555;
    margin-left: 1em;
}

.section {
    width: 40em;
    padding-top: 0.2em;
    padding-left: 1em;
    text-align: left;
}

address {
    font-size: 0.75em;
    font-weight: bold;
    font-style: normal;
    color: #5e8eab;
    border-top: solid 9px #c5e1ed;
    padding: 6px 10px 10px 10px; 
    margin-top: 30px;
    text-align: right;
}