Sunday 15 January 2017

Array_merge and Array_combine in php

Array Combine

array_combine() is used to creates a new array by using the key of one array as keys and using the value of other array as values.One thing to keep in mind while using array_combine() that number of values in both arrays must be same.

<?php
$array1    = array("subject1","subject2","subject3");
$array2    = array("php","html","css");
$new_array = array_combine($array1, $array2);
print_r($new_array);
?>

Result
Array Combine Array ( [subject1] => php [subject2] => html [subject3] => css )

<br>

Array Merge
array_merge merges one or more than one array such that the value of one array appended at the end of first array. If the arrays have same strings  key  then the later value overrides the previous value for that key .

<?php
$array1 = array("one" => "java","two" => "sql");
$array2 = array("one" => "php","three" => "html","four"=>"Me");
$result = array_merge($array1, $array2);
print_r($result);?>

Result
Array Merge Array ( [one] => php [two] => sql [three] => html [four] => Me )

Thursday 12 January 2017

Learn How To Create Wordpress Plugin and their Shortcodes

Create Shortcodes in WordPress Plugin
WordPress offers a predefined shortcode function to create shortcode in WordPress plugin. For using shortcode function, you have to define a handler function that parse the shortcode and return some output. Then, you need to register a shortcode using add_shortcode() function.
add_shortcode( $shortcode_name, $handler_function);
·         $shortcode_name – (required, string) It is a string that to be searched in the post.
·         $handler_function – (required, callable) It is a hook to run when shortcode is found.

WordPress ShortCode Example 1 – Display Form On Pages/Posts

You can create shortcode to display form on any page or post of the website. The below code includes-
·         Plugin details
·         form_creation() to create a form which includes form fields.
·         add_shortcode() function which contain shortcode name test and calling of form_creation() function as parameters.

<?php
/*
* Plugin Name: WordPress ShortCode
* Description: Create your WordPress shortcode.
* Version: 1.0
* Author: InkThemes
* Author URI: https://inkthemes.com
*/

// Example 1 : WP Shortcode to display form on any page or post.
function form_creation(){
?>
<form>
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br>
Message: <textarea name="message"> Enter text here...</textarea>
</form>]
<?php
}
add_shortcode('test', 'form_creation');
?>

WordPress Shortcode Example 2 – Share Pages/Posts On Twitter

You can create shortcode to share your pages or posts on Twitter. The below code includes-
·         Plugin details
·         ink_wp_shortcode() get the particular post or page that you want to share on Twitter.
·         add_shortcode() function contain shortcode name twitter and calling of ink_wp_shortcode() function as parameters.
 
 
<?php
/*
* Plugin Name: WordPress ShortCode
* Description: Create your WordPress shortcode.
* Version: 1.0
* Author: InkThemes
* Author URI: https://inkthemes.com
*/
 
// Example 3 : WP Shortcode to share post or page on Twitter.
function ink_wp_shortcode($atts, $content=null)
{
$post_url = get_permalink($post->ID);
$post_title = get_the_title($post->ID);
$tweet = '<a style="color:blue; font-size: 20px;" href="http://twitter.com/home/?status=Read' . $post_title . 'at' . $post_url . '">
<b>Share on Twitter </b></a>';
return $tweet;
}
add_shortcode('twitter', 'ink_wp_shortcode');
?>


Tuesday 3 January 2017

How to create customize google map with custom image marker, icon and multiple infowindow.

var markers = [
 {
        "title": 'SPAIN',
        "lat": '39.90973623',
        "lng": '-2.63671875',
        "description": 'Spain,(US Embassy, Puerto Banus, Youth Council Spain, British International School of Marbella)'
    },
];
    window.onload = function () {
        LoadMap();
    }
    function LoadMap() {
  var mapOptions = {
            center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
            zoom: 2,
            scrollwheel: false,
     minZoom: 2,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        
        var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
        var iconBase = 'http://example.com/';
        var infoWindow = new google.maps.InfoWindow();

        for (var i = 0; i < markers.length; i++) {
            var data = markers[i];
            var myLatlng = new google.maps.LatLng(data.lat, data.lng);
            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: data.title,
                icon: iconBase + 'image.png'
            });

            //Attach click event to the marker.
            (function (marker, data) {
                google.maps.event.addListener(marker, "click", function (e) {
                    //Wrap the content inside an HTML DIV in order to set height and width of InfoWindow.
                    infoWindow.setContent("<div style = 'width:200px;min-height:40px'>" + data.description + "</div>");
                    infoWindow.open(map, marker);
                });
            })(marker, data);
        }
    }

---------------------------------------------------------------------------------
<div id="dvMap" style="width: 1230px; height: 450px;"></div>

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

PHP script for printing first 20 Fibonacci numbers

<?php
$count = 0 ;
$f1 = 0;
$f2 = 1;
echo $f1." , ";
echo $f2." , ";
while ($count < 20 )
{
$f3 = $f2 + $f1 ;
echo $f3." , ";
$f1 = $f2 ;
$f2 = $f3 ;
$count = $count + 1;
}
?>
Its output will be 
0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377 , 610 , 987 , 1597 , 2584 , 4181 , 6765 , 10946 

Swap two variables value without using third variable in php

<?php
$a=5;
$b=6;

 // This method will work only for numbers:
$a =  $a + $b;  // 5 + 6 = 11
$b = $a - $b;   // 11 - 6 = 5
$a = $a - $b;  // 11 - 5 = 6
echo $a . ',' . $b;

 // This method will work for any variable type:

$a = 5;
$b = 6;
list($a, $b) = array($b, $a);
echo $a . ',' . $b;
?>