Saturday, February 11, 2017

VBScript Lunch Program Using Shell Execute

VBScript is a scripting language to manage computer developed by Microsoft. Below is a code snippet to open an external program using Shell Application.

Dim objShell

Program_Name = "Notepad.exe"
Arguments = "" 'Optional
Directory = "" 'Optional
Operation = "open" 'open,edit,find,print,properties
Show_Option = 1 

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute Program_Name, Arguments, Directory, Operation, Show_Option

Set objShell = Nothing

'0 = Open the application with a hidden window.
'1 = Open the application with a normal window. If the window is minimized or maximized, the system restores it to its original size and position.
'2 = Open the application with a minimized window.
'3 = Open the application with a maximized window.
'4 = Open the application with its window at its most recent size and position. The active window remains active.
'5 = Open the application with its window at its current size and position.
'7 = Open the application with a minimized window. The active window remains active.
'10 = Open the application with its window in the default state specified by the application.

VBScript Launch programs whose path contains spaces

Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("""C:\Program Files\Mozilla Firefox\firefox.exe""")
Set objShell = Nothing

Pass old input/ get old input from view after Form Request validation in laravel

Sometimes we need to get old values in form to show them when error back to the same form, It is now well organized in Laravel. Consider the following situations:

1. Suppose you submitted a form with some values.
2. Found some errors and send back to the form again from Laravel controller to show the form again.
3. You have to show the values when he was here few moments ago (before submit the form)

$username = Request::old('username');

or in view:

{{ old('username') }}

Laravel join or left join queries AS different name

There are many situations where you need to join same table, but must have different names (you can define this as alias). Below is a simple code snippet to show how you can join same table with different name.

$limit = 10;
$page = 1;
$groups = DB::table('person')
       ->leftJoin('users as for_user', function ($join) {
           $join->on('for_user.id', '=', 'person.for_id')->where("for_user.id", "=", 10);
       })
       ->join('users as by_user', function ($join) {
           $join->on('by_user.id', '=', 'person.by_id');
       })
       ->where('person.status', '=', 1)
       ->select('person.id', 'person.first_name', 'by_user.first_name')
       ->limit($limit)->offset($page * $limit)
       ->get();

jQuery extend prototype

It is important that you can extent jQuery to improve your work environment. Below is a code snippet to do this as simple:

If you write your code in some-file.js then follow below:


(function () {
    $.prototype.hi = function () {
        this.html("Prototype invoked...")
        return this
    }
})()

Else if you write your code in <script> tag then follow below:


$.prototype.hi = function () {
    this.html("Prototype invoked...")
    return this
}

Example of usage:


$(".class").hi()

How to get the query executed in Laravel

As a developer you must face some situation where you have to need the sql executed. In Laravel you can see your sql query generated each step. Below is a simple code snippet to output the sql query generated by Laravel query generator. Also you have the parameters there used in Laravel query.

DB::enableQueryLog();
$total = DB::table('user')
       ->where('group_id', '=', 1)
       ->where('status', '=', 1)
       ->count();
$queries = DB::getQueryLog();
$last_query = end($queries);
echo "<pre>"; print_r($last_query); echo "</pre>";


Output following:




Array
(
    [query] => select count(*) as aggregate from `user` where `group_id` = ? and `status` = ?
    [bindings] => Array
        (
            [0] => 64
            [1] => 1
        )

    [time] => 0
)

How to get the current URL inside @if statement (blade) in Laravel

It is sometimes you need to control your view page(.blade.php) based on url visited by user. You can check if user visited by a Controller or Controller+Action both. It can be now done by Laravel very easily. Below is code snippet to do the tricks.

@if (Request::is('controller/*'))
    <!-- code block here -->
@endif

@if (Request::is('controller/action'))
    <!-- code block here -->
@endif