How to do radius search in PostgreSQL

Standard

Add this line in HTML

window.navigator.geolocation.getCurrentPosition(function(pos) { console.log(pos); var lat = pos.coords.latitude; var lon = pos.coords.longitude; })

For Angular – Use below lines.

ngOnInit() {
   this.getLocation();
}


getLocation() {
        if (window.navigator.geolocation) {
            window.navigator.geolocation.getCurrentPosition((position: Position) => {
                if (position) {
                    this.lat = position.coords.latitude;
                    this.lng = position.coords.longitude;
                }
            },
                (error: PositionError) => console.log(error));
        } else {
            alert("Geolocation is not supported by this browser.");
        }
    }

In Postgres SQL Query-

SELECT user_id,name
FROM accounts where status=1 and earth_distance(
ll_to_earth(latitude, longitude),
ll_to_earth(22.5738752,88.3720192)
) > 4000; // result will return in KM by default it’s give in Meters

Or

SELECT user_id, earth_distance(ll_to_earth('22.5738752','88.3720192'), ll_to_earth(latitude, longitude))/1000 as distance FROM users order by distance

How to add external library in Angular on the fly in component

Standard

Add this line on top in the Controller

import { Component, OnInit, OnDestroy, Renderer2, ElementRef } from '@angular/core';
ngOnInit() {
this.addJsToElement('http://geodata.solutions/includes/countrystatecity.js').onload = () => {
            console.log('geodata connected');
        }
}



addJsToElement(src: string): HTMLScriptElement {
        const script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = src;
        this.renderer.appendChild(document.body, script);
        return script;
    }

How to install MongoDB with Docker in Ubuntu

Standard

I assume that you have installed Docker already on your machine.

Following the below steps:-

Step-1. Create Compose file, docker-compose.yml

version: '3.7'
services:
  mongodb_container:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: rootpassword
    ports:
      - 27017:27017
    volumes:
      - mongodb_data_container:/data/db

volumes:
  mongodb_data_container:

Step-2. Run it

sudo docker-compose up -d

Above command will start the services on detach mode (similar like running in the background).

If everything OK then our MongoDB server will start

How to check MongoDB container is running or not?

Type this command.

sudo docker ps

To display all the containers pulled, use the following command:

sudo docker ps -a

Looks good and our MongoDB container is running well.

How to install Apache HBase with DOCKER in Ubuntu

Standard

I assume that you have installed Docker already on your machine.

Following the below steps:-

Step-1. Create Compose file, docker-compose.yml

version: '2'

services:
  hbase-master:
    image: blueskyareahm/hbase-base:2.1.3
    command: master
    ports:
      - 16000:16000
      - 16010:16010

  hbase-regionserver:
    image: blueskyareahm/hbase-base:2.1.3
    command: regionserver
    ports:
      - 16030:16030
      - 16201:16201
      - 16301:16301

  zookeeper:
    image: blueskyareahm/hbase-zookeeper:3.4.13
    ports:
      - 2181:2181

Step-2. Build docker container and start

Run the following command in the directory where docker-compose.yml is located.

sudo docker-compose up --build -d

Above command will start the services on detach mode (similar like running in the background).

the docker image specified in docker-compose.yml has been downloaded locally.

You can also see that containers are started for each of hbase-masterhbase-regionserver and zookeeper

Type this command.

sudo docker images
sudo docker ps

To display all the containers pulled, use the following command:

sudo docker ps -a

Looks good and our MongoDB container is running well.

Start hbase-shell

Start hbase-shell with the following command.
Starting with the hbase-shell command in the hbase-master container.

sudo docker-compose exec hbase-master hbase shell


sudo docker-compose exec hbase-master hbase rest start -p 16001

This above command for Hbase connection from backend.


-To see all docker containers
sudo docker ps -a

-To delete docker container
sudo docker rm CONTAINER ID

-To see all docker images
sudo docker images

-To delete docker image
sudo docker rmi IMAGE ID

How to Integrate PayPal Standard Payment Gateway in PHP

Standard

 

PayPal Standard Payment Gateway is the quickest way to accept payment online. The buyer can make the payment from the website to purchase an item online.

1.) Create HTML form

Config file PHP

<?php 
// PayPal configuration 
define('PAYPAL_ID', 'Insert_PayPal_Business_Email'); 
define('PAYPAL_SANDBOX', TRUE); //TRUE or FALSE 
 
define('PAYPAL_RETURN_URL', 'http://www.example.com/success.php'); 
define('PAYPAL_CANCEL_URL', 'http://www.example.com/cancel.php'); 
define('PAYPAL_NOTIFY_URL', 'http://www.example.com/ipn.php'); 
define('PAYPAL_CURRENCY', 'USD'); 
 
// Change not required 
define('PAYPAL_URL', (PAYPAL_SANDBOX == true)?"https://www.sandbox.paypal.com/cgi-bin/webscr":"https://www.paypal.com/cgi-bin/webscr");
<div class="container">
    <?php 
        // Fetch products from the database 
        $results = $db->query("SELECT * FROM products WHERE status = 1"); 
        while($row = $results->fetch_assoc()){ 
    ?>
        <div class="pro-box">
            <img src="images/<?php echo $row['image']; ?>"/>
            <div class="body">
                <h5><?php echo $row['name']; ?></h5>
                <h6>Price: <?php echo '$'.$row['price'].' '.PAYPAL_CURRENCY; ?></h6>
				
                <!-- PayPal payment form for displaying the buy button -->
                <form action="<?php echo PAYPAL_URL; ?>" method="post">
                    <!-- Identify your business so that you can collect the payments. -->
                    <input type="hidden" name="business" value="<?php echo PAYPAL_ID; ?>">
					
                    <!-- Specify a Buy Now button. -->
                    <input type="hidden" name="cmd" value="_xclick">
					
                    <!-- Specify details about the item that buyers will purchase. -->
                    <input type="hidden" name="item_name" value="<?php echo $row['name']; ?>">
                    <input type="hidden" name="item_number" value="<?php echo $row['id']; ?>">
                    <input type="hidden" name="amount" value="<?php echo $row['price']; ?>">
                    <input type="hidden" name="currency_code" value="<?php echo PAYPAL_CURRENCY; ?>">
					
                    <!-- Specify URLs -->
                    <input type="hidden" name="return" value="<?php echo PAYPAL_RETURN_URL; ?>">
                    <input type="hidden" name="cancel_return" value="<?php echo PAYPAL_CANCEL_URL; ?>">
					
                    <!-- Display the payment button. -->
                    <input type="image" name="submit" border="0" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif">
                </form>
            </div>
        </div>
    <?php } ?>
</div>

 

Continue reading

How to integrate Stripe Payment Gateway in PHP

Standard

1.) Create HTML form

<h1>Charge $55 with Stripe</h1>

<!-- display errors returned by createToken -->
<span class="payment-errors"></span>

<!-- stripe payment form -->
<form action="submit.php" method="POST" id="paymentFrm">
    <p>
        <label>Name</label>
        <input type="text" name="name" size="50" />
    </p>
    <p>
        <label>Email</label>
        <input type="text" name="email" size="50" />
    </p>
    <p>
        <label>Card Number</label>
        <input type="text" name="card_num" size="20" autocomplete="off" class="card-number" />
    </p>
    <p>
        <label>CVC</label>
        <input type="text" name="cvc" size="4" autocomplete="off" class="card-cvc" />
    </p>
    <p>
        <label>Expiration (MM/YYYY)</label>
        <input type="text" name="exp_month" size="2" class="card-expiry-month"/>
        <span> / </span>
        <input type="text" name="exp_year" size="4" class="card-expiry-year"/>
    </p>
    <button type="submit" id="payBtn">Submit Payment</button>
</form>

 

Continue reading

How to Add Ajax Call with sweet alert on confirmation event

Standard

For a quick example i can show you how i did it on my site. I put the Ajax call inside the sweet alert.

This is my code used in my site.

<script>
    function blog_delete() {
        var token = $(this).data('id');
        swal("You will not be able to recover this data once you delete!", {
            buttons: {
                cancel: "Cancel",
                catch: {
                    text: "Delete",
                    value: "catch",
                },
                closeModal: false,
            },
        }).then((value) => {
            switch (value) {
                case "catch":
                    $.ajax({
                        url: "/delete-sub-admin",
                        type: "POST",
                        data: {
                            "_token": "<?php echo csrf_token(); ?>",
                            "idToken": token
                        },
                        dataType: 'JSON',
                        success: function(data) {
                            switch (data.status) {
                                case 'success':
                                    $("#tr_" + slug).hide();
                                    swal("Success!", data.message, "success");
                                    break;
                                default:
                                    swal("Error!", data.message, "error");
                                    break;
                            }
                        },
                        error: function(data) {
                            swal("Error!", data.message, "error");
                        }
                    });
                    break;
            }
        });
    }
</script>

How to JQuery validator plugin extended for custom validation

Standard

Add below lines in your page, This is required.

https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js
https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js
<script>
    $.extend(jQuery.validator.messages, {

        maxlength: jQuery.validator.format("Please enter no more than {0} digits."),
        minlength: jQuery.validator.format("Please enter at least {0} digits.")

    });

    $.validator.addMethod("checkemail", function(value, element) {
        var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        return this.optional(element) || re.test(value);
    }, "Invalid email address");

    $("#regForm").validate({
        rules: {
            firstName: {
                required: true,
                lettersonly: true,
                minlength: 1,
                maxlength: 30
            },
            lastName: {
                required: true,
                lettersonly: true,
                minlength: 2,
                maxlength: 30
            },
            email: {
                required: true,
                checkemail: true,
                email: true
            },
            contactNo: {
                digits: true,
                minlength: 10,
                maxlength: 15
            }
        }
    });
</script>

How to Send FormData from AJAX

Standard

JQuery + Ajax

$(“form#frm”).submit(function(event){
event.preventDefault();

var formData = new FormData($(this)[0]);
$.ajax({ url:’index.php?ajax’,processData: false,contentType: false,type: “POST”,data:formData,success:function(res){
console.log(res);
}});

return false;
});