Tag

php functions

Browsing

Share Your Website Post To Social Media using php

Share Your Website Post To Social Media using php

Share your website post to social media using php. Everyone want to share their post in social media regularly to get increase the page views and also website view. So the social media is the best way to promote our content to reach everyone. When ever we write our post we directly update this post into our social media using php

Here I am written sharing code, Just click social media icons it will share automatically to your social media timeline

Here I have get our website details description and content using php

<?php $url="https://www.developerdesks.com/";
$name="Share post links articles to social network with Yoursite"; 
$caption="social sharing developer desks"; 
$description="Write some fo the description about your content."; 
$logo="https://www.developerdesks.com/logo.png"; ?>

Here we going to add Facebook and Google plus sharing

<html>
<head>
<title>Share links | Developerdesks</title>
</head>
<body>
<div class="gplus">
<!--google+ sharing --> 
<a href="https://plus.google.com/share?url={<?php echo $url;?>}" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;">
<img width="200" src="google+.png"/> </a> 
</div>
<!--/google+ sharing --> 
<div class=""fb-class">
<!-- fb sharing -->
<a href="#" id="shareonfacebook"><img src="facebook.jpg"></a>
<div class="fb-share-button" data-href="<?php echo $url; ?>" data-layout="button_count">
</div></a>
</div> </body> </html>

Add Javascript for sharing link
Het your facebook api id from the developers facebook page

Find how to get app id – Here

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script> $(document).ready(function () { $('#shareonfacebook').click(function (e) {  var name ='<?php echo $name; ?>'; var caption ='<?php echo $caption; ?>'; var picture ='<?php echo $picture; ?>'; var description ='<?php echo $description; ?>'; var link ='<?php echo $url; ?>';  e.preventDefault(); FB.ui( { method: 'feed', name: name, link: link, caption: caption, picture: picture, description: description, message: '' }); }); });  </script> <script> FB.init({appId: 'yourappid', status: true, cookie: true, xfbml: true}); </script> <div id="fb-root"></div> <script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&appId=yourid&version=v2.0"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script>

Here we added the javascript for post article. now you can used ur post to share in social media

Dynamic dependent dropdown select list using jquery and php

Dynamic dependent dropdown select list using jquery and php

In this tutorial we are going to see how to change states drop down list ans city drop down list option based on the dropdown select country name. we are going use jquery and mysql database for select country and retrieve state data. means Loading records from database dynamically and display it in select box without refreshing the whole page with the help of Ajax and jQuery and PHP code.  country state city Dropdown, using ajax and jqurey fill country state city dropdown.  Ajax country state city dropdown php, onchange in jquery, dynamic dropdown.

Step 1:  Create database and database table With content. Create database name “Country_db” and table with Country, state and city

Country table:

CREATE TABLE IF NOT EXISTS `countries` (
  `country_id` int(11) NOT NULL,
  `country_name` varchar(30) CHARACTER SET utf8 NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '0:Blocked, 1:Active'
) ENGINE=InnoDB AUTO_INCREMENT=240 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Cities Table

CREATE TABLE IF NOT EXISTS `cities` (
  `city_id` int(11) NOT NULL,
  `city_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `state_id` int(11) NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '0:Blocked, 1:Active'
) ENGINE=InnoDB AUTO_INCREMENT=6178 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

States Table

CREATE TABLE IF NOT EXISTS `states` (
  `state_id` int(11) NOT NULL,
  `state_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `country_id` int(11) NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '0:Blocked, 1:Active'
) ENGINE=InnoDB AUTO_INCREMENT=1652 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Step 2: Database Config

<?php
//db details
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'country';

//Connect and select the database
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

if ($db->connect_error) {
    die("Connection failed: " . $db->connect_error);
}
?>

Step 2: Create the index page

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

</head>
<body>
    <div class="select-boxes">
    <?php
    //Include database configuration file
    include('dbConfig.php');
    
    //Get all country data
    $query = $db->query("SELECT * FROM countries WHERE status = 1 ORDER BY country_name ASC");
    
    //Count total number of rows
    $rowCount = $query->num_rows;
    ?>
    <select name="country" id="country">
        <option value="">Select Country</option>
        <?php
        if($rowCount > 0){
            while($row = $query->fetch_assoc()){ 
                echo '<option value="'.$row['country_id'].'">'.$row['country_name'].'</option>';
            }
        }else{
            echo '<option value="">Country not available</option>';
        }
        ?>
    </select>
    
    <select name="state" id="state">
        <option value="">Select country first</option>
    </select>
    
    <select name="city" id="city">
        <option value="">Select state first</option>
    </select>
    </div>
</body>
</html>

Step 3: create jquery with ajax function to pass country id into next page and select state and cities form the database table

<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#country').on('change',function(){
        var countryID = $(this).val();
        if(countryID){
            $.ajax({
                type:'POST',
                url:'ajaxData.php',
                data:'country_id='+countryID,
                success:function(html){
                    $('#state').html(html);
                    $('#city').html('<option value="">Select state first</option>'); 
                }
            }); 
        }else{
            $('#state').html('<option value="">Select country first</option>');
            $('#city').html('<option value="">Select state first</option>'); 
        }
    });
    
    $('#state').on('change',function(){
        var stateID = $(this).val();
        if(stateID){
            $.ajax({
                type:'POST',
                url:'ajaxData.php',
                data:'state_id='+stateID,
                success:function(html){
                    $('#city').html(html);
                }
            }); 
        }else{
            $('#city').html('<option value="">Select state first</option>'); 
        }
    });
});
</script>

Ajax page for select database of cities and states

<?php
//Include database configuration file
include('dbConfig.php');

if(isset($_POST["country_id"]) && !empty($_POST["country_id"])){
    //Get all state data
    $query = $db->query("SELECT * FROM states WHERE country_id = ".$_POST['country_id']." AND status = 1 ORDER BY state_name ASC");
    
    //Count total number of rows
    $rowCount = $query->num_rows;
    
    //Display states list
    if($rowCount > 0){
        echo '<option value="">Select state</option>';
        while($row = $query->fetch_assoc()){ 
            echo '<option value="'.$row['state_id'].'">'.$row['state_name'].'</option>';
        }
    }else{
        echo '<option value="">State not available</option>';
    }
}

if(isset($_POST["state_id"]) && !empty($_POST["state_id"])){
    //Get all city data
    $query = $db->query("SELECT * FROM cities WHERE state_id = ".$_POST['state_id']." AND status = 1 ORDER BY city_name ASC");
    
    //Count total number of rows
    $rowCount = $query->num_rows;
    
    //Display cities list
    if($rowCount > 0){
        echo '<option value="">Select city</option>';
        while($row = $query->fetch_assoc()){ 
            echo '<option value="'.$row['city_id'].'">'.$row['city_name'].'</option>';
        }
    }else{
        echo '<option value="">City not available</option>';
    }
}
?>

That’s it enjoy the code. demo and download the script and use it easily. Feel free to comment your suggestions regarding this tutorial. Subscribe me. 

Simple JavaScript Calculator code with example

Here we going to learn simple javascript calculator code. Generally for beginners, they think its difficult to writing code for calculator. But its not that much difficult in the codding.
We just know only simple things in function like add, sub, mutli and div functions in php. If you know that then the calci code is very simple, you may try it now
javascript calculator code

Step 1:
First create html file

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Simple JavaScript Calculator</title>

</head>
 
<body>
<h2>Simple JavaScript Calculator</h2>
 
<form name="calcForm">
<table>
<tr>
  <td colspan="4"><input type="text" name="display"
                         style="text-align:right"></td>
</tr>
<tr>
  <td><input type="button" name="btn1" value="1"
             onclick="calcForm.display.value += '1'"></td>
  <td><input type="button" name="btn2" value="2"
             onclick="calcForm.display.value += '2'"></td>
  <td><input type="button" name="btn3" value="3"
             onclick="calcForm.display.value += '3'"></td>
  <td><input type="button" name="btnAdd" value="+"
             onclick="calcForm.display.value += ' + '"></td>
</tr>
<tr>
  <td><input type="button" name="btn4" value="4"
             onclick="calcForm.display.value += '4'"></td>
  <td><input type="button" name="btn5" value="5"
             onclick="calcForm.display.value += '5'"></td>
  <td><input type="button" name="btn6" value="6"
             onclick="calcForm.display.value += '6'"></td>
  <td><input type="button" name="btnSub" value="-"
             onclick="calcForm.display.value += ' - '"></td>
</tr>
<tr>
  <td><input type="button" name="btn7" value="7"
             onclick="calcForm.display.value += '7'"></td>
  <td><input type="button" name="btn8" value="8"
             onclick="calcForm.display.value += '8'"></td>
  <td><input type="button" name="btn9" value="9"
             onclick="calcForm.display.value += '9'"></td>
  <td><input type="button" name="btnMul" value="x"
             onclick="calcForm.display.value += ' * '"></td>
</tr>
<tr>
  <td><input type="button" name="btnClear"
             value="C" onclick="calcForm.display.value = ''"></td>
  <td><input type="button" name="btn0" value="0"
             onclick="calcForm.display.value += '0'"></td>
  <td><input type="button" name="btnEqual" value="="
             onclick="calcForm.display.value = eval(calcForm.display.value)"></td>
  <td><input type="button" name="btnDiv" value="/"
             onclick="calcForm.display.value += ' / '"></td>
</tr>
</table>
</form>
 
</body>
</html>

Step 2: Add Css file for the page

input {
       font-family: consola, monospace;
       color: blue;
     }
     td {
       margin-left: auto;
       margin-right: auto;
       text-align: center;
     }
     table {
       border: thick solid;
     }

Step 3: Add javascript for the calci function.

window.onload = initClock;
 
function initClock() {
  var now = new Date();
  var hr  = now.getHours();
  var min = now.getMinutes();
  var sec = now.getSeconds();
  if (min < 10) min = "0" + min;  // insert a leading zero
  if (sec < 10) sec = "0" + sec;
  document.getElementById('clockDisplay').innerHTML
        = "Time is " + hr + ":" + min + ":" + sec;
  setTimeout('initClock()', 500);
}

Here the Demo –

See the Pen KpvNZR by Rajaseka G (@SekarG) on CodePen.

How to search the key values in the array – array_search

If we have a very large array values in Indexed or Associative array, we have to search or filter the particular array value or key.

Array_Search function is used to find the value.

This  function used to  search an array for a value and returns the key.

<?php
$search =array('a'=>'php','b'=>'css','c'=>'html,'d'=>'dssd','e'=>'eeee',f=>'fdfd','g'=>'dsfsd',....);
echo array_search('css',$search);
?>

 

In the Given example, we have to find whether  “CSS” Find or not in the array. It prints the key of the value.

Output

b   // output

Explaination

array_search is used to search the array value(‘css’) and returns the value’s key(‘b’);

2. In_array()

In_array is simillar to the array_search

In_array Functions is used to find the value in the array list

using this function you can find the value is present or not in array queue.

syntax: In_array();

you can search anything value or key using IN_ARRAY();

for example

have to find “Developerdesks” in the given array

<?php

$var = array(‘raj’,’sekar’,’developer’,’desks’,'php','html','css','developerdesks','designer');

if(in_array('developerdesks', $var, ))
{
echo "found Developerdesks";
}
else
{
echo "not found";

}

?>

 

You can see explanation in this url

 

Function to find particular value or key in the array for php – In_array functions

Function to find particular value or key in the array for php – In_array

In_array Functions is used to find the value in the array list

using this function you can find the value is present or not in array queue.

syntax: In_array();

for example

have to find “Developerdesks” in the given array

<?php

$var = array(‘raj’,’sekar’,’developer’,’desks’,'php','html','css','developerdesks','designer');

if(in_array('developerdesks', $var, ))
{
echo "found Developerdesks";
}
else
{
echo "not found";

}

?>

Explanation:

If you want find a particular key or value in the very long array for any action..

<?php

$var = array(‘raj'=>'1233','sekar'=>'434','developer'=>'asd','desks'=>'ded','developerdesks'=>'free','designer'=>'dsa');

?>

here ‘raj’ is ‘key’ and  ‘1233’ is ‘value’

you can search anything value or key using IN_ARRAY();

Similar to In_array();

2. array_search() 

If we have a very large array values in Indexed or Associative array, we have to search or filter the particular array value or key.

Array_Search function is used to find the value.

This  function used to  search an array for a value and returns the key.

You can see Explanation Here

How can we print array with in array using for-each loop – Three dimensional array

A multidimensional array is an array containing one or more arrays.

Three-Dimensional array

For three dimensional array u have to use two for loop condition
find 0 indexed array value and then 0 indexed array value

<?php

$developedesks = array(
"Skills" => array
(
"php" => 80,
"css" => 85,
"html" => 80
),
"design" => array
(
"photoshop" => 70,
"flash" => 75,
"indesign" => 72
),
"framework" => array
(
"wordpress" => 85,
"drupal" => 75,
"joomla" => 70
)
);

$count = count($developedesks);
echo "$count<br/>";
foreach($developedesks  as $develop => $desks)
{

foreach($desks as $key =>$value)
{
echo "$develop &nbsp; $key &nbsp; $value<br/>";
}

}

?>

Explain

First foreach consider skill as $develop and php =>80 as $desks

Second For each consider $key as php and $ value as 80

so the $develop –> indexed as skill, $key–> indexed as php and $value indexed as 80

simple foreach concept worked

Output

Skills  php  80
Skills  css    85
Skills  html   80
design  photoshop 70
design  flash    75
design  indesign   72
framework wordpress 85
framework drupal   75
framework joomla   70

 

Similar like three dimensional array

Multidimensional Array concept in Php

A multidimensional array is an array containing one or more arrays.

Its is solution for to know how to handle a array with in array
Example
1.Two-dimensional array
2.Three-dimensional array

Here You can see Exaplaination

How We used Multidimensional Array concept in Php?

Multidimensional Array concept in Php

A multidimensional array is an array containing one or more arrays.

Its is solution for to know how to handle a array with in array
Example
1.Two-dimensional array
2.Three-dimensional array

1.Two-dimensional array

<?php

$develop= array(
array("developer",php,html),
array("desks",php5,html5),
array("raja",css,ajax),
array("sekar",jquery,drupal)
);

$count= count($develop);
echo "$count";

 for ($row = 0; $row < $carss; $row++) {
echo "<p><b>Level $row</b></p>";
echo "<ul>"; 
for ($col = 0; $col < 3; $col++) {
echo "<li>".$cars[$row][$col]."</li>";
}
echo "</ul>";
}

?>

Output

Indexed Level 0
developer
php
html

Indexed Level 1
desks
php5
html5

Indexed Level 2
raja
css
ajax

Indexed Level 3
sekar
jquery
drupal

Explanation:

First For loop consider all array values inside the array  –> array(“developer”,php,html) as 0 indexed

Second For loop consider inside values –> Developer as 0 indexed

For two dimensional array u have to use two for loop condition

find 0 indexed array value and then 0 indexed array value

simple example

<?php

echo $develop[0][0].": langauge 1: ".$develop[0][1].", language 2: ".$develop[0][2].".";
echo $develop[1][0].": langauge 1: ".$develop[1][1].", langauge 2: ".$develop[1][2].".";
echo $develop[2][0].": langauge 1: ".$develop[2][1].", langauge 2: ".$develop[2][2].".";
echo $develop[3][0].": langauge 1: ".$develop[3][1].", langauge 2: ".$develop[3][2].".";

?>

Example For Three-Dimensional array

How to print array values in php using mysql – associative array

Associative array is array that use named keys that you assign to them.

example

<?php

$var2 = array('raj'='123','sekar'=>'213','developer'=>'545','desks'=>'345');

echo $var2['raj']; // output is 123
echo $var2['sekar']; // output is 213
echo $var2['developer']; // output is 545
echo $var2['desks']; // output is 345

?>

named key ‘raj’ has vlaue ‘123’;

IF the named key is unknown and unlimited the use foreach

<?php

$var3 = array('raj'='123','sekar'='213','developer'='545','desks'='345','php'='34','html'='54654','css'='123'); 

foreach($var3 as $X=>$value)
{
echo $X .$value;
}

?>

output

raj 123
sekar 213
developer
545
desks 345
php 34
html 54654
css 123

Similar like Associative array

Multidimensional Array concept in Php

A multidimensional array is an array containing one or more arrays.

Its is solution for to know how to handle a array with in array
Example
1.Two-dimensional array
2.Three-dimensional array

Here You can see Exaplaination

 

How to use for loop concept for php using find array values

How to use for loop concept for php using find array values

for loop is used to initialize the code for how many times it should run.

syntax

for (initialization counter; total counter; increment counter) {
    code to be executed;
}

for example

<?php 
for ($x = 0; $x <= 5; $x++) {
    echo "The number is: $x <br>";
} 
?>

output

The number is 0

The number is 1

The number is 2

The number is 3

The number is 4

The number is 5

for loop php is used to count and auto increment the array values using loop.

What is Array?

An array is a variable, which can hold more than one value at a time.
php Array basic function defined as
array();

example

< ?php

$design = array('photoshop','flash','dreamviewer','html5');

echo $design[0]; //output is photoshop

echo $design[1]; //output is flash

?>

For more explanation about array

How to print array values in php using find i value – Indexed array

How to print array values indexing in php using Indexed array

Indexed array is array with a numeric index. Index will Start with ‘0’ and it can be assigned automatically

example

< ?php

$design = array(‘photoshop’,’flash’,’dreamviewer’,’html5′);

echo $design[0]; //output is photoshop

echo $design[1]; //output is flash

?>

If you know the total number of array values means you can do like this,

For unknown and unlimited array values are present then go to Forloop

Example

< ?php
$var1 = array('one','122','122','122','dfd','dfgdf','dfgdfg','dfgdfg','dfgdfgf','dfgdfg','dfgdfgdf','gfdgdfg','dfgdfgdf','fdg','dfgdf','last');

$total = count($var1); // Use Count(); functions

for($i=0; $i< $total; $i++)
{
echo "$var1[$i]
";
}
?>

To know the array total values use count(); Functions

 2. Count();

This Function is used to fin the count of array key in the array.

3. Associative array()

Associative array is array that use named keys that you assign to them.

example

<?php

$var2 = array('raj'='123','sekar'=>'213','developer'=>'545','desks'=>'345');

echo $var2['raj']; // output is 123
echo $var2['sekar']; // output is 213
echo $var2['developer']; // output is 545
echo $var2['desks']; // output is 345

?>

For more Explanation click here