abc

Share This blog with your friends, so that we can improve more & more . our aim is to easy & simple way of learning.

12/15/2020

What is scrollspy , menu according to scroll.

In this shows moving scroll of page from sticky menu , with simple use of bootstrap. less code use, header is stick and navigation detect if user move scroll , easily current block div of which menu item.


Example

<html lang="en">

<head>

  <title>Bootstrap scroll detect and menu change </title>

  <meta charset="utf-8">

  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

  <style>

  body {

      position: relative; 

  }

  </style>

</head>

<body data-spy="scroll" data-target=".navbar" data-offset="50">


<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">  

<ul class="navbar-nav">

<li class="nav-item">

<a class="nav-link" href="#Fruit">Fruit</a>

</li>

<li class="nav-item">

<a class="nav-link" href="#Vegitable">Vegitable</a>

</li>

<li class="nav-item">

<a class="nav-link" href="#Electronic">Electronic item</a>

</li>

<li class="nav-item dropdown">

<a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">

Section 4

</a>

<div class="dropdown-menu">

<a class="dropdown-item" href="#section41">laptop</a>

<a class="dropdown-item" href="#section42">cooling items</a>

</div>

</li>

</ul>

</nav>


<div id="Fruit" class="container-fluid bg-success" style="padding-top:70px; padding-bottom:70px">

  <h1>Fruit</h1>

  <p>Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar </p>

</div>

<div id="Vegitable" class="container-fluid bg-warning" style="padding-top:70px;padding-bottom:70px">

  <h1>Vegitable</h1>

  <p>Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar </p>

</div>

<div id="Electronic" class="container-fluid bg-secondary" style="padding-top:70px;padding-bottom:70px">

  <h1>Electronic item</h1>

  <p>Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar </p>

</div>

<div id="laptop" class="container-fluid bg-danger" style="padding-top:70px;padding-bottom:70px">

  <h1>laptop</h1>

  <p>Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar </p>

</div>

<div id="mobile" class="container-fluid bg-info" style="padding-top:70px;padding-bottom:70px">

  <h1>cooling items</h1>

  <p>Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar Try to scroll or click on menu bar </p>

</div>


</body>

</html>


1) call bootstrap.min.js and css 

2) 'nav-item' & 'nav-item dropdown' using this class name add menubar

3) create div with same id as href link for menu item.


How to send attachment email using PHP

1) use basic mail function available in PHP 

2) get file type using application/octet-stream

3) form must  enctype="multipart/form-data"

4) sure to use MD5 encryption & chunk_split(base64_encode)

5) to upload file use move_uploaded_file



<?php

$from = $_REQUEST["from"];

$emaila = $_REQUEST["emaila"];

$filea = $_REQUEST["filea"];


if ($filea)

{

function mail_attachment ($from , $to, $subject, $message, $attachment)

{

$fileattachis = $attachment; // Path to the file

$fileattachis_type = "application/octet-stream"; // File Type 

$start = strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;

$fileattachis_name = substr($attachment, $start, strlen($attachment));

$email_from = $from; //email from

$subject = "New Attachment Message";

$email_subject =  $subject;

$email_txt = $message;

$email_to = $to;

$headers = "From: ".$email_from;

$file = fopen($fileattachis,'rb'); 

$data = fread($file,filesize($fileattachis)); 

fclose($file); 

$msg_txt="\n\n You have recieved a new attachment email from $from";

$semi_rand = md5(time()); 

$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . "

boundary=\"{$mime_boundary}\"";

$email_txt .= $msg_txt;

$email_message .= "This is a multi-part message in MIME format.\n\n" . 

"--{$mime_boundary}\n" . "Content-Type:text/html; 

charset = \"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . 

$email_txt . "\n\n";

$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileattachis_type};\n" .

" name = \"{$fileattachis_name}\"\n" . //"Content-Disposition: attachment;\n" . 

//" filename = \"{$fileattachis_name}\"\n" . "Content-Transfer-Encoding: 

base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n";

$ok = mail($email_to, $email_subject, $email_message, $headers);

if($ok)

{

echo "File Sent Successfully.";

unlink($attachment); // delete a file after attachment sent.

}

else

{

die("Sorry email not sent. Please try again!");

}

}

move_uploaded_file($_FILES["filea"]["tmp_name"], 'temp/'.basename($_FILES['filea']['name']));


mail_attachment("$from", "youremailaddress@gmail.com", "subject", "message", ("temp/".$_FILES["filea"]["name"]));

}

?>


<html>

<head>


<script type = "text/javascript">

function checkfile() {

with(document.filepost) {

if(filea.value ! = "") {

document.getElementById('textis').innerText = "Attaching File ... Please Wait";

}

}

}

</script>


</head>

<body>


 

<form name= "filepost" method="post" action="file.php" enctype="multipart/form-data" id = "file">

<table width = "300" border = "0" cellspacing = "0" cellpadding = "0">

<tr valign = "bottom">

<td height = "20">Name:</td>

</tr>

<tr>

<td>

<input name = "from" type = "text" id = "from" size = "30">

</td>

</tr>

<tr valign = "bottom">

<td height = "20">Email Address:</td>

</tr>

<tr>

<td class = "frmtxt2">

<input name = "emaila" type = "text" id = "emaila" size = "30">

</td>

</tr>

<tr>

<td height = "20" valign = "bottom">Attachment:</td>

</tr>

<tr valign = "bottom">

<td valign = "bottom">

<input name = "filea" type = "file" id = "filea" size = "16">

</td>

</tr>

<tr>

<td height = "40" valign = "middle">

<input name = "Reset2" type = "reset" id = "Reset2" value = "Reset">

<input name = "Submit2" type = "submit" value = "Submit" onClick = "return checkfile()">

</td>

</tr>

</table>


</form>


<div id="textis"></div>


</body>

</html>


Encryption and Decryption in PHP

Encryption means convert to such form which is not easy to understand .

base64_encode which encrypt current string or data , also helpful to store secure data.

base64_decode which decrypt encrypted data. 


<?php


$var1 = 'test'; 


echo base64_encode($var1); 


//dGVzdA==


echo '<p>'.base64_decode('dGVzdA');

?>


md5 encryption - which cannot decode - one way encryption


<?php

$sampl = 'sample text here';


echo md5($sampl);


?>


What is and How to create PHP array

 array - store multiple value into single variable -

three types of array -  index, associative, Multidimensional array.


1) index array - store value into variable in curley and quoted form.


for example - 


$name = {'drip','pipe','sprinkler'};

echo $name[0] . '  '.$name[2];


2) Associative array - associate some value 


let us see example, 

$price = array("mobile"=>"8500","fan"=>"4500","light"=>"300"); 

echo $price['light'];


3) Multidimensional array - multiple checkup done


for example we have two for loop which check in $emp array and get respective value


echo $emp[0][0] ouput is 1,"sonoo",400000

<?php    

$emp = array  

  (  

  array(1,"sonu",4000),  

  array(2,"monu",5000),  

  array(3,"pinu",7000)  

  );  

  

for ($row = 0; $row < 3; $row++) {  

  for ($col = 0; $col < 3; $col++) {  

    echo $emp[$row][$col]."  ";  

  }  

  echo "<br/>";  

}  

?>   


Bootstrap Filter table - search into table

1)  Call bootstrap.min.js and CSS

2) make table & one input

3) jQuery keyup function which is useful when start typing it check in table, for each character check using keyup function.

4) filter function default in jQuery


<html lang="en">

<head>

  <title>Bootstrap filter table.</title>

  <meta charset="utf-8">

  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

</head>

<body>


<div class="container mt-3">

<input class="form-control" id="valuetosearch" type="text" placeholder="Search..">


<table class="table table-bordered">

<thead>

<tr>

<th>Firstname</th>

<th>Lastname</th>

<th>Email</th>

</tr>

</thead>

<tbody id="searchintotable">

<tr>

<td>kumar</td>

<td>sample</td>

<td>abc@example.com</td>

</tr>

<tr>

<td>check</td>

<td>hi</td>

<td>check@mail.com</td>

</tr>

<tr>

<td>learn</td>

<td>joy</td>

<td>lear@greatstuff.com</td>

</tr>

<tr>

<td>blog</td>

<td>where</td>

<td>blog@test.com</td>

</tr>

</tbody>

</table>

</div>


<script>

$(document).ready(function(){

  $("#valuetosearch").on("keyup", function() {

    var value = $(this).val().toLowerCase();

    $("#searchintotable tr").filter(function() {

      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)

    });

  });

});

</script>


</body>

</html>

PHP Function call by value and call by reference

 <?php  

function increment($i)  

{  

    $i++;  

}  

$i = 10;  

increment($i);  

echo $i;  

?>


above function output is 10 



Call by Reference - 


<?php  

function adder(&$str2)  

{  

    $str2 .= 'Call By Reference';  

}  

$str = 'This is ';  

adder($str);  

echo $str;  

?>


above returns output is 'this is call by reference'

use & (ampersand) symbol with formal arguments. The & represents reference of the variable.


What is PHP PDO, how it's work

 PDO refers to PHP Data Object, which is a PHP extension that defines a quick access database. 

Each database driver can expose database-specific features as a regular extension function that implements the PDO interface.

pdo connect to various database like sqlite, mysql, oracle


example of connect to database using PDO


<?php  

    $dbHost="localhost";  

    $dbName="myDB";  

    $dbUser="root"; 

    $dbPassword=""; 

    try{  

        $dbConn= new PDO("mysql:host=$dbHost;dbname=$dbName",$dbUser,$dbPassword);  

        Echo "Successfully connected with myDB database";  

    } catch(Exception $e){  

    Echo "Connection failed" . $e->getMessage();  

    }  

?> 


//above code short as $dbconn = new PDO("localhost","root","","myDB"); no need first five lines.


PDO is good as compare to mysqli , because, it connect with any database, mysqli only connect phpmyadmin mysql.


this PDO  connection short and quick connect. 


** PLEASE LIKE - SHARE - COMMENT **

What are magic constant in PHP ?

 1) __LINE__ it returns current line number 


example

<?php

 echo 'current line'.__LINE__;

?> 


2) __FILE__ - get full path of current file 


example -  echo __FILE__ . "<br><br>";  


3) __DIR__  - get full directory path


    echo __DIR__ . "<br><br>";  

OR  

echo dirname(__FILE__) . "<br><br>";    


4) __FUNCTION__ - get function name where magic constant used.


<?php   

    echo "<h3>Example for __FUNCTION__</h3>";    

   

    function test(){    

         echo 'The function name is '. __FUNCTION__ . "<br><br>";   

    }    

    test();    

    

    echo  __FUNCTION__ . "<br><br>";  

?>  


5) __CLASS__ - get class name


below example output is 'base'

<?php   

    class base  

    {    

function test_first(){    

             echo __CLASS__;   

        }    

    }    

    class child extends base    

    {    

        public function __construct() {    

            ;    

        }    

    }    

    $t = new child;    

    $t->test_first();    

?>  


6) __TRAIT__  get trait name


for eg -

trait created_trait {    

        function jtp(){    

            echo __TRAIT__;  

        }    

    }   


7) __METHOD__ get method name



public function meth_fun(){    

            //print method::meth_fun    

                echo __METHOD__;   

        }    


8) __NAMESPACE__  current namespace


 class name {    

        public function __construct() {    

            echo 'This line will print on calling namespace.';     

        }     

    }    

    $class_name = __NAMESPACE__ . '\name';    

    $a = new class_name;   

9) ClassName::class  - full name of class


output of below example is Technical_Portal/javatpoint

namespace Technical_Portal;  

    echo "<h3>Example for CLASSNAME::CLASS </h3>";  

class javatpoint {    

}  

    echo javatpoint::class;

in short magic contstant in php returns the current elements like filename, filepath, classname, methodname, directory path.


An Introduction to the Laravel Framework: What It Is and Why You Should Use It

  If you're a PHP developer looking for a modern, efficient, and powerful framework to build web applications, look no further than Lara...