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';
     }
}

Thursday, December 10, 2015

MEAN Stack Tutorials and courses - MongDB, ExpressJS, AngularJS, NodeJS

MEAN Stack RESTful API Tutorial (1/5) - Using MongoDB, Express, AngularJS, and NodeJS Together

Node awesome modules to be used Take all updated module from this link https://www.airpair.com/node.js/posts/top-10-mistakes-node-developers-make
MEAN Stack Intro: Build an end-to-end application - must watch again and again https://www.youtube.com/watch?v=AEE7DY2AYvI
RestFull API Basics https://www.youtube.com/watch?v=kIzdaR_cKWY REST call

https://www.youtube.com/watch?v=DeFJ9oQm-b0

Building your first MEAN application - good intro to Mongo DB https://www.youtube.com/watch?v=PH_5lXxSpww
awesome blog example using mongodb node.js https://github.com/madhums/node-express-mongoose-demo
Good express module for Node js application for nitification https://github.com/madhums/node-notifier

MEAN

ANGULR

d3 with angular.js http://jsfiddle.net/en6k0zsc/1/

NODE

WATCH all this video TO LEARN NODE.JS http://technotip.com/3674/node-js-video-tutorial-list/
TJ Holowaychuck's modular web applications with node js and express 1152x720 https://www.youtube.com/watch?v=9CTfGS0gEOk

Morris chart in angular.jss http://angular-js.in/angular-morris-chart/
kyle simpson about java script https://www.youtube.com/watch?v=__8eIX0QFXU

EXPRESS

Node Guides - Understanding Event Emitter https://vimeo.com/46976277

JAVA SCRIPT



SOURCE: https://www.reddit.com/r/meanstack/comments/2v68zd/mean_js/

Saturday, October 31, 2015

document ready in javascript - alternative to $(document).ready

There are many Answers available for the above question on the following URL.

http://stackoverflow.com/questions/2304941/what-is-the-non-jquery-equivalent-of-document-ready

Friday, October 9, 2015

PNG to JPG - Conversion and Compression - PHP






Code is below:

<?php

if(isset($_POST['submit'])){

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["myfile"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

move_uploaded_file($_FILES["myfile"]["tmp_name"], $target_file);

$filePath = './uploads/'.basename( $_FILES["myfile"]["name"]);
$image = imagecreatefrompng($filePath);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);
$quality = 50; // 0 = worst / smaller file, 100 = better / bigger file

$image_name = str_replace('.png','',$filePath);

imagejpeg($bg, $image_name . ".jpg", $quality);

imagedestroy($bg);


}


?>




<form action="" enctype="multipart/form-data" method="post">
         <input type="file" name="myfile" value="" >
         <input type="submit" name="submit" >
</form>