How mysqli different from mysql

Mysql

Posted on 08 Jan 16 by Vickey
  1284

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
  1385

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
  1241

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
  1470

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
  1384

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

 

How to use constant in javascript

Js

Posted on 28 Dec 15 by Vickey
  1369

In javascript we can define constant as below

var constants = {

   'siteUrl': 'test.com',

}; 

you can write all constant in a single file and include this file in your all pages and where we need this constant we can get it by using 

constants.siteUrl;

 

How to debug mysql queries in cakephp

Cakephp

Posted on 23 Dec 15 by Vickey
  796

In cakephp we use find and save function for read and write data in database but we do not see the row queries behind these function.If you want to see these query you need to do some cahanges in your cakephp files

1.Open app/config/core.php file and change debug mode 0 to 2

Configure::write('debug', 0);

to

Configure::write('debug', 2);

2. In your layout add

echo $this->element('sql_dump');

by doing these two steps you can see and debug your cakephp queries

How to get started with php

php

Posted on 22 Dec 15 by Vickey
  1223

For get started php you should have basic knowledge of javascript,css and Html.For run php program apache server is needed, you can use wamp xampp or lamp server for php development according to your operation system. If you have window then you will use wamp (window apache mysql php) or if you are working on linux you use lamp (linux apache mysql php) and xampp server works on both operation system.

So lets start with php, once you have install wamp server you need to start this server.By clicking on wamp icon you can start wamp server.If your server has started type localhost in your browser's address bar and hit enter you will see wamp server index page you can use your custom index page here.Without wamp server you can not run your php file so if you want run your php file you need to put your files on wamp server, Lets do this 

Open your c drive >> wamp >> www directory. The www directory is wamp server public directory here php file runs or executes and you see result on browser by url localhost/your filename.php. Thanks I hope this will help you

Add credit card by using braintree.js

Js

Posted on 18 Dec 15 by Vickey
  711

For create credit card in braintree fir we need to verify credit card details.If card verified successfull we get nonce from braintree for this we can use braintree.js

https://js.braintreegateway.com/v2/braintree.js
var client = new braintree.api.Client({clientToken: "your client tocken"});
      var credit_card_number = 411111111111111
      var credit_card_exp_month = 01
      var credit_card_exp_year = 2020
      var credit_card_card_name = 'Jhon'
      var zip = 12345
      client.tokenizeCard({

        number: credit_card_number ,
        cardholderName: credit_card_card_name ,
        expirationMonth: credit_card_exp_month ,
        expirationYear: credit_card_exp_year ,
        
        billingAddress: {

          postalCode: zip

        }

      }, function (err, nonce) {

            //here we get nonce 
      });

This nonce we can pass to braintree php api with customer id and it will add new credit crad with customer

      try {
      $result = Braintree_PaymentMethod::create([
        'customerId' => customer_id,
        'paymentMethodNonce' => $nonce,
        'options' => [
          'verifyCard' => true          
        ]
      ]);
      } 
      catch (Exception $e) {
      return $this->_returnJson(false, $e->getMessage());
      }

 

How to make drop down option selected in javascript

Js

Posted on 18 Dec 15 by Vickey
  965

With the help of below javascript code we can select a dropdown option dynamically.I have a dropdown box with id exp_year

<select id="exp_year">

<option value="2001"></option>

<option value="2002"></option>

<option value="2003"></option>

<option value="2004"></option>

<option value="2005"></option>

</scelect>
var exp_year = document.getElementById("exp_year");

for (var i = 0; i < exp_year.options.length; i++) {

	if (exp_year.options[i].value== 2004) {
	
		exp_year.options[i].selected = true;
		
		break;
	 
	}
}

 

www.000webhost.com