Yii : CGridView – Create Custom Column


I have a field which contains one character to represent project status (eg: F=Finished, S=Start, P=Pending, etc)

By default, CGridView will display one character value, but we want CGridView to display its description taken from other table, this is what I did:

First, create a component, let’s name it MyComponent. You can do it by create MyComponent.php and save it in component folder, I write something like this:

<?php
class MyComponent extends CApplicationComponent 
{
 public function getProjectStatus($statusCode)
 { 
 $sql = "SELECT STATUS_CODE,
 DESCRIPTION
 FROM REF_PROJECT_STATUS WHERE STATUS_CODE='$statusCode'";

 $rs = Yii::app()->db->createCommand($sql)->queryAll();

 $data = array();

 foreach($rs as $item){

 $statusCode = $item['STATUS_CODE'];
 $description = $item['DESCRIPTION'];

 $data[$statusCode] = $description;
 }
 
 return $data[$statusCode]; 
 } 
}

This way, you can call the function like this
Yii::app()->mycomponent->getProjectStatus($statusCode)

Then, replace the field name in your views (admin.php) with something like this

array('header' => 'Project Status', 'value' => 'Yii::app()->mycomponent->getProjectStatus($data->PROJECT_STATUS)'),

Some people suggest the function should be static

Yii version: 1.1.10

jQuery : Select All Checkbox


Yahoo! Mail and GMail both has a checkbox on top of their email list which will “checked” all chekboxes when clicked. Previously I use plain old javascript like those in this link, I just try to use jQuery to do this, its only 5 lines

$('#selectAll').click(function(event){
    $('input[type="checkbox"]').each(function(){
        this.checked = event.target.checked;
    });
});

Android Studio Error on First Run (Windows)


After installing Android Studio on my new laptop and then running it for the first time, it throw a dialog box detailing error messages, a quick search on Google point me to this link Issue 172032: Android Studio Start failed with internal error and the “temporary solution” is simple, just edit idea.properties file located in

C:\Program Files\Android\Android Studio\bin\

then add this text

disable.android.first.run=true

and save, then try to relaunch Android Studio, it works like a charm🙂

Note: editing idea.properties file required Administrator privileges so you maybe need to launch your editor as Administrator

Python – PyODBC – Microsoft Access : Simple script


Here’s a simple python script showing how to access MDB (Microsoft Access) database

import os
import pyodbc

dataFile = "Northwind.mdb"
databaseFile  = os.getcwd() + "\\" + dataFile
connectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=%s" % databaseFile
dbConnection   = pyodbc.connect(connectionString)
cursor = dbConnection.cursor()
cursor.execute("select top 5 * from Suppliers")
rows = cursor.fetchall()
for row in rows:
    print row.CompanyName

take a look at the script below to get column names, column type and width

import os
import pyodbc

dataFile = "Northwind.mdb"
databaseFile  = os.getcwd() + "\\" + dataFile
connectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=%s" % databaseFile
dbConnection   = pyodbc.connect(connectionString)
cursor = dbConnection.cursor()

for row in cursor.columns(table='Suppliers'):
    print "Field name: " + str(row.column_name)
    print "Type: " + str(row.type_name)
    print "Width: " + str(row.column_size)