5 Tips for Better jQuery Code

Filed Under (General) by Wenbert on 20-11-2008

Tagged Under : ,

#1: Use data method instead of storing data inside the DOM.
#2: Take advantage of jQuery’s built-in custom selectors.
#3: If you are Manipulating the DOM a lot, use livequery.
#4: Use jQuery form plugin to submit files via Ajax.
#5: Use classes as flags.

Go here to read the complete post.

jQuery Javascript Tutorial: Check All / Uncheck All Checkboxes

Filed Under (General) by Wenbert on 19-11-2008

Tagged Under :

This is using jQuery’s toggle() function.

First you have to include jquery.js in the head of your html. (duh)

  1. <script language="javascript" src="js/jquery.js"></script>

Then in your body, you will probably have something like this:

  1. <img id="checkallboxes" src="img/check_all_boxes.gif" alt="" />
  2. <input id="target_item[]" class="checkbox1" name="target_item[]" type="checkbox" value="myval01" /> My Value 01
  3. <input id="target_item[]" class="checkbox1" name="target_item[]" type="checkbox" value="myval02" /> My Value 02
  4. <input id="target_item[]" class="checkbox1" name="target_item[]" type="checkbox" value="myval03" /> My Value 03
  5. <input id="target_item[]" class="checkbox1" name="target_item[]" type="checkbox" value="myval04" /> My Value 04
  6. <input id="target_item[]" class="checkbox1" name="target_item[]" type="checkbox" value="myval05" /> My Value 05
  7.  
  8. <!– This is the jQuery part. It will check all boxes with classname that is equal to checkbox1 –>
  9. <script type="text/javascript"><!–
  10. $(document).ready(function(){
  11.     $(".checkallboxes").toggle(                    //this is the gif file id
  12.     function() {
  13.         $(‘.checkbox1′).attr(‘checked’,‘checked’); //add the atrribute checked
  14.     },
  15.     function () {
  16.         $(‘.checkbox1′).removeAttr("checked");     //remove the attribute checked
  17.     });
  18. });
  19. –></script>

That’s about it.

Taking a break. Playing Diablo 2 ;-)

Filed Under (General) by Wenbert on 19-11-2008

Tagged Under : , , ,

There will be fewer posts for a few weeks. I am taking a break from all the side-projects — both pet projects and paid ones. I’ll be doing things one at a time within the next few weeks.

My sorceress (ice+lightning=w00t!) is now level 30 :) I play with my brothers and cousins. If you want to hook up on a TCP-IP game just send me an email. We are mostly doing Andariel and Diablo runs :D

Zend Framework 1.7.0 Released!

Filed Under (General) by Wenbert on 18-11-2008

Tagged Under : ,

A lot of additions. Including something from jQuery (the Javascript framework that saves lives). Here is a quote from Zend’s official statement.

Zend Framework 1.7.0 is now available from the Zend Framework download site: http://framework.zend.com/download/latest

This release introduces many new components and features, including:

* Zend_Amf with support for AMF0 and AMF3 protocols
* Dojo Toolkit 1.2.1
* Support for dijit editor available in the Dojo Toolkit
* Zend_Service_Twitter
* ZendX_JQuery in extras library
* Metadata API in Zend_Cache
* Google book search API in Zend_Gdata
* Preliminary support for GData Protocol v2 in Zend_Gdata
* Support for skip data processing in Zend_Search_Lucene
* Support for Open Office XML documents in Zend_Search_Lucene indexer
* Performance enhancements in Zend_Loader, Zend_Controller, and server components
* Zend_Mail_Storage_Writable_Maildir enhancements for mail delivery
* Zend_Tool in incubator
* Zend_Text_Table for formatting table using characters
* Zend_ProgressBar
* Zend_Config_Writer
* ZendX_Console_Unix_Process in the extras library
* Zend_Db_Table_Select support for Zend_Paginator

XP-Dev: Assembla’s Alternative (Free Subversion Hosting)

Filed Under (General) by Wenbert on 02-11-2008

Tagged Under : , , ,

If you looking for an Assembla alternative, you can use XP-Dev. They offer free subversion hosting. The best part is that they also allow you to host commercial code. I have emailed the owner directly regarding concerns about how he pays for bandwidth and storage. He directed me to his blog post — and so far so good. He seems like a good entrepreneur, letting the customers come first — then the money will come next. Someone who would put himself in the customers’ shoes and not let them down.

So what are you waiting for, go to XP-Dev.com and sign up now.

Here are some of the features:

*  5 free subversion repositories (modules) to each user *
* Unlimited number of repository users
* Each subversion repository has a soft limit of 300MB **
* Nightly offsite backup
* No restriction to the type of content that is hosted (open source, proprietary, etc are all allowed)
* All repositories are private to you and your team members
* Control access permissions (read/write) for each user to each repository
* Post commit emails to keep track of checkins

Zend Framework Tutorial: A Fully Customized Form Using Zend_Form and Decorators (UPDATED now using Tables)

Filed Under (General) by Wenbert on 29-10-2008

Tagged Under : , ,

This tutorial is derived from this post. So I suggest that you read weirophinney’s post first before this.

This method of rendering a form can be more verbose, but it also allows you to tweak the form at almost infinitely whole still gaining the advantages of error reporting, labeling, etc that decorators provide (by using the decorators associated with the elements).

I would like to stress out the advantages:

  • Error Reporting
  • And Labelling

You get these by doing only this in your view script (.phtml file):

  1. <?=$this->form->firstname ?>
  2. <?=$this->form->lastname ?>

Looking good? Well, here is the rest of it.

First, create this file: application/forms/sample/sampleform.php

Inside sampleform.php, you should have something like this:

  1. class forms_sample_sample2form extends Zend_Form
  2. {
  3.     public $checkboxDecorator = array(
  4.                                     ‘ViewHelper’,
  5.                                     ‘Errors’,
  6.                                     ‘Description’,
  7.                                     array(‘HtmlTag’,array(‘tag’ => ‘td’)),
  8.                                     array(‘Label’,array(‘tag’ => ‘td’,‘class’ =>‘element’)),
  9.                                     array(array(‘row’ => ‘HtmlTag’), array(‘tag’ => ‘tr’)));
  10.     public $elementDecorators = array(
  11.                                     ‘ViewHelper’,
  12.                                     ‘Errors’,
  13.                                     ‘Description’,
  14.                                     array(‘HtmlTag’,array(‘tag’ => ‘td’)),
  15.                                     array(‘Label’,array(‘tag’ => ‘td’,‘class’ =>‘element’)),
  16.                                     array(array(‘row’ => ‘HtmlTag’), array(‘tag’ => ‘tr’)));
  17.     public $buttonDecorators = array(
  18.                                     ‘ViewHelper’,
  19.                                     array(‘HtmlTag’,array(‘tag’ => ‘td’)),
  20.                                     //array(’Label’,array(’tag’ => ‘td’)), NO LABELS FOR BUTTONS
  21.                                     array(array(‘row’ => ‘HtmlTag’), array(‘tag’ => ‘tr’)));
  22.     public function init()
  23.     {
  24.         $this->setAction();
  25.         $this->setMethod(‘post’);
  26.  
  27.         $this->addElement(‘text’, ‘email’, array(
  28.             ‘decorators’ => $this->elementDecorators,
  29.             ‘label’       => ‘Email:’,
  30.             ‘required’   => true,
  31.             ‘validators’  => array(
  32.                             ‘EmailAddress’,
  33.                             ),
  34.             ‘attribs’ =>   array(
  35.                                 ‘id’=>‘email_id’,
  36.                                 ‘class’=>‘email_class’
  37.                             ),
  38.         ));
  39.  
  40.         $this->addElement(‘text’, ‘age’, array(
  41.             ‘decorators’ => $this->elementDecorators,
  42.             ‘label’      => ‘Age:’,
  43.             ‘required’   => true,
  44.         ));
  45.  
  46.         $this->addElement(’select’, ‘country’, array(
  47.             ‘decorators’ => $this->elementDecorators,
  48.             ‘label’      => ‘Country:’,
  49.             ‘required’   => true,
  50.             ‘attribs’ =>   array(
  51.                                 ‘id’=>‘country_id’,
  52.                             ),
  53.             ‘multioptions’   => array(
  54.                             ‘ph’ => ‘Philippines’,
  55.                             ‘us’ => ‘USA’,
  56.                             ),
  57.         ));
  58.  
  59.         $this->addElement(‘text’, ‘username’, array(
  60.             ‘decorators’ => $this->elementDecorators,
  61.             ‘label’       => ‘Username:’,
  62.             ‘validators’  => array(
  63.                                 array(’stringLength’, 1, 255)
  64.                             ),
  65.             ‘required’   => true,
  66.         ));
  67.  
  68.         $this->addElement(‘text’, ‘firstname’, array(
  69.             ‘decorators’ => $this->elementDecorators,
  70.             ‘label’      => ‘First Name:’,
  71.             ‘required’   => true,
  72.         ));
  73.  
  74.         $this->addElement(‘text’, ‘lastname’, array(
  75.             ‘decorators’ => $this->elementDecorators,
  76.             ‘label’       => ‘Last Name:’,
  77.             ‘required’   => true,
  78.         ));
  79.  
  80.         $this->addElement(‘radio’, ‘gender’, array(
  81.             ‘decorators’ => $this->elementDecorators,
  82.             ‘label’      => ‘Gender:’,
  83.             ‘required’   => true,
  84.             ‘attribs’ =>   array(
  85.                                 ‘id’=>‘country_id’,
  86.                             ),
  87.             ‘multioptions’   => array(
  88.                             ‘male’ => ‘Male’,
  89.                             ‘female’ => ‘Female’,
  90.                             ),
  91.         ));
  92.  
  93.         $checkboxDecorator = array(
  94.                                 ‘ViewHelper’,
  95.                                 ‘Errors’,
  96.                                 array(array(‘data’ => ‘HtmlTag’), array(‘tag’ => ’span’, ‘class’ => ‘element’)),
  97.                                 array(‘Label’, array(‘tag’ => ‘dt’),
  98.                                 array(array(‘row’ => ‘HtmlTag’), array(‘tag’ => ’span’)),
  99.                             ));
  100.         $this->addElement(‘checkbox’, ‘agreement’, array(
  101.             ‘decorators’ => $checkboxDecorator,
  102.             ‘label’       => ‘Agreement:’,
  103.             ‘required’   => true,
  104.         ));
  105.  
  106.         $this->addElement(’submit’, ’save’, array(
  107.             ‘decorators’ => $this->buttonDecorators,
  108.             ‘label’       => ‘Save’,
  109.         ));
  110.  
  111.     }
  112.     public function loadDefaultDecorators()
  113.     {
  114.         $this->setDecorators(array(
  115.             ‘FormElements’,
  116.             array(‘HtmlTag’, array(‘tag’ => ‘table’)),
  117.             ‘Form’,
  118.             ‘Errors’
  119.         ));
  120.     }
  121. }

Then inside one of my controller-action, I have this:

  1.    public function sample2Action()
  2.     {
  3.         require_once  ‘forms/sample/sampleform.php’;
  4.         $form = new forms_sample_sampleform();
  5.         //get the request object
  6.         $request = $this->getRequest();
  7.         if($request->isPost()) {
  8.             if($form->isValid($request->getPost())) {
  9.                 //do some saves here
  10.                 var_dump($request->getPost());
  11.             }
  12.         }
  13.         $this->view->form = $form;
  14.     }

Now, in my view script. These are the .phtml files.

  1.  
  2. <h4>Please register with us!</h4>
  3. <form action="<?= $this->escape($this->form->getAction()) ?>" method="< ?= $this->escape($this->form->getMethod()) ?>">
  4.  
  5. <fieldset>
  6.     <legend>Demographics</legend>
  7.  
  8.         Please provide us the following information so we can know more about
  9.         you.
  10.  
  11.     < ?= $this->form->age ?>
  12.     < ?= $this->form->country ?>
  13.  
  14. </fieldset>
  15.  
  16. <fieldset>
  17.     <legend>User Information</legend>
  18.  
  19.         Now please tell us who you are and how to contact you.
  20.  
  21.     < ?= $this->form->username ?>
  22.     < ?= $this->form->firstname ?>
  23.     < ?= $this->form->lastname ?>
  24.     < ?= $this->form->email ?>
  25.     < ?= $this->form->gender ?>
  26.     < ?= $this->form->agreement ?
  27.  
  28. </fieldset>
  29.  
  30. < ?= $this->form->save ?>
  31. </form>

Enjoy!

Feel free to comment/suggest… ;)

Some references in order to help you understand the form decorators ($elementDecorators, etc.) can be found in this thread from Nabble Zend Framework.

UPDATE:
The $elementDecorators variable above might seem a little bit confusing, so here is another example on how to handle it. This one sets a “class” for the first cell and the second cell of the table.

  1.  
  2.     public $elementDecorators = array(
  3.                                     ‘Label’,
  4.                                     array(array(‘labelTd’=>‘HtmlTag’),
  5.                                           array(‘tag’=>‘td’, ‘class’=>‘label_cell’)),
  6.                                     array(array(‘elemTdOpen’=>‘HtmlTag’),
  7.                                           array(‘tag’=>‘td’, ‘openOnly’=>true,
  8.                                                 ‘class’=>‘input_cell’, ‘placement’=>‘append’)),
  9.                                     ‘ViewHelper’,
  10.                                     ‘Errors’,
  11.                                     array(‘Description’, array(‘tag’ => ‘div’)),
  12.                                     array(array(‘elemTdClose’=>‘HtmlTag’),
  13.                                           array(‘tag’=>‘td’, ‘closeOnly’=>true, ‘placement’=>‘append’)),
  14.                                     array(array(‘row’ => ‘HtmlTag’), array(‘tag’ => ‘tr’)));
  15.  
  16.     public $checkboxDecorator = array(
  17.                                     ‘ViewHelper’,
  18.                                     ‘Errors’,
  19.                                     ‘Description’,
  20.                                     array(‘HtmlTag’,array(‘tag’ => ‘td’)),
  21.                                     array(‘Label’,array(‘tag’ => ‘td’,‘class’ =>‘element’)),
  22.                                     array(‘Description’, array(‘tag’ => ’span’)),
  23.                                     array(array(‘row’ => ‘HtmlTag’), array(‘tag’ => ‘tr’)));
  24.  
  25.     public $buttonDecorators = array(
  26.                                     ‘ViewHelper’,
  27.                                     array(‘HtmlTag’,array(‘tag’ => ‘td’)),
  28.                                     //array(’Label’,array(’tag’ => ‘td’)), NO LABELS FOR BUTTONS
  29.                                     array(array(‘row’ => ‘HtmlTag’), array(‘tag’ => ‘tr’)));
  30.  

The best color picker for Mac OS

Filed Under (General) by Wenbert on 25-10-2008

Tagged Under : , , ,

Lightweight. Fast. Simple. It’s Pipette.

Type casting in MySQL: Comparing string numbers and getting the right result

Filed Under (General) by Wenbert on 22-10-2008

Tagged Under : ,

I recently ran into a problem where I had to compare “1000″ and “999″ — take note of the quotes, these values are strings. I did a SUBSTRING() in order to get those 2 strings, er “numbers”.

In this SQL statement, it asks whether “1000″ is greater than “999″. At first glance, the expected result would be ‘Yes’ but when you run that query, the result is ‘NO’. More info for MySQL If statements here.

  1. SELECT
  2.     IF("1000" > "999",‘Yes’,‘NO’)
  3. FROM table WHERE 1

By doing a CAST() on the string, you will the correct result.

  1. SELECT
  2. IF( CAST("1000" AS DECIMAL) > CAST("9" AS DECIMAL),‘Yes’,‘NO’)
  3. FROM table WHERE 1

More info for the MySQL CAST() function here.

ZF Quickstart Downloadable - Copy to your htdocs and then run it

Filed Under (General) by Wenbert on 21-10-2008

Tagged Under : , ,

Download the file. Untar, put in your htdocs folder (/var/www/zf_working) and the your should be able to run Zend Framework using: http://localhost/zf_working

I have tested this in two machines only :P I changed some paths in the bootstrap files from the ZF Quickstart tutorial. This file is provided as is — I’m at work right now and will be downloading when I get home to start one of my pet projects.

A Paper on SQL Injection

Filed Under (General) by Wenbert on 17-10-2008

Tagged Under : ,

It is a long read but this post links to PDF file about SQL Injection — here is the abstract:

 

ABSTRACT

Googling for “SQL injection” gets about 4 million hits. The topic excites interest and superstitious fear. This whitepaper dymystifies the topic and explains a straightforward approach to writing database PL/SQL programs that provably guarantees their immunity to SQL injection.

Only when a PL/SQL subprogram executes SQL that it creates at run time is there a risk of SQL injection; and you’ll see that it’s easier than you might think to freeze the SQL at PL/SQL compile time. Then you’ll understand that you need the rules which prevent the risk only for the rare scenarios that do require run-time-created SQL. It turns out that these rules are simple to state and easy to follow.

 

I have not read the entire paper yet — that is why I’m posting this one on my blog.

Subscribe to Rss Feed : Rss