May18

Adding shadow to wp-codebox

Posted by: Brian Chan | Filed in: technology | Tags: , | 05:55 pm, May 18th, 2012 No Comments »

Too bad wp-codebox contains two divs namely wp_codebox_msgheader and wp_codebox, we cannot simply give the box-shadow to one single div. After playing with this a bit, here is my parameters. The css file is located at /your-wp-root/wp-content/plugins/wp-codebox/css/codebox.css.

1
2
3
4
5
6
.wp_codebox_msgheader {
        box-shadow: 6px 14px 11px #808083;
}
.wp_codebox {
        box-shadow: 7px 10px 10px #888;
}

The overlapped area seems smooth with this setting.

I also added bottom margin to the codebox, see if you like it.

1
2
3
.wp_codebox {
        margin-bottom: 20px;
}


May18

Installing Ruby on Rails in osx

Posted by: Brian Chan | Filed in: technology | Tags: | 05:23 pm, May 18th, 2012 No Comments »

Install Xcode first. You will need the one from the Mac App Store. You will need version 4.3 or above.

Then install homebrew. Go to this link (https://github.com/mxcl/homebrew/wiki/installation) to find the installation command. As of May 2012, the command is:

1
/usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"

Then after the process finishes (it took like 30 seconds), run

1
brew doctor

This is just to make sure your environment is good to move on. If your Xcode is old you will get a complain here like the following:

1
2
3
4
$ brew doctor
 
Error: You have Xcode 4.1, which is outdated.
Please install Xcode 4.3.

I have installed the mcrypt lib myself, and I got warnings about that (see below). If that happens to you just ignore it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Error: Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.
 
Unexpected dylibs:
    /usr/local/lib/libmcrypt.4.4.8.dylib
Error: Unbrewed .la files were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.
 
Unexpected .la files:
    /usr/local/lib/libmcrypt.la
Error: Unbrewed static libraries were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.
 
Unexpected static libraries:
    /usr/local/lib/libarmdis.a    /usr/local/lib/libAstrisAPI.a

Then Install git by:

1
brew install git

You can make sure git is successfully installed by issuing:

1
git --version

Then install MySQL. Now, I believe you already have it if you are using Lion. Type the following command to see if you have mysql installed.

1
mysql -u root

If you get a mysql prompt, then you are good. If not, install it by issuing:

1
brew install mysql

You can alternatively install MAMP if you like, as MySQL will be one of the packages inside MAMP.

Then install RVM. This is the Ruby Version Manager. It will make your life so much easier down the road.

1
bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

After installing rvm, edit your ~/.bash_profile file to include the following line. Make sure it’s there.

?View Code BASH_PROFILE
1
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

Then source your bash_profile file as you always do.

1
source ~/.bash_profile

Now Install Ruby. Lion already has 1.8 but we are going to install the latest 1.9.3.

1
rvm install 1.9.3

It will start downloading and installing. Just wait until it’s done, all automated. Isn’t it nice?

After installing check your ruby version by issuing:

1
ruby -v

You should see something like ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.3.0].

Then lastly install Rails.

1
2
3
gem install bundler
gem install mysql2
gem install rails

Now that we have rails installed, it’s time to test it out! Let’s create a simple test app in our home directory.

1
2
cd
rails new mytest

All your new projects files are inside the newly created directory called mytest. Now to run this app issue:

1
2
cd mytest
rails server

You will see something like the following:

?View Code CONSOLE
1
2
3
4
5
6
7
8
$ rails server
=> Booting WEBrick
=> Rails 3.2.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-05-18 17:09:11] INFO  WEBrick 1.3.1
[2012-05-18 17:09:11] INFO  ruby 1.9.3 (2012-04-20) [x86_64-darwin11.3.0]
[2012-05-18 17:09:11] INFO  WEBrick::HTTPServer#start: pid=7008 port=3000

So open a web browser and type in http://0.0.0.0:3000 into the location bar. If you see the rails welcome page, then congratulations! You are on Rails now!


You can find more very useful info on the rails official site at http://guides.rubyonrails.org/getting_started.html

I heavily borrowed much content and edited some portions from http://thinkvitamin.com/code/ruby-on-rails/installing-ruby-rails-and-mysql-on-os-x-lion/.



May16

Check the existence of an element in jquery

Posted by: Brian Chan | Filed in: technology | Tags: , | 11:18 am, May 16th, 2012 No Comments »

When dealing with dynamic creation of DOM objects, sometimes you need to check if a certain element exists. Below is how you check:

1
2
3
if ($('#my_element').length > 0){
  // it exists!
}

If you need to check whether a dropdown has any options, here is how

1
2
3
if ($('#my_dropdown option').length > 0){
  // contains options
}


May2

Find out the physical memory size in osx

Posted by: Brian Chan | Filed in: technology | Tags: | 01:22 pm, May 2nd, 2012 No Comments »

Far too often I accomplish this by clicking on the apple icon on the menu bar, then click “About this Mac”, then just read from the popup window. This is perfectly fine and all, until one day I was given only a shell and I needed to get that info.

Well, here is how you can do it in command line:

1
sysctl -a | grep mem

Then look for hw.memsize in the list. The number is in byte size, so you will need to do some conversion in your head. For example, if you see hw.memsize = 12884901888, that’s actually 12 * 1024 * 1024 * 1024, which is 12Gb.



May1

Nest Learning Thermostat

Posted by: Brian Chan | Filed in: DIY, technology | Tags: | 01:52 pm, May 1st, 2012 No Comments »

Interesting thermostat from Nest. The website also has an installation tutorial video.



Apr13

MySQL converting Text to Varchar

Posted by: Brian Chan | Filed in: technology | Tags: , | 05:04 pm, April 13th, 2012 No Comments »

One incentive of converting a text column to a varchar column is that, you can index that column for quicker query.

Before converting, you want to make sure you won’t be truncating anything. Run the following to make sure

1
2
SELECT MAX( LENGTH(  target_column ) ) 
FROM  target_table

As long as the returned length is less than your varchar length, you are good to go.



Apr4

Project Glass

Posted by: Brian Chan | Filed in: technology | Tags: | 07:13 pm, April 4th, 2012 No Comments »



Mar29

DuckDuckGo

Posted by: Brian Chan | Filed in: technology | Tags: | 07:07 pm, March 29th, 2012 No Comments »

DuckDuckGo is a search engine much like google. I tried it with a few searches and it’s just as fast as google if not faster. For now it’s not a threat yet but who knows.

I like how it gives you its traffic stats as well. ;)



Mar26

PHPExcel: the right way to open files

Posted by: Brian Chan | Filed in: technology | Tags: | 10:29 pm, March 26th, 2012 No Comments »

Just let PHPExcel figure out the file type by using PHPExcel_IOFactory::identify()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$filename = 'your_file.xls';
 
require_once 'PHPExcel/Classes/PHPExcel.php';
 
// Create new PHPExcel object
$filetype = PHPExcel_IOFactory::identify($filename);
$objReader = PHPExcel_IOFactory::createReader($filetype);
$objReader->setReadDataOnly(true);  // set this if you don't need to write
$objPHPExcel = $objReader->load($filename);
 
// go through each sheet
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
  ...
}


Mar22

Packing form elements using json

Posted by: Brian Chan | Filed in: technology | Tags: | 07:47 pm, March 22nd, 2012 No Comments »

Sometimes I don’t really want to pass form elements one by one in my ajax call, I just want to pack them all in one giant obj and send it over to the backend script. After looking for a while, I found the solution. See sample code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
json_str = JSON.stringify($('#my_form').serializeArray());
 
$.ajax({
  url: 'do_something.php',
  type: 'POST',
  dataType: 'json',
  data: {
    form_data: json_str
  },
  success: function(data) {
    // do something with data
  },
  error: function(request, error) {
    alert('error: '+error+'\nreadyState: '+request.readyState+'\nstatus: '+request.status);
    alert('responseText: '+request.responseText);
  }
});

Yea, serializeArray() will convert a form object to a javascript array. Notice this method can only apply to form objects. Then the stringify method will convert the array to a json string.

Once the json string gets sent over, here is what happens on the php side. I like associated arrays.

1
2
3
4
5
6
7
8
9
10
$form_data_encoded = $_POST['form_data'];
$form_data_decoded = json_decode($form_data_encoded);
 
// building an associated array
$form_data = array();
foreach ($form_data_decoded as $obj){
  $name = $obj->{"name"};
  $value = $obj->{"value"};
  $form_data[$name] = $value;
}

In case you are a pure javascript dude, here is some code to build an associated array in javascript out of that json_str from above.

1
2
3
4
5
6
var arr = jQuery.parseJSON(json_str);
var dict = new Array();
for (k in arr){
  arr2 = arr[k];
  dict[arr2.name] = arr2.value;
}