Monday, August 12, 2013

PHP Barcode Generator

Here is an easy to use PHP barcode generator class.
Requirements: PHP Web server and GD Library (Graphics Library).

The PHP class will create a GIF barcode image or save a GIF barcode image file, here is an example:// include Barcode39 class 
include 
"BarCode.php"; 
// set Barcode39 object 
$bc 
= new Barcode39("AB3740-45"); 
// display new barcode 
$bc
->draw();
This example will output this barcode:






You can also easily adjust the barcode bar sizes and text size:// set object 
$bc 
= new BarCode("AB33740-45"); 
// set text size 
$bc
->barcode_text_size = 5; 
// set barcode bar thickness (thick bars) 
$bc
->barcode_bar_thick = 4; 
// set barcode bar thickness (thin bars) 
$bc
->barcode_bar_thin = 2; 
// save barcode GIF file 
$bc
->draw("barcode.gif");
This example will save this barcode as "barcode.gif": 





http://www.shayanderson.com/php/php-barcode-generator-class-code-39.htm

Best way to remove an event handler in jQuery among two event handler

With jQuery 1.7 onward the event API has been updated, .bind()/.unbind() are still available for backwards compatibility, but the preferred method is using the on()/off() functions. The below would now be,
$('#myimage').click(function() { return false; }); // Adds another click event
$('#myimage').off('click');
$('#myimage').on('click.mynamespace', function() { /* Do stuff */ });
$('#myimage').off('click.mynamespace');

In your example code you are simply adding another click event to the image, not overriding the previous one:
$('#myimage').click(function() { return false; }); // Adds another click event
Both click events will then get fired.
As people have said you can use unbind to remove all click events:
$('#myimage').unbind('click');
If you want to add a single event and then remove it (without removing any others that might have been added) then you can use event namespacing:
$('#myimage').bind('click.mynamespace', function() { /* Do stuff */ });
and to remove just your event:
$('#myimage').unbind('click.mynamespace');

Sunday, August 11, 2013

File upload using ExtJS and Grails

/* ExtJS code to create a form */
    var customerEditMainFormParent = new Ext.FormPanel({
        labelAlign: 'top',
        header:false,
        style: "height: 200px;",
        border:false,
        renderTo: 'updateCustomer',
        id: "customer-form2",
        name: "customer-form2",
        fileUpload:true,
        isUpload: true,
        enctype:'multipart/form-data',
        url: Ext.SERVER_URL + 'save_file_data',
        items: [
            {
                xtype: 'fileuploadfield',
                id: 'filedata',
                emptyText: 'Select a document to upload...',
                fieldLabel: 'File',
                buttonText: 'Browse'
            }
        ]
    )};

/* Grails code */
if(params.containsKey("filedata") && bankAccountDetails != null) {
    try {
        String path = System.getProperty("java.io.tmpdir");
        /* Temporary directory */
        CommonsMultipartFile uploadFile = request.getFile("filedata");
        println uploadFile.getOriginalFilename();
        println uploadFile.getContentType();
        println uploadFile.getStorageDescription();
        uploadFile.transferTo(new File(path+"a.png"));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

ExtJS add checkbox group to a panel on fly

/* Add a panel to parent panel as item */
{
    xtype: 'panel',
    id: "paymentMethod",
    fieldLabel: 'Preferred Payment Method'
}

/* Bind 'load' event on store that loads data as JSON */
var paymentstore = new Ext.data.JsonStore({
    proxy: new Ext.data.HttpProxy({
        url: Ext.SERVER_URL + 'get_data',
        method: 'post'
    }),
    listeners :{
        load:function() {
            var testArray = new Array();
            for(var i=0; i<records.length; i++) {
                testArray.push({

                    id: "some_id_" + records[i].data.id,
                    checked: true, /* or false */
                    name: "payment_method[" + records[i].data.id + "]",
                    boxLabel: records[i].data.name,
                    listeners:{
                        check:function (clickedObject, isChecked){
                            console.log(
clickedObject);
                            console.log(
isChecked);
                            console.log("Check box check status changed.");

                            var group = Ext.getCmp ("payment_group");
                            var length = group.items.getCount ();
                            var isCheck = group.items.items [0]. checked;
                        }
                    }
                });
            }
            var myCheckboxgroup = new Ext.form.CheckboxGroup({
                id:'payment_group',
                fieldLabel: 'Checkboxes',
                boxLabel:'State',

                columns: 1,
                items:testArray //created in previous snippet.
            });
            Ext.getCmp("paymentMethod").add(myCheckboxgroup);

            Ext.getCmp("paymentMethod").doLayout();
        }
    },
    fields: ['id',  'name', "description"],
    totalProperty: 'totalCount',
    root: 'data'
});

/* Load store */
paymentstore.load({
    params: {
        start: 0
    }
});

Jquery/JS prevent right click menu in browsers

Using jQuery:
$('div#id_of_div').on("contextmenu",function(evt){
    evt.preventDefault(); return false;
});
Or disable context menu on the whole page:
$(document).on("contextmenu",function(evt){
    evt.preventDefault(); return false;
});

Thursday, August 8, 2013

Php create a singletone mysql database connection class

<?php

class Database {

    private static $db;
    private $connection;

    private function __construct() {
        $this->connection = new MySQLi("localhost", "my_user", "my_password", "my_db", "3306");
    }

    function __destruct() {
        $this->connection->close();
    }

    public static function getConnection() {
        if ($db == null) {
            $db = new Database();
        }
        return $db->connection;
    }
}

?>
Then just use $db = Database::getConnection(); wherever I need it.