Friday, 11 December 2015

Show clock using PHP

Show Current Time On Webpage Using PHP with Javascript 

Timer Function:-

<script type="application/javascript">
function ajaxcall(){
     $.ajax({
         url: 'getTime.php',
         success: function(data) {
             data = data.split(':');
             $('#hours').html(data[0]);
             $('#minutes').html(data[1]);
             $('#seconds').html(data[2]);
         }
     });
 }



setInterval('ajaxcall()',1000);

</script>




gettime.php

<?php    
date_default_timezone_set('Asia/Calcutta');
echo $date = date('h:i:s A'); 
?>



Friday, 28 August 2015

How to upload large amount of data within seconds into mysql database USING csv file



Using this php script you can upload thousands of  record  within a second

1. HTML form for upload csv file -

<form name="import" method="post" enctype="multipart/form-data">
        <input type="file" name="file" /><br />
        <input type="submit" name="submit" value="Submit" />
    </form>




2. PHP Script FOr Execution -
<?php
    $con = mysql_connect('localhost','root','123456'); //database Connections
if(!$con){die(mysql_error());
}
else
{
mysql_select_db('test',$con);
}

   
    if(isset($_POST["submit"]))
    {
$status=2;       
  move_uploaded_file($_FILES["file"]["tmp_name"],
      "/var/www/html/importviaexcel/upload/" . $_FILES["file"]["name"]);
      $file_path="/var/www/html/importviaexcel/upload/" . $_FILES["file"]["name"]; //file & folder name Where csv uploaded

echo "My file - ".$file_path;
$table="csv";
$fp = file("$file_path");
$total_count=count($fp);
echo "Total record - ".count($fp);
$sql_load =     " LOAD DATA INFILE '".$file_path."'" . 
            "IGNORE INTO TABLE " . $table . 
            " FIELDS TERMINATED BY " . "','" . 
            // " LINES TERMINTED BY " . "\r\n" . 
            " IGNORE 1 LINES
            ( @no,@name,@mobile,@email)
            set
            no = @no,
            name = @name,
            mobile = @mobile,
            email = @email,
            status = '2' 
            ";


$result = mysql_query($sql_load, $con);

$upload_rec=mysql_affected_rows();
$duplicate=$total_count-$upload_rec;
echo "Total - $total_count </br>Upload - $upload_rec </br> duplicate - $duplicate  ";
           

    }
?>




  Hope it will Helpful for you.

Wednesday, 15 July 2015

Remove file extension from url using htaccess ?

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

How to get Duplicate Record From Mysql Database ?

Use this query to get duplicate record-

SELECT mobile, count(id) as total_count FROM servicecall
GROUP BY mobile HAVING total_count > 1

Wednesday, 8 July 2015

How to set multiple google maps on single web page using javascript ?

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


<script type="text/javascript">
// Enable the visual refresh
google.maps.visualRefresh = true;
// Set the Map variable
var map;
var infowindow = new google.maps.InfoWindow();
function initialize() {
var myOptions = {
zoom: 8,
mapTypeControl: true,
scrollwheel: true,
streetViewControl : true,
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles:[
{ featureType: "water", stylers: [ { color: "#8DC4E1", hue: 0.1 } ] },
{ featureType: "landscape.natural", stylers: [ { color: "#F4F3EF", lightness: +100 } ] },
{ featureType: "road.local", stylers: [ { color: "#FFF" } ] }
],
};
var all = [
["4605 S Tamiami Trail, Sarasota, FL 34231, USA", "27.287359", "-82.530634"],
["8519 state road 70 east bradenton fl 34211", "27.439685", "-82.452375"],
];
var infoWindow = new google.maps.InfoWindow;
map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
map1 = new google.maps.Map(document.getElementById('map_canvas1'), myOptions);
// Set the center of the map
var pos = new google.maps.LatLng(27.287359, -82.530634);
var pos1 = new google.maps.LatLng(27.439685, -82.452375);
map.setCenter(pos);
map1.setCenter(pos1);
function closeinfocallback(infowindow)
{
return infowindow.close();
}
function infoCallback(infowindow, marker,map)
{
return function() {
infowindow.open(map, marker);
};
}
function setMarkers(map, all,i)
{

var name = all[i][0];
var lat = all[i][1];
var lng = all[i][2];
var latlngset;
latlngset = new google.maps.LatLng(lat, lng);
var marker = new google.maps.Marker({
position: latlngset,
map: map,
});
var content = '<div class="map-content"><h3>' + name + '</h3></div>';
var infowindow = new google.maps.InfoWindow();
infowindow.setContent(content);
google.maps.event.addListener(marker, 'click', infoCallback(infowindow, marker,map));

}
// Set all markers in the all variable
setMarkers(map, all,'0');
setMarkers(map1, all,'1');
}
// Initializes the Google Map
google.maps.event.addDomListener(window, 'load', initialize);
</script>




<div id="map_canvas1" style="height: 300px; width: 100%;">  </div>

<div id="map_canvas" style="height: 300px; width: 100%;">  </div>

Tuesday, 7 July 2015

How to send mail using PHP ?

You can use this Script to send mail !

$headers = "From: Company name<info@domainname.com>\r\n" .
"Reply-to: Customer<info@domainname.com>\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=utf-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n\r\n";
$subject="Service Invoice";

$to ="amit******@gmail.com";
mail($to,"Subject","Test Mail" ,$headers);

Saturday, 4 July 2015

What is SSL ?

Secure Sockets Layer (SSL). cryptographic protocols which provide secure communications on the Internet.


How we can increase the execution time of a PHP script ?


I have given you some function using them you can increase the execution time of a PHP script.Default time for execution of a PHP script is 30 seconds.These are -

1.set_time_limit()//change execution time temporarily.

2.ini_set()//change execution time temporarily.

3. By modifying `max_execution_time' value in PHPconfiguration(php.ini) file.//change execution time permanent.

Tuesday, 30 June 2015

How to check cURL enabled or disabled ?

You can use this script to get CURL status..

<?php
echo 'Curl: ', function_exists('curl_version') ? 'Enabled' : 'Disabled';
?>

What is cURL and how use ?

cURL is a library that lets you make HTTP requests in PHP. Everything you need to know about it (and most other extensions) can be found in the PHP manual.


How to use :-

The first Curl script we are going to look at is the simplest Curl script that is actually useful - it will load a web page, retrieve the contents, then print it out. So, keeping the four-step Curl process in mind, this equates to:

Initialise Curl

Set URL we want to load

Retrieve and print the URL

Close Curl

Here is how that looks in PHP code:

<?php
    $curl = curl_init();
    curl_setopt ($curl, CURLOPT_URL, "http://www.php.net");
    curl_exec ($curl);
    curl_close ($curl);
?> 

How To make sub-string in PHP ?

string substr ( string $string , int $start [, int $length ] )

Returns the portion of string specified by the start and length parameters.

<?php
echo substr('abcdef', 1);     // bcdef
echo substr('abcdef', 1, 3);  // bcd
echo substr('abcdef', 0, 4);  // abcd
echo substr('abcdef', 0, 8);  // abcdef
echo substr('abcdef', -1, 1); // f

?>

Monday, 22 June 2015

PHP Date Function Some days previous

Use the strtotime method provided by PHP.

<?php

echo date('Y-m-d', strtotime('-5 days')); // It shows 5 days previous date

echo date('Y-m-d', strtotime('+5 days')); // It shows 5 days Next date

?>

How To create Constant in PHP ?

Constants are like variables except that once they are defined they cannot be changed or undefined.


<?php

//The example below creates a constant with a case-sensitive name

define("PI", 3.14);

echo PI;


//The example below creates a constant with a case-insensitive name

define("PI", 3.14,true);

echo pi;

?>

What is variable and how to declare in php ?

Variables are "containers" for storing information.

<?php
$str = "Hello world!";
$x = 10;
$y = 15.5;
?>

NOTE : -

the variable $str will hold the value Hello world!
the variable $x will hold the value 10
and the variable $y will hold the value 15.5.




Rules for variable declaration:

A variable starts with the $ sign, followed by the name of the variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive ($age and $AGE are two different variables)

Sunday, 21 June 2015

how to connect to mysql database using pdo in php ?

Connections are established by creating instances of the PDO base class


<?php
try {
$dsn='mysql:host=localhost;dbname=test';
$username='root';
$password='root';

    $con= new PDO($dsn, $username, $password);
 
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

  NOTE : - If there are any connection errors, a PDOException object will be thrown.You may catch the exception if you want to handle the error condition.

PHP Database Connections with MySql

<?php
$con= mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("mysql_database",$con);
echo 'Connected successfully';
mysql_close($con);
?>

localhost at different port like 3307,3308

<?php
$con= mysql_connect('localhost:3308', 'mysql_user', 'mysql_password');
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("mysql_database",$con);
echo 'Connected successfully';
mysql_close($con);
?>

OR

<?php
$con= mysql_connect('127.0.0.1:3308', 'mysql_user', 'mysql_password');
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("mysql_database",$con);
echo 'Connected successfully';
mysql_close($con);
?>

How to get first and last date of current month ?

<?php
$first_day_this_month = date('Y-m-01');
$last_day_this_month  = date('Y-m-t');
echo "First Date Of current Month - ".$first_day_this_month."</br>";
echo "First Date Of current Month - ".$last_day_this_month."</br>";
?>

How To get First Date And Last Date of current Week ?

<?php

if(date('D')!='Mon')
{  
//take the last monday
$staticstart = date('Y-m-d',strtotime('last Monday'));
}else{
$staticstart = date('Y-m-d');  
}
if(date('D')!='Sat')
{
$staticfinish = date('Y-m-d',strtotime('next Saturday'));
}else{
$staticfinish = date('Y-m-d');
}
echo "First Date of Current week - ".$staticstart."</br>";
echo "Last Date of Current week - ".$staticfinish."</br>";

?>

how to get difference between two dates in php

<?php 
$date1="2011-01-02";
$date2=date("Y-m-d");

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);

?>


O/P -  4 years, 5 months, 20 days


Saturday, 20 June 2015

What is PHP

The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications.

"Hello World" Script in PHP

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'?> 
 </body>
</html>