Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Wednesday, 4 April 2012

how to find n'th highest value of a column in mysql

SELECT salary FROM tbl_salary ORDER BY salary DESC LIMIT n,1

Where n is the position you want - 1 (i.e. to get the second highest salary it would be LIMIT 1,1)

Friday, 10 February 2012

Copy all files and folder from one directory to another directory PHP

function directory_file_copy($src,$dst) {
    $dir = opendir($src);
    @mkdir($dst);
    while(false !== ( $file = readdir($dir)) ) {
        if (( $file != '.' ) && ( $file != '..' )) {
            if ( is_dir($src . '/' . $file) ) {
                directory_file_copy($src . '/' . $file,$dst . '/' . $file);
            }
            else {
                copy($src . '/' . $file,$dst . '/' . $file);
            }
        }
    }
    closedir($dir);
}

 Example :

$src = 'source_folder';
$dst = 'destination_folder';
directory_file_copy($src,$dst);

Saturday, 21 January 2012

How to get data from xml in php

1. Put this code in php file 

<?php
 
    $objDOM = new DOMDocument();
    $objDOM->load("test.xml"); //make sure path is correct
   
    $employee = $objDOM->getElementsByTagName("employee");
    // for each employee tag, parse the document and get values for
    // name and designation tag.
   
    foreach( $employee as $value )
    {
        $names = $value->getElementsByTagName("name");
        $name  = $names->item(0)->nodeValue;
       
        $designations = $value->getElementsByTagName("designation");
        $designation  = $designations->item(0)->nodeValue;
       
        echo "$name :: $designation <br><br>";
    }
    echo "<u>Another Way using <strong>simplexml_load_file</strong> php function</u><br><br>";
    $xml = simplexml_load_file("test.xml") or die ("Unable to load XML file!");
   
    // access XML data
    foreach($xml as $xml_value)
    {
        $emp_name = $xml_value->name;
        $emp_designation = $xml_value->designation;
        echo "$emp_name => $emp_designation <br><br>";
    }
?>

2. Here is XML file

<?xml version="1.0" encoding="ISO-8859-1"?>
<company>
  <employee>
    <name>Prashant Chaudhari</name>
    <designation>PHP Web Developer</designation>
  </employee>
  <employee>
    <name>Dhirendra Rathod</name>
    <designation>C#.Net Web Developer</designation>
  </employee>
</company>

How to get last 12 months in php

<?php
$months = array();
for($i = 0; $i < 12; $i++){
  $timestamp = strtotime("-$i month");
  $value =  date('n,Y', strtotime("-$i month"));
  $text  =  date('F Y', strtotime("-$i month")); 
  $months[$value] =  $text;
}
echo '<select>';
foreach($months as $value => $text){
    echo '<option value="'.$value.'">'.$text.'</option>';
}
echo '</select>';
?>