Thursday, April 2, 2015

Convert CSV to Associative Array

function csvToAssocArray($csvFile) {
//This function will help you to convert any count of csv data in associative array
$row = 0;
$resultSet = array();
if (($handle = fopen($csvFile, "r")) !== FALSE) {
$dataCtr = 0;
$dataLabel = array();

while (($data = fgetcsv($handle, 10000, "\t")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
if($row==0) $dataLabel[] = $data[$c]; //First row as heading as key for the associative array
else $resultSet[$row][$dataLabel[$c]] = $data[$c]; //other row to map the data with associative array
}
$row++;
}
fclose($handle);
} //End of while

return count($resultSet):$resultSet?false; //If no data return false;
}

Tuesday, February 18, 2014

jQuery AutoComplete with JSON:

<input name="txt_search" type="text"  id="txt_search"  />
.
.
.
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
.
.
.

jQuery("#txt_search").autocomplete({
source: function(request, response) {
   $.ajax({
   url: "search.jsp",
   type: "POST",
   dataType: "json",
   data: { searchStringParam: request.term},
   success: function( data ) {
    response( $.map( data, function( item ) {
       return {
           label: item.value,
           value: item.value,
       }
       }));
   },
   error: function (error) {
      var obj = jQuery.parseJSON(error);
      alert('error: ' + obj.value);
   }
   });
   },
   minLength: 2
});

Search.jsp:
[
{"value":val1},
{"value":val2},
]

Friday, March 16, 2012

Mysql JSP Connection

Download mysql jar and add in your project library. The jar file can be download from below url:
http://mirrors.ibiblio.org/pub/mirrors/maven2/mysql/mysql-connector-java/3.1.12/mysql-connector-java-3.1.12.jar

<%@ page language="java" import="java.sql.*"%>
<% /*Database connection*/ out.println("MySQL Connect Example.
");
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String driver = "com.mysql.jdbc.Driver"; //or String driver = "org.gjt.mm.mysql.Driver";
String userName = "user";
String password = "pass";

Connection con=null;
ResultSet rst=null;
Statement stmt=null;

try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+dbName,userName,password);
out.println("Connected to the database
");

stmt=con.createStatement();
rst=stmt.executeQuery("Query");
while(rst.next()) {
out.println("
"+rst.getString("field"));
}

con.close();
out.println("
Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
out.println(e);
}
%>

Friday, February 17, 2012

MySQL User Privileges

Create User:
CREATE USER username IDENTIFIED BY 'password';

ex:
- CREATE USER myuser IDENTIFIED BY 'mypass';
- CREATE USER myuser;


Assign Permission:
GRANT PRIVILEGES_LIST|ALL ON DATABASE[.TABLES|.*] TO 'username'@'HOST';

ex:
- GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON mydb TO 'myuser'@'localhost';

List Permission:
SHOW GRANTS FOR 'user'@'host';

ex:
SHOW GRANTS FOR 'admin'@'localhost';

Friday, February 3, 2012

MySql Database backup command - mysqldump

Syntax:
mysqldump [options] -h hostname -u username -p password database|--all-databases [tables] > backupfile.sql

Example:
  • mysqldump -h localhost -u root -p test > backup.sql
  • mysqldump -h localhost -u root -p test mytable > backup.sql
  • mysqldump -h localhost -u root -p --all-databases > backup.sql

Sunday, August 14, 2011

Getting Nth highest salary (will work for all databases)

SELECT * FROM employee e1 WHERE (n-1)=(SELECT COUNT(DISTINCT salary) FROM employee  e2 WHERE e2.salary>e1.salary)

*n - Nth highest salary

Wednesday, July 20, 2011

How to get Latitude and Longitude of a location

  • Open maps.google.com.
  • Point the position, which latitude and longitude require.
  • Place the below JS code in address bar of your browser.
javascript:void(prompt('',gApplication.getMap().getCenter()));
  • A popup alert will show you the latitude and longitude of the selected location.

Friday, July 1, 2011

Linux - Useful commands

Search any software and its version:
apt-cache search software_name


Install software:
sudo apt-get install software_name

Install sh file:

sudo sh file_name.sh

Saturday, June 25, 2011

Perl - How to install and configure Perl with Apache web server on windows

1. Install ActivePerl on your windows machine as per your OS.

2. Downlaod "mod_perl.so" file and place under "Apache/modules" folder.

3. Create "startup.pl" under "apache at your system/conf/extra".

use ModPerl::Util ();
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::RequestUtil ();
use Apache2::ServerRec ();
use Apache2::ServerUtil ();
use Apache2::Connection ();
use Apache2::Log ();
use Apache2::Const -compile => ':common';
use APR::Const -compile => ':common';
use APR::Table ();
use Apache2::compat ();
use ModPerl::Registry ();
use CGI ();
1;

4. Create "httpd-perl.conf" under "apache at your system/conf/extra".

LoadFile "Perl/at/your/system/path/bin/perl-x.dll"
LoadModule perl_module modules/mod_perl.so
PerlPostConfigRequire "apache at your system/conf/extra/startup.pl"

AddType text/html .pl

SetHandler perl-script
PerlResponseHandler ModPerl::Registry
PerlOptions +ParseHeaders

SetHandler cgi-script

5. Restart your apache and write the below test perl code under your web-root as "demo.pl"

print "Content-type: text/plain;\n\n";
print "Hello Perl"

6. Run the code in web browser as

http://web-root/demo.pl

Wednesday, December 15, 2010

PHP - Compare two single dimensional array

function is_same($array1, $array2)
{
if(count($array1) == count($array2)) {
sort($array1);
sort($array2);
foreach($array1 as $k => $val) {
if($array1[$k] != $array2[$k]) return false;
}
}
else
return false;
return true;
}

Monday, November 15, 2010

Drupal 6 - How to place regions/block position

Step 1: Open template .info file and define the region name
regions[region_name] = Region Label

Step 2: In page.tpl.php file write the code
<?php print $region_name; ?>

Step 3: From admin section under
Home » Administer » Site building » Blocks
You will have a look of the all regions/block positions. You can assign your block to your regions.

Step 4: Go to website, you will be able to see the content on your own region/block position.