Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Friday, January 23, 2015

Export data to excel in php

Below is the simple script to exporting your data to Excel file.

You can download below codes from github.

function download_xls() {

  $data = array(
    array("Kali", "Sundar", 28),
    array("Amala", "silk", 18),
    array("Vinoth", "bharath", 31)
  );

  // Form header names.
  $header = array("Firstname", "Lastname", "Age");

  // File name for download.
  $file_name = "Export_data_" . date('Ymd') . ".xls";

  // This option to trigger download widget.
  header("Content-Disposition: attachment; filename=\"$file_name\"");
  header("Content-Type: application/vnd.ms-excel");

  // push header to file.
  echo implode("\t", $header) . "\n";

  foreach($data as $row) {
    // Push each row contents.
    echo implode("\t", $row) . "\n";
  }
  exit;
}

// Phpexcel is a drupal module to do export / create xls files.

Below is the example code to export xls file with out any open file format issue.

 module_load_include('inc', 'phpexcel');

// Create directory my_dir.
  $filepath = 'public://my_dir/';
  file_prepare_directory($filepath, FILE_CREATE_DIRECTORY);

  // Store file.
  $dir = file_stream_wrapper_get_instance_by_uri('public://my_dir')->realpath();
  $filename_timestamp = "file-name" . date('d-m-Y') . ".xls";
  $path = "$dir/$filename_timestamp";

  // Use the .xls format
  $options = array('format' => 'xls');
  $excel_result = phpexcel_export($header, $data, $path, $options);

  // To avoid file format issue while open file.
  $file = new stdClass();
  $file->uri = $path;

  header('Content-Type: application/vnd.ms-excel');
  header('Content-Disposition: attachment; filename="' . $filename_timestamp . '"');
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');

  readfile($file->uri);
Usage in Drupal:

1) Create hyper link in your page <a href="download/xls"> Export </a>
2) Create menu call back for this URL.

Ex:
  $items['download/xls'] = array(
    'page callback' => 'download_xls',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

Original post - See more details here [>>];

Wednesday, January 7, 2015

PHP sort multi-dimension array by value

Below is the example code for sorting multi-dimension array by its value.

$my_array = array (
  '0' => array (
      'ID' => 425,
      'ln' => 'CBolware',
      'fn' => 'Christian',
      'mi' => '',
     ),
  '1' => array (
      'ID' => 423,
      'ln' => 'Bernstein',
      'fn' => 'ZBear',
      'mi' => 'D.',
     ),
  '2' => array (
     'ID' => 419,
     'ln' => 'DBellweather',
     'fn' => 'Brent',
     'mi' => '',
    ),
  '3' => array (
     'ID' => 356,
     'ln' => 'ABayleaf, III',
     'fn' => 'Joe',
     'mi' => 'X.',
    ),
  '4 '=> array (
     'ID' => 336,
     'ln' => 'Public',
     'fn' => 'John',
     'mi' => 'Q.',
    ),
);

usort($my_array, function($a, $b) {
   return strcmp($a['fn'], $b['fn']);  // fn -> key of the array
});
print_r($my_array);

Tuesday, April 16, 2013

Embed Syntax highlighter in your blog

 

SyntaxHighlighter is a fully functional self-contained code syntax highlighter developed in JavaScript.

 To integrate it to your blog, just follow the below steps.

1) Login into your blog
2) Goto template -> edit as shown blow

Edit template
 3) Paste the below codes before the </head> tag ends

<link href='http://alexgorbatchev.com/pub/sh/2.1.364/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/2.1.364/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushCpp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushCSharp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushCss.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushJava.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushJScript.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushPhp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushPython.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushRuby.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushSql.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushVb.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushXml.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushPerl.js' type='text/javascript'></script>
<script language='javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/2.1.364/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>
 
Note this is pointing to the Latest released version v2.1.364; older versions can be found at: http://alexgorbatchev.com/pub/sh/ 

4)Save the template & open you blog in IE & Firefox to test if any javascript errors are reported as this might conflict with various other embedded scripts in your template.

This completes the direct embedding of SyntaxHighlighter in your Blog. Now lets proceed how to use it in your posts: Usage: For SyntaxHighlighter 2.0 & above: Any code you want to be placed & displayed using SyntaxHighlighter must be enclosed in:

<<span class="IL_AD" id="IL_AD12">script type</span>="syntaxhighlighter" class="brush: html"><![CDATA[
#######YOUR CODE############
]]></script> 
 
Note that we have option of using different brushes for highlighting different type of codes..e.g. HTML/ASP/C++ etc In order to choose the appropriate brush, following are the various brush aliases supporting the corresponding code highlighting: actionscript3 bash, shell c-sharp, csharp cpp, c css     delphi, pas, pascal diff, patch groovy js, jscript, javascript java jfx, javafx perl, pl php plain, text ps, powershell py, python rails, ror, ruby scala sql vb, vbnet xml, xhtml, xslt, html, xhtml Above aliases can be used at: class="brush: alias" while embedding your code in the Blog. Another easy way supported by SyntaxHighlighter is to enclose the code within:

#########YOUR CODE########
The only catch involved out here is, you have to use encoded HTML while embedding within <pre>tags.
Your code can be converted to HTML Encoded using following easy to use Tool:
http://www.string-functions.com/htmlencode.aspx

Just copy paste your code & generate the encoded HTML Code. Paste the encoded HTML within <pre>tags for correct rendering. Failure to do so might render wrong code, especially < & > brackets.

You can find original post from here