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)

Tuesday, 13 March 2012

Simple pagination with Php and MySql

$max = 2; //amount of records per page. change to what to want

$p = $_GET['p'];

if(empty($p))

{

$p = 1;

}

$limits = ($p - 1) * $max;

//view the news article!

//view all the pagess in rows

$sql = mysql_query("SELECT * FROM table LIMIT ".$limits.",$max") or die(mysql_error());

//the total rows in the table

$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM table"),0);   

//the total number of pages (calculated result), math stuff...

$totalpages = ceil($totalres / $max);

//the table

echo "<table><tr><td>Title</td><td>Author</td></tr>";

while($r = mysql_fetch_array($sql))

{

    $title = $r['title'];

   $author = $r['author'];

   echo "<tr><td>$title</td><td>$author</td></tr>";

}

//close up the table

echo "</table>";

for($i = 1; $i <= $totalpages; $i++)
{

//this is the pagination link

echo "<a href='news.php?p=$i'>$i</a>|";

}

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>';
?>

Wednesday, 14 December 2011

Find the occurace of substr in given string in mysql select query

1 
syntax : LOCATE(substr,str)  
example :SELECT LOCATE('bar', 'foobarbar');
it return 4 
 
2
syntax : FIND_IN_SET(str,strlist)
example : SELECT FIND_IN_SET('b','a,b,c,d');
it return 2 

Tuesday, 15 November 2011

Get current date and time of india according to timezone

$dateTime = new DateTime("now", new DateTimeZone('Asia/Calcutta'));
echo $dateTime->format("Y-m-d H:i:s");