Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Monday, November 25, 2019

Get Static Properties from a PHP Class

We will get all the static properties as an PHP Array

private function theme_default_value ($key) {
  $full_class_path = Constants::$my_namespace . 'MyClass';
  $class = new \ReflectionClass( $full_class_path );
  $default_value = $class->getStaticPropertyValue( $key );
  return $default_value;
}

Saturday, September 9, 2017

Seeding database in Laravel

Using php artisan make:seeder
=========================

php artisan make:seeder PeopleTableSeeder
inside PeopleTableSeeder class

<?php

use Illuminate\Database\Seeder;
use Faker\Factory as Faker;
use Carbon\Carbon;

class PeopleTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
      $faker = Faker::create();
      DB::table('people')->insert(
          [
            'first_name' => $faker->firstname,
            'last_name' => $faker->lastname,
            'age' => rand(10, 44),
            'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
            'updated_at' => Carbon::now()->format('Y-m-d H:i:s')
          ]
        );
    }
}

In DatabaseSeeder class

$this->call(PeopleTableSeeder::class);


For multiple entry
======================
 DB::table('people')->insert([
   [], []
 ]);

Using PHP factory
====================
$factory->define(App\Person::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->name,
        'first_name' => $faker->firstname,
        'last_name' => $faker->lastname,
        'age' => rand(10, 44)
    ];
});

In DatabaseSeeder class

factory(Person::class, 100000)->create();

Tuesday, March 22, 2016

Necessary MySql command

to log in 
=================
mysql -u root -p

to show databases
===================
SHOW DATABASES;

to select a database
======================
USE databaseName;

RENAME TABLE <old table name> TO <newtable name>

to show tables;
==============================
SHOW TABLES;

to show details of a specific table
===================================
DESCRIBE tableName;

to create a database
============================
CREATE DATABASE databaseName

to delete a database 
======================
DROP DATABASE databaseName

to create a table under a database
===============================
CREATE TABLE tableName(
id INT(11) AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(30) NOT NULL,
email VARCHAR(30) NOT NULL,
contact INT(11),
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

to delete a table
======================================
DROP TABLE tableName;

add primary key to existing table
==============================
ALTER TABLE tableName
ADD PRIMARY KEY (columnName);

remove primary key from a table;
===============================
ALTER TABLE tableName
DROP PRIMARY KEY;

How to delete a column in existing table
====================================
ALTER TABLE tableName
DROP COLUMN
columnName;

How to add a new column in existing table;
=========================================
ALTER TABLE tableName
ADD COLUMN
columnName;

How to add a new column with current timestamp;
===================================
ALTER TABLE tableName
ADD COLUMN
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

How to change datatype of column in mysql
==================================
ALTER TABLE tableName
MODIFY (COLUMN = optional)
columnName VARCHAR(30);

alter a column to be auto increment;
=================================
ALTER TABLE tableName 
MODIFY COLUMN (column keyword is optional) 
columnName INT AUTO_INCREMENT;

insertion in a table
=============================
INSERT INTO 
tableName (column1, column2, column3) 
VALUES ('value_1', 'value_2', 'value_3');

to show all row in table;
===============================
SELECT * FROM tableName;

to update a row in table;
===================================
UPDATE tableName 
SET country = 'india', name = 'Vasanth', email = 'vasanth@gmail.com'
WHERE id = 2; 

to delete a row in a table
=================================
DELETE FROM tableName WHERE id = 3;

how to move a column OR sort column
==========================================
ALTER TABLE tableName 
MODIFY COLUMN (column keyword is optional)
id INT(11) AFTER another_column_name;


ALTER TABLE tableName
MODIFY COLUMN (column keyword is optional)
id INT(11) FIRST;

Friday, September 18, 2015

Readmore function using explode function

<?php

$string = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolorum in minima necessitatibus, neque odit optio perspiciatis quasi ratione suscipit voluptates! Alias blanditiis corporis distinctio exercitationem, facere ipsa necessitatibus quo saepe.";

function read_more($string, $length = 20){
   $string_array = explode(' ', $string);
    $string_array_length = count($string_array);
    $new_string = "";
    if($length > $string_array_length){
        $length = $string_array_length;
    }
   for($i = 0; $i < $length; $i++){
     $new_string .= $string_array[$i] . ' ';
   }
    return $new_string;
}
echo read_more($string, 200);

Sunday, September 13, 2015

file upload in php

<?php
if(isset($_POST['submit'])){
$file = $_FILES['file'];
$name = $file['name'];
    $type = $file['type'];
$tmp_name = $file['tmp_name'];
$error = $file['error'];
$extension = explode('.', $name);
    $extension = end($extension);
    $extension = strtolower($extension);
    $allowed = ['txt', 'php', 'html'];
    $directory = 'assignment/';
    if(in_array($extension, $allowed)){
        $location = $directory . $name;
        move_uploaded_file($tmp_name, $location);
    }
}


?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" id="">
<input type="submit" name="submit" id="">
</form>
</body>
</html>

Saturday, September 12, 2015

queue class for pushing and pop from last

<?php

class Queue {
    public $number = array();
    public $index = -1;
    public function is_empty(){
        if($this->index == -1){
            return true;
        }
        return false;
    }
    public function push($element)
    {
        $this->index++;
        $this->number[$this->index] = $element;
    }
    public function pop_from_last(){
       return array_shift($this->number);
    }

}

$person = new Queue();
$person->push(2);
$person->push(4);
$person->push(8);
$person->push(9);
print_r($person->number);
echo '<br>';
echo $person->pop_from_last();
echo '<br>';
print_r($person->number);

storing form data to a file (working with file)

<?php
if(isset($_POST['name'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $contact = $_POST['contact'];
    $string = $name . " \t "  . $email . " \t " . $contact . "\n";
    $pointer = fopen('myfile.txt', 'a+');
    fwrite($pointer, $string);
}


?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <h1>Please contact with us</h1>
    <form method="post">
       <div class="form-group">
           <label for="name">Name</label>
           <input type="text" name="name" id="name" class="form-control">
       </div>
        <div class="form-group">
            <label for="email">Email</label>
            <input type="text" name="email" id="email" class="form-control">
        </div>
        <div class="form-group">
            <label for="contact">Contact</label>
            <input type="text" name="contact" id="contact" class="form-control">
        </div>
        <div class="form-group">
            <input type="submit" name="submit" id="contact" class="btn btn-lg btn-success">
        </div>

    </form>
</div>

</body>
</html>

Wednesday, September 9, 2015

understanding iterating loop by example

<?php
//following loop will iterate 66 times
$index = 1;
for($i = 0; $i <= 10; $i++){
    for($j = 0; $j <= 5; $j++){
        echo $index . " ) ";
        echo $i . ', ' . $j;
        echo '<br>';
        $index++;
    }
}
echo '<hr>';
//This following loop will be iterate 55 times
$index = 1;
for($i = 0; $i <= 10; $i++){
    for($j = 0; $j <= 5; $j++){
        if($j == 0){
            continue;
        }
        echo $index . " ) ";
        echo $i . ', ' . $j;
        echo '<br>';
        $index++;
    }
}

echo '<hr>';
//This following loop will be iterate 60 times
$index = 1;
for($i = 0; $i <= 10; $i++){
        if($i == 0){
            continue;
        }
    for($j = 0; $j <= 5; $j++){
        echo $index . " ) ";
        echo $i . ', ' . $j;
        echo '<br>';
        $index++;
    }
}

Reverse Polish notation Calculator using stack (for better understanding oop concept)

<?php

class Stack {
    public $array = array();
    public $index = -1;

    public function isEmpty()
    {
        if($this->index == -1){
            return true;
        }else{
            return false;
        }
    }
    public function push($element)
    {
        $this->index++;
        $this->array[$this->index] = $element;
    }

    public function pop()
    {
        //return $this->array[$this->index--];
        if($this->isEmpty()){
           echo 'Nothing to pop';
        }else{
            $element = $this->array[$this->index];
            unset($this->array[$this->index]);
            $this->index--;
            return $element;

        }

    }
    public function calculator($value1, $value2, $operator){
        if($operator == "+"){
            $result = $value1 + $value2;
        } elseif($operator == "-"){
            $result = $value1 - $value2;
        } elseif($operator == "*"){
            $result = $value1 * $value2;
        } elseif($operator == "/"){
            $result = $value1 / $value2;
        }

        return $result;
    }

}
$stack = new Stack();
if(isset($_POST['numbers'])){
    $numbers = trim($_POST['numbers']);
    $numbers = explode(' ', $numbers);
    foreach ($numbers as $number) {
       if(is_numeric($number)) {
           $number = intval($number);
           $stack->push($number);
       }else{
           $value2 = $stack->pop();
           $value1 = $stack->pop();
           $result = $stack->calculator($value1, $value2, $number);
           $stack->push($result);
       }
    }
$result = $stack->pop();
    echo $result;
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<form method="post" action="">
    <input type="text" name="numbers" id="">
    <input type="submit" name="submit" id="">
</form>
</body>
</html>

array stack and queue function with initialize array using construct function

<?php
class Stack {
    private $stack_array = array();
    public $index = -1;

    public function __construct($array)
    {
        $this->stack_array = $array;
        $length = count($this->stack_array);
        $this->index = $length - 1;
    }
    public function print_array()
    {
        print_r($this->stack_array);
    }
    public function isEmpty()
    {
        if($this->index == -1){
            return true;
        }else{
            return false;
        }
    }

    public function push($element)
    {
        $this->index++;
        $this->stack_array[$this->index] = $element;
    }

    public function pop()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            $element = $this->stack_array[$this->index];
            unset($this->stack_array[$this->index]);
            $this->index--;
        }
    }

    public function pop_from_last()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            $element = $this->stack_array[0];
            unset($this->stack_array[0]);
            $this->index--;
            return $element;
        }
    }
    public function first()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            return $this->stack_array[$this->index];
        }
    }

    public function last()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            return $this->stack_array[0];
        }
    }

    public function get_element($position)
    {
        if($position >= $this->index + 1){
            echo 'position is exceeding the array index';
        }else{
            $i = $this->index - ($position - 1);
            return $this->stack_array[$i];

        }
    }
}

$stack = new Stack([3, 4, 5, 8]);
$stack->print_array();
echo $stack->index;

Tuesday, September 8, 2015

array stack and queue function in a class to demonstrate push, pop, first, last, pop from last, get element

<?php
class Stack {
    private $stack_array = array();
    private $index = -1;

    public function print_array()
    {
        print_r($this->stack_array);
    }
    public function isEmpty()
    {
        if($this->index == -1){
            return true;
        }else{
            return false;
        }
    }

    public function push($element)
    {
        $this->index++;
        $this->stack_array[$this->index] = $element;
    }

    public function pop()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            $element = $this->stack_array[$this->index];
            unset($this->stack_array[$this->index]);
            $this->index--;
        }
    }

    public function pop_from_last()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            $element = $this->stack_array[0];
            unset($this->stack_array[0]);
            $this->index--;
            return $element;
        }
    }
    public function first()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            return $this->stack_array[$this->index];
        }
    }

    public function last()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            return $this->stack_array[0];
        }
    }

    public function get_element($position)
    {
        if($position >= $this->index + 1){
           echo 'position is exceeding the array index';
        }else{
            $i = $this->index - ($position - 1);
            return $this->stack_array[$i];

        }
    }
}

$stack = new Stack();
$stack->push(8);
$stack->push(5);
$stack->push(4);
$stack->push(9);
$stack->push(2);
$stack->print_array();
echo '<br>';
echo $stack->get_element(1);
echo '<br>';
echo $stack->get_element(2);
echo '<br>';
echo $stack->get_element(3);
echo '<br>';
echo $stack->get_element(30);

Monday, September 7, 2015

Basic object oriented programming in php

<?php

function is_phone_number($number){
$length = strlen($number);
if($length == 11){

for ($i=0; $i < $length; $i++) { 
if(ord( $number[$i]) < ord('0') ||  ord( $number[$i]) > ord('9')){
return false;
}
}
return true;
}
return false;


class Person {
public $name;
public $profession;
public $phone_number;
public function check_phone_number($phone_number){
if(is_phone_number($phone_number)){
$this->phone_number = $phone_number;
}
}
}

$polo = new Person();
$polo->name = 'Polo dev';
$polo->profession = 'Student';
$polo->check_phone_number('01670978989');

echo $polo->name . '<br>'; 
echo $polo->profession . '<br>'; 
echo $polo->phone_number . '<br>'; 

array stack class to demonstrate push pop first last

<?php
class Stack {
    private $array = array(2, 3, 4, 5);
    public function print_array(){
        print_r( $this->array );
    }
    public function push_to_array($value){
        $this->array[] = $value;
    }
    public function pop_from_array(){
        $count = count($this->array) - 1;
        if(empty($this->array[$count])){
           echo 'array is empty';
        } else{
            $pop_value = $this->array[$count];
            unset($this->array[$count]);
            return $pop_value;
        }
    }
    public function last(){
        if(empty($this->array[0])){
            return 'array is empty';
        } else{
            return $this->array[0];
        }
    }
    public function first(){
        $count = count($this->array) - 1;
        if(empty($this->array[$count])){
            return 'array is empty';
        } else{
            return $this->array[$count];
        }
    }

}

$array = new Stack();
echo $array->pop_from_array();
$array->print_array();

Sunday, September 6, 2015

setcookie() function and $_COOKIE[] super global usage

<?php

if(isset($_GET['name'])){
   $name = $_GET['name'] ;
    setcookie('user_name', $name, time()+1000);
    $user_name = $name;
}

if(isset($_COOKIE['user_name'])){
$user_name = $_COOKIE['user_name'];
}
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            <br><br>
            <?php if(isset($user_name)){
                echo '<h1> Welcome ' . $user_name .'</h1>';
            }else{

             ?>
            <form method="get" action="">
               <div class="form-group text-center">
                   <label class="control-label" for="name">Name</label>
                   <input class="form-control" type="text" name="name" id="name">
               </div>
                <div class="form-group">
                    <input class="btn btn-block" type="submit" name="submit" id="name">
                </div>
            </form>
            <?php } ?>
        </div>
    </div>
</div>
</body>
</html>

Saturday, September 5, 2015

manual dictionary function in php

<?php
function lowerCase($string){
    $new_string = "";
    $length = strlen($string);
    for($i = 0; $i < $length; $i++){
        if(ord($string[$i]) >= 65 && ord($string[$i]) <= 91){
            $new_string .= chr(ord($string[$i]) + 32);
        }else{
            $new_string .= $string[$i];
        }
    }
    return $new_string;
}

function which_is_first($string1, $string2){
    $string1 = lowerCase($string1);
    $string2 = lowerCase($string2);
    $len1 = strlen($string1);
    $len2 = strlen($string2);
    if($len1 < $len2){
        $min_len = $len1;
    }else{
        $min_len = $len2;
    }
    for($i = 0; $i < $min_len; $i++){
        if($string1[$i] > $string2[$i]){
            return 2;
        }elseif($string1[$i] < $string2[$i]){
            return 1;
        }
    }
    if($len1 < $len2){
        return 1;
    }else{
        return 2;
    }

}
function dictionary($words){
    $arr_len = count($words);
    for($i = 0; $i < $arr_len; $i++){
        for($k = $i; $k < $arr_len; $k++){
            if(which_is_first($words[$i], $words[$k]) == 2 ){
                $temp = $words[$i];
                $words[$i] = $words[$k];
                $words[$k] = $temp;
            }
        }
    }
    return $words;
}
$dictionary = dictionary(['cat', 'bat', 'ball', 'home', 'he', 'she']);
print_r($dictionary);

function for min number from array - manually

<?php

function min_number($array){
    $min_number = $array[0];
    $length = count($array);
    for($i = 0; $i < $length; $i++){
        if($min_number > $array[$i]){
           $min_number = $array[$i];
        }
    }
    return $min_number;
}

manual reverse string function in php

<?php

function string_rev($string){
    $new_string = "";
    $length = strlen($string) - 0;
    for($i = $length; $i <= 0; $i--){
        $new_string .= $string[$i];
    }
    return $new_string;
}

manual string length function in php

<?php

function string_len($string){
    $i = 0;
    while(isset($string[$i])){
       $i++ ;
    }
    return $i;
}

function for lower word between 2 word - basic logic of dictionary

<?php
function lowerCase($string){
    $new_string = "";
    $length = strlen($string);
    for($i = 0; $i < $length; $i++){
        if(ord($string[$i]) >= 65 && ord($string[$i]) <= 91){
            $new_string .= chr(ord($string[$i]) + 32);
        }else{
            $new_string .= $string[$i];
        }
    }
    return $new_string;
}

function lower_word($string1, $string2){
    $string1 = lowerCase($string1);
    $string2 = lowerCase($string2);
    $len1 = strlen($string1);
    $len2 = strlen($string2);
    if($len1 < $len2){
        $min_len = $len1;
    }else{
        $min_len = $len2;
    }
    for($i = 0; $i < $min_len; $i++){
        if($string1[$i] > $string2[$i]){
            return $string2;
        }elseif($string1[$i] < $string2[$i]){
           return $string1;
        }
    }
    if($len1 < $len2){
        return $string1;
    }else{
        return $string2;
    }

}
echo lower_word('Bat', 'ball');

Thursday, September 3, 2015

manual anagram in php

<?php
function lowerCase($string){
    $new_string = "";
    $length = strlen($string);
    for($i = 0; $i < $length; $i++){
        if(ord($string[$i]) >= 65 && ord($string[$i]) <= 91){
            $new_string .= chr(ord($string[$i]) + 32);
        }else{
            $new_string .= $string[$i];
        }
    }
    return $new_string;
}
function is_anagram($string_one , $string_two){
    $string_one = lowerCase($string_one);
    $string_two = lowerCase($string_two);
    $length_string_one = strlen($string_one);
    $length_string_two = strlen($string_two);
    $length = 0;
    if ($length_string_one == $length_string_two){
        for($i = 0; $i < $length_string_one; $i++){
            for($k = 0; $k < $length_string_one; $k++){
                if($string_one[$i] == $string_two[$k]){
                    $length += 1;
                    break;
                }
            }
        }
        if($length == $length_string_one){
            return true;
        }else{
            return false;
        }
    }
}

echo is_anagram('bangladesh', 'eshbanglad');

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }