Sunday, 26 June 2016

Wordpress PLUGIN adding script

1) Add script or link js file  in wordpress plugin

        CREATE js FILE IN PLUGIN FOLDER

function wpb_adding_scripts() {
wp_register_script('my_amazing_script', plugins_url('amazing_script.js', __FILE__), array('jquery'),'1.1', true);
wp_enqueue_script('my_amazing_script');
}
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );  
?>

Thursday, 23 June 2016

How to send SMTP email from localhost in PHP?

You can send mail from localhost with sendmail package , sendmail package is inbuild in XAMPP. So if you are using XAMPP then you can easily send mail from localhost.
for example you can configure C:\xampp\php\php.ini and c:\xampp\sendmail\sendmail.ini for gmail to send mail.
in C:\xampp\php\php.ini find extension=php_openssl.dll and remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.
in php.ini file find [mail function] and change
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = my-gmail-id@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Now Open C:\xampp\sendmail\sendmail.ini. Replace all the existing code in sendmail.ini with following code
[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=my-gmail-id@gmail.com
auth_password=my-gmail-password
force_sender=my-gmail-id@gmail.com
Now you have done!! create php file with mail function and send mail from localhost.

Thursday, 16 June 2016

ng-repeat Across Multiple HTML Elements


<html ng-app="notesApp">
<body ng-controller="MainCtrl as ctrl">
<table>
<tr ng-repeat-start="note in ctrl.notes">
<td>{{note.label}}</td>
</tr>
<tr>
<td>value: {{note.value}}</td>
</tr>
<tr ng-repeat-end>
<td>Done: {{note.done}}</td>
</tr>
</table>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js">
</script>
<script type="text/javascript">
angular.module('notesApp', [])
.controller('MainCtrl', [function() {
var self = this;
self.notes = [
{id: 1, label: 'First Note', value:'1', done: false},
{id: 2, label: 'Second Note', value:'2', done: false},
{id: 3, label: 'Finished Third Note', value:'3', done: true}
];
}]);
</script>
</body>
</html>


OUTPUT

First Note
value: 1
Done: false
Second Note
value: 2
Done: false
Finished Third Note
value: 3
Done: true

Angular.js Helper Variables in ng-repeat

<html ng-app="notesApp">
<head><title>Notes App</title></head>
<body ng-controller="MainCtrl as ctrl">
<div ng-repeat="note in ctrl.notes">
<div>First Element: {{$first}}</div>
<div>Middle Element: {{$middle}}</div>
<div>Last Element: {{$last}}</div>
<div>Index of Element: {{$index}}</div>
<div>At Even Position: {{$even}}</div>
<div>At Odd Position: {{$odd}}</div>
<span class="label"> {{note.label}}</span>
<span class="status" ng-bind="note.done"></span>
<br/><br/>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js">
</script>
<script type="text/javascript">
angular.module('notesApp', [])
.controller('MainCtrl', [function() {
var self = this;
self.notes = [
{id: 1, label: 'First Note', done: false},
{id: 2, label: 'Second Note', done: false},
{id: 3, label: 'Done Note', done: true},
{id: 4, label: 'Last Note', done: false}
];
}]);
</script>
</body>
</html>

OUTPUT

First Element: true
Middle Element: false
Last Element: false
Index of Element: 0
At Even Position: true
At Odd Position: false
First Note false
First Element: false
Middle Element: true
Last Element: false
Index of Element: 1
At Even Position: false
At Odd Position: true
Second Note false
First Element: false
Middle Element: true
Last Element: false
Index of Element: 2
At Even Position: true
At Odd Position: false
Done Note true
First Element: false
Middle Element: false
Last Element: true
Index of Element: 3
At Even Position: false
At Odd Position: true
Last Note false 

Explation:
In this example, we use the same array that we did in the example with the ng-repeat
over the array of items. The only difference is that we now display more state about the
item being repeated in the HTML. For each item, we display which index the item is in,
and whether it is the first, middle, last, odd, or even item.


Angular.js ng-repeat Over an Object

<html ng-app="notesApp">
<head><title>Notes App</title></head>
<body ng-controller="MainCtrl as ctrl">
<div ng-repeat="(author, note) in ctrl.notes">
<span class="label"> {{note.label}}</span>
<span class="author" ng-bind="author"></span>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js">
</script>
<script type="text/javascript">
angular.module('notesApp', [])
.controller('MainCtrl', [function() {
var self = this;
self.notes = {
shyam: {
id: 1,
label: 'First Note',
done: false
},
Misko: {
id: 3,
label: 'Finished Third Note',
done: true
},
brad: {
id: 2,
label: 'Second Note',
done: false
}
};
}]);
</script>
</body>
</html>

OUTPUT


Finished Third Note Misko
Second Note brad
First Note shyam

Explanation:
In this example, we have intentionally capitalized Misko while leaving brad and shyam
lowercase. When we use the ng-repeat directive over an object instead of an array, the
keys of the object will be sorted in a case-sensitive, alphabetic order. That is, uppercase
first, and then sorted by alphabet. So in this case, the items would be shown in the HTML
in the following order: Misko, brad, shyam.

Angular.js More Directives

<html ng-app="notesApp">
<head>
<title>Notes App</title>
<style>
.done {
background-color: green;
}
.pending {
background-color: red;
}
</style>
</head>
<body ng-controller="MainCtrl as ctrl">
<div ng-repeat="note in ctrl.notes"
ng-class="ctrl.getNoteClass(note.done)">
<span class="label"> {{note.label}}</span>
<span class="assignee"
ng-show="note.assignee"
ng-bind="note.assignee">
</span>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js">
</script>
<script type="text/javascript">
angular.module('notesApp', []).controller('MainCtrl', [
function() {
var self = this;
self.notes = [
{label: 'First Note', done: false, assignee: 'Shyam'},
{label: 'Second Note', done: false},
{label: 'Done Note', done: true},
{label: 'Last Note', done: false, assignee: 'Brad'}
];
self.getNoteClass = function(status) {
return {
done: status,
pending: !status
};
};
}]);
</script>
</body>
</html>

OUTPUT

First Note Shyam
Second Note
Done Note
Last Note Brad

Angular.js Working with and Displaying Arrays

<html ng-app="notesApp">
<head><title>Notes App</title></head>
<body ng-controller="MainCtrl as ctrl">
<div ng-repeat="note in ctrl.notes">
<span class="label"> {{note.label}}</span>
<span class="status" ng-bind="note.done"></span>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js">
</script>
<script type="text/javascript">
angular.module('notesApp', [])
.controller('MainCtrl', [function() {
var self = this;
self.notes = [
{id: 1, label: 'First Note', done: false},
{id: 2, label: 'Second Note', done: false},
{id: 3, label: 'Done Note', done: true},
{id: 4, label: 'Last Note', done: false}
];
}]);
</script>
</body>

</html>


OUTPUT


First Note false
Second Note false
Done Note true
Last Note false




Tuesday, 14 June 2016

Display data from mysql using angular.js [with search filter]

display_data.html

<html ng-app="fetch">
    <head>
    <title>AngularJS GET request with PHP</title>
      <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
            <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.24/angular.min.js"></script>
    </head>
    <body>
    <br>
      <div class="row">
        <div class="container">
          <h1>Angular $http GET Ajax Call</h1>
          <div ng-controller="dbCtrl">
          <input type="text" ng-model="searchFilter" class="form-control">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Roll No</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="students in data | filter:searchFilter">
                        <td>{{students.sno}}</td>
                        <td>{{students.name}}</td>
                    </tr>
                </tbody>
            </table>
          </div>
        </div>
      </div>
    </body>
    <script>
        var fetch = angular.module('fetch', []);
        fetch.controller('dbCtrl', ['$scope', '$http', function ($scope, $http) {
            $http.get("select.php")
                .success(function(data){
                    $scope.data = data;
                })
                .error(function() {
                    $scope.data = "error in fetching data";
                });
        }]);
    </script>
    </html>

fetch_data.php

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = mysqli_connect("localhost", "root", "", "task_management");
$result = mysqli_query($conn,"SELECT sno,name,course from students");
$outp = array();
while($rs = mysqli_fetch_array($result)) {
   $outp[] = $rs;
}
$outp = $outp;
$conn->close();
echo json_encode($outp);
?>



Insert JSON Data Into MySQL Using PHP

The following is the JSON file which we have already created earlier.
[{
"sno": "1",
"name": "vikram",
"course": "php"
}, {
"sno": "2",
"name": "lucky",
"course": "java"
}, {
"sno": "3",
"name": "vikram",
"course": "php"
}, {
"sno": "4",
"name": "kkk",
"course": "java"
}, {
"sno": "5",
"name": "kkkkkk",
"course": "kk"

}]

PHP code to Insert JSON data into MySQL using PHP :

$string = file_get_contents("results.json");
$data = json_decode($string,true);
//print_r($data);
$con = mysqli_connect('localhost','root','','test');
//echo $data[0]['sno'];
foreach ($data as $key => $val)
{
       $sno = $val['sno'];
$name = $val['name'];
$course = $val['course'];
$sql = "insert into students2(sno,name,course) values('".$sno."','".$name."','".$course."')";
$result = mysqli_query($con,$sql);
if($result)
echo "Inserted";
else 
echo "Not Inserted";
}
?>

Create A JSON File With PHP And MySQL

How to Create a JSON file with PHP and MySQL ?

Step 1: Create “Students” table in MySQL. The name of the database here is “test”.
CREATE TABLE `students` (
  `sno` int(10) NOT NULL auto_increment,
  `rollno` varchar(20) NOT NULL,
  `sname` varchar(100) NOT NULL,
  `class` varchar(100) NOT NULL,
  PRIMARY KEY  (`sno`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
-- 
-- Dumping data for table `students`
-- 
INSERT INTO `students` VALUES (1, 'PHPT20161', 'vikram', '2nd');
INSERT INTO `students` VALUES (2, 'PHPT20162', 'Lucky', 'LKG');
INSERT INTO `students` VALUES (3, 'PHPT20163', 'Dusyanth', '3rd');
INSERT INTO `students` VALUES (4, 'PHPT20165', 'Lucky', '4th');

Step 2: Now, copy and execute the following PHP Code.

include('dbconfig.php');
$sql = "select * from students";
$result = mysqli_query($con,$sql);
$students_data = array();
while($row=mysqli_fetch_assoc($result))
{
$students_data[] = $row;
}
$json = json_encode($students_data);
//append data in json file 
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($json));
fclose($fp);
?>

Step 3: Copy the dbconfig.php file to access MYSQL DB.
$con = mysqli_connect('localhost','root','','test123'); 
?>

Monday, 13 June 2016

Generate PDF using PHP from MySQL database

For this purpose we will use the popular PHP library FPDF which will enable us to generate the PDF file with the content and ourput format we desire.


Step-1: Get the data from MySQL database into the page
Step-1: Download the FPDF library from fpdf.org
Step-2: Copy the fpdf.php file into your application folder
Step-3: Use the fpdf library like so

books.sql

index.php

database.php

How to send email using NodeJS.

Sometimes we may need an email form or contact form for our website
Scenario-1: We wish our users to be able to communicate to the admin, author, managers of our website.
Scenario-2: We wish to send emails to our users regarding
  • Signup success
  • Forgot password
  • Reset password success
  • Invalid activity

Run

  1. Download and extract the zip file
  2. Open command prompt and browse to the extracted folder
  3. write the command node .
  4. Open Chrome browser and point to http://localhost:8080/

Tutorial

We will be creating a simple AngularJS web application which will send an email to the website administrator when any users wish to contact regarding any issue.
We are going to implement the above functionality in just 3 steps
  1. Craft the user interface (front end client side)
  2. Configure the email client (Sendgrid)
  3. Create the server and make the server talk to Sendgrid and AngularJS frontend (Using NodeJS)

Step-1

Let’s build the user interface using google’s material design and our friend AngularJS
Let’s use the material design CSS library provided by materializecss.com

Step-2

To send email we will use some node libraries
  • nodemailer
  • nodemailer-sendgrid-transport
For this we need to create a sendgrid account and generate a api key and use in index.js

Project Structure

client
– style css
– app.js
– index.html
index.js
package.json
 

index.js (NodeJS)

package.json

index.html

app.js (AngularJS)