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>  

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'])){...