Wednesday, 19 July 2017

How to create an visitor counter in PHP | hit counter

Total page view in the php. This count hit in the page.





 <?php  
 session_start();  
 if(isset($_SESSION['page_view'])){  
   $_SESSION['page_view'] = $_SESSION['page_view']+ 1;  
 }else{  
   $_SESSION['page_view'] = 1;  
 }  
 echo "Total page views = ". $_SESSION['page_view'];  
 ?>  



Click to download here.

Tuesday, 18 July 2017

Data read from excel file in php

This is a example of read data from excel file in php. In this example all
data read from excel file and these data shows in html table.

Screenshot 1: Excel file data





Screenshot 2: Data show as html table





Click to download from here.




<?php  
  include 'excel_reader.php';   // include the class  
 // creates an object instance of the class, and read the excel file data  
 $excel = new PhpExcelReader;  
 $excel->read('demo.xls');  
 //returns a HTML table with excel rows and columns data  
 function sheetData($sheet) {  
  $html = '<table style="border-collapse: collapse;">';   // begin html table  
  $x = 1;  
  while($x <= $sheet['numRows']) {  
   $html .= "<tr>\n";  
   $y = 1;  
   while($y <= $sheet['numCols']) {  
    $cell = isset($sheet['cells'][$x][$y]) ? $sheet['cells'][$x][$y] : '';  
    $html .= "<td style='border: 1px solid black; padding: 0 0.5em;'>$cell</td>\n";  
    $y++;  
   }   
   $html .= "</tr>\n";  
   $x++;  
  }  
  return $html .'</table>';   // ends and returns the html table  
 }  
 $sheets = count($excel->sheets);    // gets the number of sheets  
 $excel_data = '';             // to store the the html tables with data of each sheet  
 // traverses the number of sheets and sets html table with each sheet data in $excel_data  
 for($i=0; $i<$sheets; $i++) {  
  $excel_data .= '<h4>Sheet '. ($i + 1) .' (<em>'. $excel->boundsheets[$i]['name'] .'</em>)</h4>'. sheetData($excel->sheets[$i]) .'<br/>';   
 }  
 ?>  
 <?php  
   echo $excel_data;  // displays tables with excel file data  
 ?>  

Monday, 17 July 2017

How to create and download a csv file in php | export csv file


Export CSV file using fputcsv(). The fputcsv() function accepts an array data. This array converted CSV format and written to the target .csv.


Click to download from here.


<?php  
   // output headers for the file is downloaded rather than displayed  
   header("Content-type: text/csv");  
   header("Content-Disposition: attachment; filename=file.csv");  
   header("Pragma: no-cache");  
   header("Expires: 0");  
   // data for csv file  
   $employees = array(  
     array("Faruk", "Development", "90000"),  
     array("Siyam", "HR", "125000"),  
     array("Jisan", "Marketting", "65000")  
   );  
   // for output stream  
   $file = fopen('php://output', 'w');  
   // for column headings  
   fputcsv($file, array('Name', 'Department', 'Salary'));  
   foreach ($employees as $employee) {  
     //use fputcsv() generate correct csv lines from your array  
     fputcsv($file, $employee);  
   }  
 ?>  

Sunday, 16 July 2017

Change selected option by value on select list with jQuery | jQuery change() Method


This is a good example of jQuery change() method. A area will be appear
when we select drop down value.


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>  
 <script>  
   $(document).ready(function () {  
     $('#gender').on('change', function () {  
       if (this.value == 'male') {  
         $("#male").show();  
         $("#female").hide();  
       }  
       else if (this.value == 'female') {  
         $("#male").hide();  
         $("#female").show();  
       } else {  
         $("#male").hide();  
         $("#female").hide();  
       }  
     });  
   });  
 </script>  
 <p>Change selected option by value on select list with jQuery</p>  
 <select id='gender'>  
   <option value="0">- Select One -</option>  
   <option value="male">Male</option>  
   <option value="female">Female</option>  
 </select>  
 <div style='display:none; margin-top: 100px' id='male'>  
   <label>Male Name</label>  
   <input type='text' class='text' name='male' value size='20' />  
 </div>  
 <div style='display:none; margin-top: 100px' id='female'>  
   <label>Female Name</label>  
   <input type='text' class='text' name='female' value size='20' />  
 </div>  




Click to download from here.

Saturday, 15 July 2017

get location from ip in php

Get location from ip address.

<?php  
 $ip = get_IP_address();  
 $details = json_decode(file_get_contents("http://ipinfo.io/$ip"));  
 print_r($details);  
 echo "City: ". $details->city."<br/>";  
 function get_IP_address()  
 {  
   foreach (array('HTTP_CLIENT_IP',  
          'HTTP_X_FORWARDED_FOR',  
          'HTTP_X_FORWARDED',  
          'HTTP_X_CLUSTER_CLIENT_IP',  
          'HTTP_FORWARDED_FOR',  
          'HTTP_FORWARDED',  
          'REMOTE_ADDR') as $key){  
     if (array_key_exists($key, $_SERVER) === true){  
       foreach (explode(',', $_SERVER[$key]) as $IPaddress){  
         $IPaddress = trim($IPaddress); // Just to be safe  
         if (filter_var($IPaddress,  
             FILTER_VALIDATE_IP,  
             FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)  
           !== false) {  
           return $IPaddress;  
         }  
       }  
     }  
   }  
 }  
 ?>  


Download from this link.  click here

Friday, 14 July 2017

Automatically create set of form fields

Most of time we need to create dynamically set of form fields. In this
example when we click add button then create two new fields. And we
 can remove that group field after click remove button.

Screenshot :







<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>  
 <script type="text/javascript">  
   var rowNum = 0;  
   function addNewFields(frm) {  
     rowNum++;  
     var row = '<p id="rowNum' + rowNum + '"><input type="text" name="first_name[]" value="' + frm.first_name.value + '"><input type="text" name="last_name[]" value="' + frm.last_name.value + '"> <input type="button" value="Remove" onclick="removeRow(' + rowNum + ');"></p>';  
     jQuery('#itemRows').append(row);  
     frm.first_name.value = '';  
     frm.last_name.value = '';  
   }  
   function removeRow(rnum) {  
     jQuery('#rowNum' + rnum).remove();  
   }  
 </script>  
 <form action="" method="post">  
   <div id="itemRows">  
     <input name="first_name" type="text"/>  
     <input name="last_name" type="text"/>  
     <input onclick="addNewFields(this.form);" type="button" value="Add"/>  
   </div>  
 </form>  


Download from this link. Click here to download.

Auto calculate input field in jquery

Calculate auto input fields in this example. When we input some value
that value will calculate and it show result. This will also example for
javascript/jQuery calculator.

<table width="250px" border="1" style="border-collapse:collapse;">  
   <tr>  
     <td>Mango</td>  
     <td><input class="calculator" type="text" name="txt"/></td>  
   </tr>  
   <tr>  
     <td>Apple</td>  
     <td><input class="calculator" type="text" name="txt"/></td>  
   </tr>  
   <tr>  
     <td>Orange</td>  
     <td><input class="calculator" type="text" name="txt"/></td>  
   </tr>  
   <tr>  
     <td>Olive</td>  
     <td><input class="calculator" type="text" name="txt"/></td>  
   </tr>  
   <tr id="summation">  
     <td align="right">Sum :</td>  
     <td align="center"><span id="total">0</span></td>  
   </tr>  
 </table>  
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>  
 <script>  
   $(document).ready(function(){  
     $(".calculator").each(function() {  
       $(this).keyup(function(){  
         calculateSum();  
       });  
     });  
   });  
   function calculateSum() {  
     var sum = 0;  
     //iterate through each text field and add the values  
     $(".calculator").each(function() {  
       if(!isNaN(this.value) && this.value.length!=0) {        //check value is number  
         sum += parseFloat(this.value);  
       }  
     });  
     $("#total").html(sum.toFixed(2));  // show result  
   }  
 </script>  

Wednesday, 12 July 2017

Javascript progress bar

Display status of a determinate or indeterminate process. 
You can use it for show processing levels.

<!-- Progress bar holder -->  
 <div id="progress" style="width:500px;border:1px solid #ccc;"></div>  
 <!-- Progress information -->  
 <div id="information" style="width"></div>  
 <?php  
 $total = 10000;  // Total processes  
 for ($i = 1; $i <= $total; $i++) {  
   $percent = intval($i / $total * 100);  // Calculate the percentation  
   ?>  
   <script>  
     document.getElementById("progress").innerHTML ="<div style='width: <?php echo $percent; ?>%; background-color:#ddd;'>&nbsp;</div>";  
     document.getElementById("information").innerHTML ="<?php echo $i; ?> row(s) processed.";  
   </script>  
   <?php  
   flush();        // Send output to browser immediately  
 }  
 ?>  

Tuesday, 11 July 2017

How to disable cut, copy and paste operation from website text

Sometimes we need to restrict cut, copy, paste for website. Below
example no body can not copy text from this page.

oncopy="return false" oncut="return false" onpaste="return false"  - this
code will be restrict cut, copy and paste
<div oncopy="return false" oncut="return false" onpaste="return false">  
   Lorem ipsum Lorem ipsum orem ipsum Lorem ipsum orem ipsum  
   Lorem ipsum Lorem ipsum orem ipsum Lorem ipsum orem ipsum  
   Lorem ipsum Lorem ipsum orem ipsum Lorem ipsum orem ipsum  
   Lorem ipsum Lorem ipsum orem ipsum Lorem ipsum orem ipsum  
   Lorem ipsum Lorem ipsum orem ipsum Lorem ipsum orem ipsum  
 </div>  
Click download from here

Get specified character or word from a string

The substr() function returns a part of a string.

Syntax: substr(string,start,length) where string and start is required. Length is a optional. This tutorial would help about a lot of string issue.

Example :


<?php  
   echo substr("Life is a beautiful", 9);       //beautiful  
   echo substr("Life is a beautiful", 3);       //e is a beautiful  
   echo substr("Life is a beautiful", -14, 2);       //is  
 ?>  

Monday, 10 July 2017

Secure pay xml api for credit card

Secure pay is a one of famous payment gateway for Australia. You can use it for your site.

For test URL:   https://test.api.securepay.com.au/xmlapi/directentry
For live URL:   https://api.securepay.com.au/xmlapi/directentry

Integration Details:
This is used as part of your transaction messages.
Merchant id:  ABC0001
Transaction password:  abc123

Test Card Number:
Use the following information when testing transactions:
Card Number: 4444333322221111
Card Type:   VISA Card
CCV:    123
Card Expiry: 08 / 23 (or any date greater then today)

Test Login Details (You can check your transaction from your site):
This is used to login to the merchant portal.
URL:    https://test.login.securepay.com.au
Merchant id:   ABC
User name:   test


<?php  
 $xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";  
 $xml.="<SecurePayMessage>";  
 $xml.= "<MessageInfo>";  
 $xml.="<messageID>8af793f9af34bea0cf40f5fb750f64</messageID>";  
 $xml.="<messageTimestamp>20162201115745000000+660</messageTimestamp>";  
 $xml.="<timeoutValue>60</timeoutValue>";  
 $xml.="<apiVersion>xml-4.2</apiVersion>";  
 $xml.="</MessageInfo>";  
 $xml.="<MerchantInfo>";  
 $xml.="<merchantID>ABC0001</merchantID>";  
 $xml.="<password>abc123</password>";  
 $xml.="</MerchantInfo>";  
 $xml.="<RequestType>Payment</RequestType>";  
 $xml.="<Payment>";  
 $xml.="<TxnList count='1'>";  
 $xml.="<Txn ID='1'>";  
 $xml.="<txnType>0</txnType>";  
 $xml.="<txnSource>23</txnSource>";  
 $xml.="<amount>2600</amount>";  
 $xml.="<recurring>1000</recurring>";  
 $xml.="<currency>AUD</currency>";  
 $xml.="<purchaseOrderNo>test</purchaseOrderNo>";  
 $xml.="<CreditCardInfo>";  
 $xml.="<cardNumber>4444333322221111</cardNumber>";  
 $xml.="<cardType>4444333322221111</cardType>";  
 $xml.="<expiryDate> 08/23</expiryDate>";  
 $xml.="<cvv>123</cvv>";  
 $xml.="</CreditCardInfo>";  
 $xml.="</Txn>";  
 $xml.="</TxnList>";  
 $xml.="</Payment>";  
 $xml.="</SecurePayMessage>";  
 ini_set('max_execution_time', 0);  
 $ch = curl_init();  
 curl_setopt( $ch, CURLOPT_URL, 'https://test.api.securepay.com.au/xmlapi/payment');  
 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );  
 curl_setopt( $ch, CURLOPT_POST, true );  
 curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));  
 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );  
 curl_setopt( $ch, CURLOPT_POSTFIELDS, $xml );  
 $result = curl_exec($ch);  
 curl_close($ch);  
 $xml = simplexml_load_string($result);  
 $json = json_encode($xml);  
 $xml_data= json_decode($json,TRUE);  
 print_r($xml_data);   
 ?> 



Click to download this file

Sunday, 9 July 2017

Create log file in php


Log file is more important for developer. It helps to find out bug or value checking for your site.

Process :
Step 1: Create log.php file and copy following code. Then run log.php file.

 <?php  
 $file = 'log.txt';  
 $log_value = 'This text will be save in log.txt file';  
 $current = file_get_contents($file);  // Open the file to get existing content  
 $current .= $log_value . "\n";      // Append a new person to the file  
 file_put_contents($file, $current);  // Write the contents back to the file  
 ?> 


















Step 2: Then check log.txt file






































How to create an visitor counter in PHP | hit counter

Total page view in the php. This count hit in the page. <?php session_start(); if(isset($_SESSION['page_view'])){...