Tuesday, 14 August 2012

Send Invitation request to facebook friends from our site

<div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js"></script>
    
    <input type="button"
      onclick="sendRequestViaMultiFriendSelector(); return false;"
      value="Send Request to Many Users"
    />
    </p>
    
    <script>
      FB.init({
        appId  : 'YOUR_APP_ID',
        frictionlessRequests: true,
      });

      function sendRequestViaMultiFriendSelector() {
        FB.ui({method: 'apprequests',
          message: 'My Great Request'
        }, requestCallback);
      }
      
      function requestCallback(response) {
        // Handle callback here
      }
    </script>

Thursday, 9 August 2012

How to read csv file

<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            if($data[$c] !='')
                echo trim($data[$c]) . "<br />\n";
        }
    }
    fclose($handle);
}
?>

How to download file in php

<?php
// your file to upload
$file = 'text.txt'; // your file destination path
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: application/txt"); // change content-type according to your file to be download
// tell file size
header('Content-length: '.filesize($file));
// set file name
header('Content-disposition: attachment; filename='.basename($file));
readfile($file);
// Exit script. So that no useless data is output-ed.
exit;
?>

Arrange ul li in tabular format

<?php
$i = 1;
?>
<ul>
<?php
while($i <= 10)
{
    ?><li><?php echo $i;?></li>
    <?php
    if($i%3 == 0)
    {
        ?></ul><ul>
        <?php   
    }
   
    $i++;
}
?>
</ul>

Wednesday, 8 August 2012

How to write data to xml file

<?php
  $employees = array();
  $employees [] = array(
  'name' => 'Albert',
  'age' => '34',
  'salary' => "$10000"
  );
  $employees [] = array(
  'name' => 'Claud',
  'age' => '20',
  'salary' => "$2000"
  );
 
  $doc = new DOMDocument();
  $doc->formatOutput = true;
 
  $r = $doc->createElement( "employees" );
  $doc->appendChild( $r );
 
  foreach( $employees as $employee )
  {
  $b = $doc->createElement( "employee" );
 
  $name = $doc->createElement( "name" );
  $name->appendChild(
  $doc->createTextNode( $employee['name'] )
  );
  $b->appendChild( $name );
 
  $age = $doc->createElement( "age" );
  $age->appendChild(
  $doc->createTextNode( $employee['age'] )
  );
  $b->appendChild( $age );
 
  $salary = $doc->createElement( "salary" );
  $salary->appendChild(
  $doc->createTextNode( $employee['salary'] )
  );
  $b->appendChild( $salary );
 
  $r->appendChild( $b );
  }
 
  echo $doc->saveXML();
  $doc->save("write.xml")
  ?>

Copy everything from one directory to another directory in php

<?php
$src = 'srcdir';
$dst = 'dstdir';
recurse_copy($src,$dst);
function recurse_copy($src,$dst) {
    $dir = opendir($src) or die('error');
    @mkdir($dst);
    while(false !== ( $file = readdir($dir)) ) {
        if (( $file != '.' ) && ( $file != '..' )) {
            if ( is_dir($src . '/' . $file) ) {
                recurse_copy($src . '/' . $file,$dst . '/' . $file);
            }
            else {
                copy($src . '/' . $file,$dst . '/' . $file);
            }
        }
    }
    closedir($dir);
}
?>

How to print last twelve months in dropdown

<?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;
}
print '<select>';
foreach($months as $value => $text){
    print '<option value="'.$value.'">'.$text.'</option>';
}
print '</select>';
?>