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

example of global scope and php current page using PHP_SELF

<?php
$x = 5;
$y = 7;
function add(){
    global $x, $y; //can't assign any value when declaring global @ same line
    return $x + $y;
}
echo add();

echo '<br>';
echo $_SERVER['PHP_SELF']; //this will return the current page location

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

object oriented programming in php :: inheritance

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

class Guest extends Person{
    public $indian_phone_number;
    public function check_indian_phone_number($phone_number){
        if(is_phone_number($phone_number . '0')){
            $this->indian_phone_number = $phone_number;
        }
    }
}

//$indian = new Guest();
//$indian->name = 'Daniel';
//$indian->profession = "Soft skill trainer";
//$indian->check_phone_number('01670978989');
//$indian->check_indian_phone_number('0123456789');
//print_r($indian);
$indian2 = new Guest();
$indian2->name = 'Vasanth';
$indian2->profession = 'Trainer';
$indian2->check_phone_number('01670978989');
$indian2->check_indian_phone_number('8008900910');
print_r($indian2);

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

string replace in php manually

<?php
function matching ($string, $matching_word) {
    $length_first = strlen($string);
    $length_second = strlen($matching_word);
    for($i = 0; $i < $length_first; $i++){
        for($j = 0; $j < $length_second; $j++){
            if($string[$i + $j] == $matching_word[$j]){
                if($j == $length_second - 1){
                    return  $i;
                }
            } else{
                break;
            }

        }
    }
    return -1;
}


$string = 'bangladesh is our home land';
$length_string = strlen($string);
$matching_word = 'home';
$length_matching_word = strlen($matching_word);
$replace_word = 'native';
$length_replace_word = strlen($replace_word);
$new_string = "";

$index = matching($string, $matching_word);

if($index == -1){
echo 'replace_word not found';
}else{

for($i = 0; $i < $index; $i++){
   $new_string .= $string[$i];
}
for($k = 0; $k < $length_replace_word; $k++){
   $new_string .= $replace_word[$k];
}
$index = $index + $length_matching_word;
for($l = $index; $l < $length_string; $l++){
   $new_string .= $string[$l];
}

echo $new_string;
}

manual string replace function in php - vasanth sai sir code

<?php

function getMatchIndex($orig,$find)
{
$origLen = strlen($orig);
$findLen = strlen($find);
for($i=0;$i<$origLen;$i++){
for($j=0;$j<$findLen;$j++)
{
if($i+$j < $origLen)
{
if($orig[$i+$j]==$find[$j])
{
if($j==$findLen-1)
{
return $i;
}
}
}
else
{
break;
}
}
}
return -1;
}

function replaceString($orig,$find,$rep,$index)
{
$origLen = strlen($orig);
$findLen = strlen($find);
$finalString = "";
for($i=0;$i<$index;$i++)
{
$finalString = $finalString.$orig[$i];
}
$finalString = $finalString.$rep;
for($i=$index+$findLen;$i<$origLen;$i++)
{
$finalString = $finalString.$orig[$i];
}
return $finalString;
}

function myStrRep($orig,$find,$rep)
{
$index = getMatchIndex($orig,$find);
if($index == -1)
{
echo "$find not found in $orig";
}
else
{
echo replaceString($orig,$find,$rep,$index);
}
}

myStrRep("hello","el","lo");

?>

Wednesday, September 2, 2015

manual sqrt function using php

<?php

//for precision number
//number_format(45.234729492, 3) = 45.235;
//number_format(45.234729492, 2) = 45.23;
//number_format(405.234729492, 2) = 405.23;

function mySqrt($number){
    $first_range = 1;
    $last_range = $number;
    $middle_point = 0;
    $sq_middle_point = 0;
    while(true){
        $middle_point = number_format(($first_range+$last_range) / 2, 3);
        $sq_middle_point = number_format($middle_point ** 2, 3);
        if($sq_middle_point == $number){
            break;
        }
        if($sq_middle_point > $number){
            $last_range = $middle_point;
        }else{
            $first_range = $middle_point;
        }

    }
    return $middle_point;
}

echo mySqrt(5);

Manual upper case function and lower case function and case_change function in php

<?php
//chr("ASCII value") will return a character
//ord('character') will return ASCII value of this respective character
//ord('a') = 97
//ord('z') = 123
//ord('A') = 65
//ord('Z') = 91
//chr("97") = a
//chr("123") = z
//chr("65") = A
//chr("91") = Z
function case_change($string){
   if(ord($string) < 97){
       $string = chr(ord($string) + 32);
   }else{
       $string = chr(ord($string) - 32);
   }
   return $string;
}

function upperCase($string){
    $new_string = "";
    $length = strlen($string);
    for($i = 0; $i < $length; $i++){
        if(ord($string[$i]) >= 97 && ord($string[$i]) <= 123){
            $new_string .= chr(ord($string[$i]) - 32);
        }else{
            $new_string .= $string[$i];
        }
    }
    return $new_string;
}

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



if(isset($_POST['string'])){
    $string = $_POST['string'];
    $upperString = upperCase($string);
    $lowerString = lowerCase($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>Put sentence in input box</h1>
    <form action="" method="post">
        <input type="text" name="string" id="" class="form-control"><br>
        <input type="submit" name="" id="" class="btn btn-lg btn-success">
    </form>
    <br>
    Upper Case: <br>
    <?php
    if(isset($upperString)){
        echo $upperString;
    }

    ?>
    <br>
    Lower Case: <br>
    <?php
    if(isset($lowerString)){
        echo $lowerString;
    }

    ?>
</div>
</body>
</html>

DOM manipulation in php - bad practice - only for getting logic

<?php
    $heading = true;
    $paragraph = true;

    if(isset($_POST['heading'])){
        $heading = true;
        $paragraph = false;
    }

    if(isset($_POST['paragraph'])){
        $paragraph = true;
        $heading = false;
    }

    if(isset($_POST['all'])){
        $paragraph = true;
        $heading = true;
    }

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <style>
        p{
            margin-top: 25px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-4">
            <form method="post" action="">
                <br><br>
                <input type="submit" name="heading" value="heading" class="btn btn-success btn-lg">
                <br><br>
                <input type="submit" name="paragraph" value="paragraph" class="btn btn-success btn-lg">
                <br><br>
                <input type="submit" name="all" value="all" class="btn btn-success btn-lg">
            </form>
        </div>
        <div class="col-md-8">
            <div id="main">
                <?php if($heading) { ?>
                <h1 id="heading">This is heading</h1>
                <?php } if ($paragraph) {?>
                <p id="paragraph">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid aspernatur assumenda at aut dolor doloremque earum inventore, iure magni nesciunt porro quis veritatis voluptate? Assumenda necessitatibus quidem reprehenderit voluptas? Accusantium? Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad architecto aut, dicta laudantium nam quam ratione rem sequi ullam vel? Ad animi delectus est libero maxime odit reiciendis, soluta! Harum!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci alias beatae deleniti dolore excepturi iste iusto maxime nesciunt nihil, nisi obcaecati placeat quae similique sit soluta! Nemo praesentium qui quod!</p>
                <?php } ?>
            </div>
        </div>
    </div>
</div>
</body>
</html>

Tuesday, September 1, 2015

How to remove all childNode by iterating and append it again by iteration - on demand - more modular approach - using switch statement

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <style>
        p{
            margin-top: 25px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-4">
            <h1 class="btn btn-success btn-lg" onclick="content_change(1)">Heading</h1><br>
            <h1 class="btn btn-warning btn-lg" onclick="content_change(2)">paragraph</h1> <br>
            <h1 class="btn btn-danger btn-lg" onclick="content_change(3)">Show all</h1>
        </div>
        <div class="col-md-8">
            <div id="main">
                <h1 id="heading">This is heading</h1>
                <p id="paragraph">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid aspernatur assumenda at aut dolor doloremque earum inventore, iure magni nesciunt porro quis veritatis voluptate? Assumenda necessitatibus quidem reprehenderit voluptas? Accusantium? Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad architecto aut, dicta laudantium nam quam ratione rem sequi ullam vel? Ad animi delectus est libero maxime odit reiciendis, soluta! Harum!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci alias beatae deleniti dolore excepturi iste iusto maxime nesciunt nihil, nisi obcaecati placeat quae similique sit soluta! Nemo praesentium qui quod!</p>
            </div>
        </div>
    </div>
</div>
<script>
    var main = document.getElementById('main');
    var all_child = [];
    for(var i = 0; i < main.childNodes.length; i++){
        all_child.push( main.childNodes[i]);
    }
    function content_change(arg){
        while(main.hasChildNodes()){
            main.removeChild(main.firstChild);
        }
        switch (arg){
            case 1:
                main.appendChild(all_child[1]);
                break;
            case 2:
                main.appendChild(all_child[3]);
               break;
            case 3:
                for(var j = 0; j < all_child.length; j++){
                    main.appendChild(all_child[j]);
                }
        }

    }
</script>
</body>
</html>

css snippet for blogger code highlighting

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