Friday, February 22, 2019

jquery ajax request with json response

$.ajax({
                        type:'POST',
                        url:'http://mysite.com/url',
                        data:{fieldname: 'fieldvalue'},
                        dataType: "json",
                        success: function(data , status, xhr) {
                              alert('ajax request successful');

                        }, fail:function(data){

                                alert('ajax request failed');
                        }
 });

Friday, February 15, 2019

QR-CODE BARCODE | Scanning field jquery useful script | focus in focus out

  //if scan field exists then focus the field after every second

 if($('.focus-me').length > 0){   

         setInterval(function(){
             $('.focus-me').focus();
         },1000);
       
        //if scan field is not focused then hide "scanner ready" message

         $(".focus-me").focusout(function(){
              $('.focus-in').hide();
              $('.focus-out').fadeIn(500);
         });
       
         //if scan field is focused then hide "scanner not ready" message
         $(".focus-me").focusin(function(){
              $('.focus-out').hide();
              $('.focus-in').fadeIn(500);
         });
       
         //reset the field
         $(document).on('keyup',function(e){
           
                  if (e.key === ' ' || e.key === 'Spacebar') {
                      e.preventDefault();
                      $('.focus-me').val('');
               
                  }
      
         });
       
     }

htaccess Force HTTPS redirect on Subdomain folder and remove www

 Follow is the code

RewriteEngine On
RewriteCond %{SERVER_PORT} 80

#force https
RewriteCond %{REQUEST_URI} subfolder
RewriteRule ^(.*)$ https://www.fdhlpk.com/subfolder/$1 [R,L]

#Remove www from the URL
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,QSA,NC,L]

Wednesday, January 30, 2019

yii2 search where not in condition usage

suppose we have two parameters to check

Like for example

where country_id = 4  AND city_id not in (2,3,4)

then using yii2 we can do the following

 Members::find()->select(['id','name'])->where(['country_id'=>4])->andWhere(['not in','id',[2,3,4]])->all();

Thursday, January 24, 2019

MySQL - replace first occurrance of string part - Example phone

Example for removing prefix + or 00 from phone number


we can replace first occurrence of string by using following methods using mysql functions REPLACE() , LEFT , INSTR(), LOCATE(), SUBSTRING()


To replace +  example (1 character)
SET @phone= '+923333333333';
SELECT  CONCAT(REPLACE(LEFT(@phone, INSTR(@phone, '+')), '+', ''), SUBSTRING(@phone, INSTR(@phone, '+') + 1)) as updated_phone;



Updating in DB:

Update [table] set phone = CONCAT(REPLACE(LEFT(@phone, INSTR(@phone, '+')), '+', ''), SUBSTRING(@phone, INSTR(@phone, '+') + 1))




To  replace 00 example (2 characters)
SET @phone= '00923333333333';
SELECT  CONCAT(REPLACE(LEFT(@phone, locate(@phone, '00')), '00', ''), SUBSTRING(@phone, instr(@phone, '00') + 2)) as updated_phone;



Updating in DB:
UPDATE [table] set phone = CONCAT(REPLACE(LEFT(@phone, locate(@phone, '00')), '00', ''), SUBSTRING(@phone, instr(@phone, '00') + 2))

Sunday, June 11, 2017

Interview Questions and Answers - Object Orient Programming in PHP (OOP) - Intermediate

Question. No. 1:
What are magic methods ?

Answer:
  • __construct : called while initializing an object with a "new" keyword
  • __destruct : when object is going out of scope.
  • __call : When method doesn't exists or access restricted in Object scope
  • __callStatic : When method doesn't exists or access restricted in Static Scope
  • __get : when trying to read a variable which doesn't exist or access restricted
  • __set: when trying to set a variable which doesn't exist or access restricted
  • __isset : when trying to check isset a variable which doesn't exist or access restricted
  • __unset : when trying to unset a variable which doesn't exist or access restricted
  • __sleep :  when serialize an object in order to store in database 
class myClass
{

       var $var1 = 3; 

private $var2 = 2;
    public function __sleep()
    {
        return array('var1', 'var2');
    }
}
$myObj= new myClass;

serialize($myObj);
  • __wakeup : when un-serialize an object get serialize object then un-serialize it 
class myClass
{

       var $var1 = 3; 

private $var2 = 2;
    public function __wakeup ()
    {
        return array('var1', 'var2');
    }
}
$myObj= new myClass;

$se = serialize($myObj);

unserialize($se);
  • __toString: when a class object was printed as string this method will be called
class myClass
{
    public function __toString()
    {
        return 'class name';
    }
}

$myObj = new myClass;
echo $myObj;
  • __invoke : when declared ,it will be used as a callable function with instantiated object name. it takes same number of arguments passed.
class myClass
{
    public function __invoke($x,$y)
    {
        echo $x.$y;
    }
}
$myObj = new myClass;
$myObj (4,5);

  • __set_state : when var_export() function called on the object
  • __clone : when called clone on an object. when we try to tract the serials of cloned.
  • __autoload : its not a method but Function, 






Wednesday, June 7, 2017

Interview Questions and Answers - Object Orient Programming in PHP (OOP) - Basics

Question.1. 
what is class?

Answer: 

  • Its a blue print (Technical Design/structure) of how methods & behaviors will be written/shown. 

Question.2:
What is an object?

Answer:

  • Its an instance of a class.


Question.3.
What is the Interface?
Answer:

  • functionality cannot be provided
  • properties cannot be defined
  • it is to enforce child class to implement all the methods existing in interface is a MUST. 
  • it cannot be instantiated. 
  • methods can only be public inside interface.
  • child class can only implement not extend with interface
  • every method should be abstracted 
  • multiple inheritance is possible with implements keyword
  • multi-level inheritance is possible , that is an interface can extend parent interface and a class can implement child interface and it should implement all the methods from parents interfaces.

Question 4:
What is an Abstract class?

Answer:

  • functionality can be provided inside it
  • properties can be defined inside it
  • Methods cannot be private, 
  • child class must implement abstract methods
  • child class can extend an abstract class with "extends" keyword instead of "implements" 
  • not every method should be abstract
  • multiple inheritance is not possible

Question 5:
What are properties?

Answer:

  • information / data/state of an object
Question 6:
What is are Methods?

Answer:

  • Functions or behaviors of the class
Question 7:
What is Encapsulation?

Answer:

  • to enclose the properties and behaviors in curly braces so that we don't need to know the information processing inside a class while working with an API calls to this class.
Question 8:
What are Visibility/Access Modifiers?
Answer:

  • Public - accessible from everywhere
  • Protected - accessible from within class as well child classes
  • private  - accessible from within class ONLY 
Question 9:
What is Static?

Answer:
  • Method or property available at class level , no object needed
  • changes will affect everywhere in all the instances of the class
Question 10:
What is Constructor?

Answer:
  • Its a magic method
  • used to initialize the properties of an object 
Question: 11:
What is Polymorphism:

Answer:
Its a concept that an object can have different forms, means a class can have many child classes which are related to parent class but having different behaviors.

example:

interface Speaking{
     public function speak();
}

cat implements speaking{
     
     public function speak(){
           echo 'meeaoo';
     }
}

dog implements speaking{
     
     public function speak(){
           echo 'woooah';
     }
}

person implements speaking{
     
     public function speak(){
           echo 'lets go';
     }
}