How to install and upload source code on Github

Others

Posted on 12 Feb 16 by Vickey
  42

GitHub is the best place where we can share our code with friends and others programmers. It provide us best code management functionality. On GitHub  every developer can share code. Here you can show your skills. On GitHub by using we can control over code version by committing code on Git. GitHub is a web based Git repository hosting service. GitHub is a largest code hosting service. In this tutorial we will install Git and commit code on GitHub step by step. Here I am using Ubuntu operating system.

1. First of all open your terminal and locate in a directory where you want to crate local repository

vickey@vickey-System:~$ cd /home/Desktop

vickey@vickey-System:~/Desktop$

2. Now use the following command to create local repository

vickey@vickey-System:~/Desktop$ git init PHP

when repository created you will see the below line in your terminal

Initialized empty Git repository in /home/vickey/Desktop/PHP/.git/

It will create a new directory with name "PHP"

3. Now change your directory

vickey@vickey-System:~/Desktop$ cd PHP
vickey@vickey-System:~/Desktop/PHP$

4. Here with the below command  create a README file where you will describe about the repository

vickey@vickey-System:~/Desktop/PHP$ gedit README

I used gedit editor you can use other editor. Write your content in the file and save the file

5. This is the important part of this tutorial here we will add our code and files which we will push on GitHub. For now here I am creating a file name as info.php with the below code.

phpinfo();

Now we have two files in "PHP" directory 'README' and 'info.php'

6. Now index these files by using the below commands

vickey@vickey-System:~/Desktop/PHP$ git add README
vickey@vickey-System:~/Desktop/PHP$ git add info.php

This git add command will be used for every file which you have in your directory

7. After files added we will commit these on server

vickey@vickey-System:~/Desktop/PHP$ git commit -m "your message here"

8. Now login your GitHub account and create repository with the name same as you have on your local system. Once you have done run the below command in your terminal. Note that 'vickey' is the GitHub account username please change it first.

vickey@vickey-System:~/Desktop/PHP$ git remote add origin https://github.com/vickey/PHP.git

9. And in last push your code on server

vickey@vickey-System:~/Desktop/PHP$ git push origin master

Username for 'https://github.com': vickey
Password for 'https://vickey@github.com': 
To https://github.com/vickey/PHP.git
 * [new branch]      master -> master

 

How to pass values from Controller to javascript file in Cakephp

Cakephp

Posted on 05 Feb 16 by Vickey
  101

These days javascript is the essential tool for an internet developer. Every and each internet application uses javascript. Sometimes we need dynamic values from database in js file. For example alert the logedin usename. In simple way we can do this by using the below code

var jsvariable='<?php echo $phpvariable; ?>';

but if you are using cakephp then there is a best way to do this task. In appController.php write the below code

public $helpers = array('Js' => 'Jquery');
public $javascriptVar=array();

public function setJavascriptVariable ($variableName, $variableValue)
{
	if (key_exists($variableName, $this->javascriptVar))
	{
	throw new Exception('a variable already has that name');
	}
	$this->javascriptVar [$variableName] = $variableValue;
}

public function beforeRender()
{
    $this->set('javascriptVariable', $this->javascriptVar);
}

And now by calling the function 'setJavascriptVariable' set javascript values in your controller.

public function welcomePage() {

	$this->setJavascriptVariable('myvar', 'Hi');

}

Now open your layout.ctp file and write the below code

echo $this->Html->scriptBlock('var javascriptVariable = '.$this->Js->object($javascriptVariable).';');

You have done now you can access this variable in every js file

alert(javascriptVariable.myvar);

 

How to change upload max file size value by using htaccess

php

Posted on 03 Feb 16 by Vickey
  45

If you don't have access the php.ini file you can change 'upload_max_filesize','memory_limit','post_max_size' by doing changes in htaccess file if you have the permission to set these values in htaccess file.

php_value memory_limit 80M
php_value post_max_size 50M
php_value upload_max_filesize 30M
php_value upload_max_filesize 10M

For set these value create a .htaccess file on your website root directory ( if not already exist ) and put the code in this file

If you do not have permission to set these values in htaccess then you will get 500 - Internal server error. For solve this you can contact web server support team.

How to get current Controller and current Action name in Laravel

Laravel

Posted on 26 Jan 16 by Vickey
  110

Some times in our projects we need to get current controller or current action name in our code or controller. We can do this in Laravel also by the help of Routing. First of all you need to add the below code at the top in your controller file

use Illuminate\Routing\Route;

Now add this code in your controller class

public $cointrollerName='';
public $actionName='';
public function __construct(Route $route)
{
	$currentAction= $route­>getActionName();
	list($controller, $method) = explode('@', $currentAction);
	$this­>cointrollerName= preg_replace('/.*\\\/', '', $controller);
	$this­>actionName=preg_replace('/.*\\\/', '', $method);
}

and now you can get your controller or action name by using code

	echo $this­>cointrollerName;

	echo $this­>actionName

The complete code will look like this

use Illuminate\Routing\Route;
class ApiController extends Controller
{
	public $cointrollerName='';
	public $actionName='';

	public function __construct(Route $route)
	{
		$currentAction= $route­>getActionName();
		list($controller, $method) = explode('@', $currentAction);
		$this­>cointrollerName= preg_replace('/.*\\\/', '', $controller);
		$this­>actionName=preg_replace('/.*\\\/', '', $method);
	}

	public function index(){

		echo $this­>cointrollerName;
		echo $this­>actionName 
	}

}

 

Eloquent ORM queries in Laravel

Laravel

Posted on 21 Jan 16 by Vickey
  93

The Eloquent ORM is simple ActiveRecord implementation for working with database. For interact with 'database tables' we use a corresponding "Model"

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
   
}

Here i did not tell which table will be used by this model because as specified in Laravel Eloquent the table name lower-case, plural and model name will be singular for example if model name is User the table name will be users

Retrieve records from users table

For retrieve all record from table users we will use User model because the corresponding table of User is users

$usersRecord = User::all();

Retrieve record by using primary key

$id=1;
$user = User::find($id);

Use where conditions

$usersRecords = User::where('age', '>', 30)->take(20)->get();

Count records

$noOfRows = User::where('age', '>', 30)->count();

Save record in databse

By using 'Eloquent ORM' we can insert and update record in database

Insert new record in database

$user = new User;

$user->name = 'abc';

$user->save();

Update record in database

$user = User::find(1);

$user->email = 'abc@gmail.com';

$user->save();

Delete records

Delete records by using primary keys

User::destroy(1);

User::destroy(array(1, 2, 3));

User::destroy(1, 2, 3);

If we want to delete more than one record we can use primary keys comma seperated or in array

Delete record by using where conditions

$affectedIds = User::where('age', '>', 30)->delete();

These are some basic 'Eloquent ORM' queries in laravel. If this content help you please leave a comments

Thanks 

How mysqli different from mysql

Mysql

Posted on 08 Jan 16 by Vickey
  157

Mysqli extension is an improved version of Mysql. The 'i' in mysqli is for “improved”.In mysqli  many new features added. Here are some benefits of mysqli

1. Object-oriented interface

You can improve or change base mysqli class

class customSql extends mysqli {
	mysqli_report(MYSQLI_REPORT_OFF);
} 

2.Prepared Statements

It prevent SQL injections and execuation is faster then mysql

3. Multiple Statements

You can execuate multiple query in one mysqli call. This reduce the no of calls between database and the PHP server.

$connection = new mysqli($host, $user, $password, $database);
$query  = "INSERT INTO users (name,email ) VALUES('user name','test@gmail.com');";
$query  .= "INSERT INTO employee (name,email ) VALUES('employee name','employee@gmail.com');";
$result = mysqli_multi_query($connection, $query);

4.Support for Transactions

Mysqli support transactions queries.  If there is an error in process in mysqli queries it will  rolled back all queries.

5.Enhanced debugging capabilities

By using "mysqli_debug()" we can write debugging information in a file

How to define validation on fly in cakephp

Cakephp

Posted on 06 Jan 16 by Vickey
  176

As we know that in cakephp we define our all form validation in our Model.

class User extends AppModel {
    public $validate = array(
		
		'cellnumber' => array(

			'isUnique' => array(
				'rule' => 'isUnique',
				'message' => 'cellnumber Already exist.'
			),
			'required' => array(
				'rule' => 'notEmpty',
				'message' => 'cellnumber is required.'
		)),

		'email' => array(

			'isUnique' => array(
				'rule' => 'isUnique',
				'message' => 'Email Already exist.'
			),
			'required' => array(
				'rule' => 'notEmpty',
				'message' => 'Email is required.'
		)),
		
        'name' => array(
            'required' => array(
				'rule' => 'notEmpty',
				'message' => 'Name is required'
             )
        ),
	);
}

But in some cases we do not need these validation on all form. For example if we have two different registration form for different types of users and for first type of user cellnumber is not required and for second type of user cellnumber is required then if I have define validation for cellnumber in Model then it will be mandatory for both type of users.So we can define this validation on fly also, where it need

public function saveUserTypeFirst(){

	if ($this->request->is('post')){
		$this->User->save();
	}
	
}
public function saveUserTypeSecond(){

	if ($this->request->is('post')){

		$this->User->validator()->add('cellnumber', 'required', array(
	        'rule' => array('notEmpty'),
	        'message' => 'cellnumber is required'
	    ))->add('cellnumber', 'isUnique', array(
	        'rule' => array('notEmpty'),
	        'message' => 'This cellnumber is already registered'));
	    $this->User->save();
    }
}

 

How to create jquery custom popup window

Jquery

Posted on 30 Dec 15 by Vickey
  144

For show popup window there are many plugins and library like 'thickbox' , 'simple model' etc. But here I am creating a custom popup window which can easily update and modify.In this popup window I am using jquery. Lets first write html part of the popup

<div style="display: none;" id="overlay" class="pop_window_overlay"></div>
<div style="display: none;" id="dialog" class="pop_window">
 	<table cellpadding="3" cellspacing="0" style="width:100%;">
		<tbody>
			<tr>
                <td class="pop_window_title">Welcome!</td>
                <td class="pop_window_title align_right">
                    <a href="#" id="close_button">Close</a>
                </td>
            </tr>
        </tbody>
    </table>
</div>

This is the html part which will be show as a popup window. For use jquery events we need to load jquery so first load jquery library and then use the below javascript code

$(document).ready(function ()
{
	show_popup();
	$("#close_button").click(function (e)
	{
		hide_popup();
	});
});

function hide_popup()
{
    $("#overlay").hide();
    $("#dialog").fadeOut(300);
}

function show_popup()
{
    $("#overlay").show();
    $("#dialog").fadeIn(300);
    $("#overlay").click(function (e)
    {
        hide_popup();
    });
}

Here i am loading popup window on window load you can load it on a button click.

And now we need to add css 

.pop_window_overlay
{
   
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    height: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
    opacity: .15;
    filter: alpha(opacity=15);
    -moz-opacity: .15;
    z-index: 101;
    display: none;
}
.pop_window
{
    display: none;
    position: fixed;
    width: 380px;
    height: 144px;
    top: 50%;
    left: 50%;
    margin-left: -190px;
    margin-top: -100px;
    background-color: #ffffff;
    border: 2px solid #ccc;
    padding: 0px;
    z-index: 102;
    font-family: Verdana;
    font-size: 10pt;
}
.pop_window_title
{
    border-bottom: solid 2px #ccc;
    background-color: #ccc;
    padding: 4px;
    color: #333333;
    font-weight:bold;
}
.pop_window_title a
{
    color: #333333;
    text-decoration: none;
}
.align_right
{
    text-align: right;
}

congratulation you have created your custom popup window now you can change color or style according to your theme

How to use email template in cakephp

Cakephp

Posted on 30 Dec 15 by Vickey
  601

By using email template we can change our email content very easily.Because we have all templete in a differente directory and if we need to change any email content we edit only the templete file and there is no need to search content in our controllers.So lets start work with email templates step by step

Step1: Create a directory 'emails' under '/app/webroot/' and here create your first templete  'welcome.html'

Step2: Add your email content in this html file 

<html>
<head><title>Welcome to {SITE_TITLE}!</title></head>
<body>
<div style="max-width: 800px; margin: 0; padding: 30px 0;">
<table width="80%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="5%"></td>
<td align="left" width="95%" style="font: 13px/18px Arial, Helvetica, sans-serif;">
<h2 style="font: normal 20px/23px Arial, Helvetica, sans-serif; margin: 0; padding: 0 0 18px; color: black;">
Hi {USER}, 
<br>Welcome to {SITE_NAME}!</h2>
Thanks for joining {SITE_NAME}.<br />
Best wishes<br />
{SITE_NAME}
</td>
</tr>
</table>
</div>
</body>
</html>

You have created your first email template now we will use it in controllers

$templetedata = file_get_contents("Your website base url" . "emails/welcome.html");

here we get all content from templete now we will replace our dynamic data for example name or site url etc

$dynamicValues = array(
	"{USER}" => 'Username',
	"{SITE_NAME}" => 'example.com'
);

$emailmsg = @str_replace(array_keys($dynamicValues), array_values($dynamicValues), $templetedata);

and now send this email message

$this->sendMail($emailmsg);

We have done with email templates if you have any question comments please mail me

Difference between require and include in php

php

Posted on 28 Dec 15 by Vickey
  728

When we need to use content of a page in another page we use require or include statement.The require and include function do the same thing load one file code in another but there is a major difference between require and include.As the meaning of word require we can understand. We use require word where we need a thing on urgent bases means we can not complete our work without this.This is the same in php also if we load a file content with require statement and this file not found on the given path it will give fatal error and script will stop. But if we are using include it will give a warning and script will be continue

require('path of file'); //fatal error




include('path of file'); //   WARNING