Get the portion of a string after the last occurrence of a character

substr(strrchr($fieldname, "_"), 1);
Posted in Code Snippets | Tagged | Leave a comment

Add Note Within Zend Form

Add a note such as “forgot password” inside a Zend form. I haven’t tried this – I just came across it when I was researching something else but I think I will want it later.

new Zend_Form_Element_Note('forgot_password', array(
    'value' => '<a href="' . $this->getView()->serverUrl($this->getView()->url(array('action' => 'forgot-password'))) . '">Forgot Password?</a>',
))

Source: http://stackoverflow.com/questions/2566432/add-some-html-to-zend-forms

Posted in Notes | Tagged | Leave a comment

Check For an Element on the Page with jQuery

Sometimes I write a function that I only want to apply to certain pages. Say, a form. I may need some javascript to apply to the form when I’m inserting and different javascript to apply when I’m editing. Since I’m reusing the form, I just change the id of the form and use this check:

if ($('#insert-form').length > 0) {
// do stuff that only applies to the insert form
}
Posted in Notes | Tagged | Leave a comment

Naming Controllers and Models with Multiple Words

Here are the rules regarding using multiple words in your controllers and models. Just use capital camel-case.
Model:

class Default_Model_TwoWords extends Zend_Db_Table
{

}

The name of the file would just be TwoWords.php.

Controller:

class Default_TwoWordsController extends Zend_Controller_Action
{

}

The name of the file would, of course, be TwoWordsController.php.

However, your views will be in a folder called “two-words” and the url will be http://mysite.com/two-words. Note the dash.

Posted in Notes | Tagged , | Leave a comment

How to Authenticate in Zend with Salt

Here’s how to create your own custom Zend_Auth_Adapter to use a salt along with a hashed password. Continue reading

Posted in Tutorials | Tagged , | Leave a comment

Custom Form Decorators in Zend

I found a great explanation of how form decorators work and how to customize them here: http://devzone.zend.com/1240/decorators-with-zend_form/

I recently needed to modify the errors decorator so that the entire element (label, input, and error message) was wrapped in a new div. In order to do this I had to create a view partial, which if you haven’t used before sounds a little complicated but it’s not at all. Continue reading

Posted in Tutorials | Tagged , | Leave a comment

System Properties Snippet

Here’s a snippet to list a whole bunch of system properties, including the path where java is installed.

public class TryProperties
{
	public static void main(String[] args) {
		java.util.Properties properties = System.getProperties();
		properties.list(System.out);
	}
}
Posted in Code Snippets | Tagged | Leave a comment

Exception Class Template

public class DreadfulProblemException extends Exception 
{
     // Default Constructor
     public DreadfulProblemException() {}

     public DreadfulProblemException(String s) {
          super(s); // Call the base class constructor
     }

     // Constructor that provides for chained exceptions
     public DreadfulProblemException(String s, Throwable cause) {
          super(s, cause); // Call the base class constructor
     }
}
Posted in Code Snippets | Tagged | Leave a comment

How to create packages in Java

  1. Put all the files in a package together in a separate directory
  2. Add package packageName; to the top of each file in the package
  3. Add the import statement at the top of each class that uses the package
  4. Include the path to the package when compiling: javac -classpath "C:\MyPackages" MyProgram.java. The class path should not include the directory where the package resides, only up to the directory. So for example, if your package resides in “C:\MyPackages\MyPackage,” your classpath would be “C:\MyPackages.”
  5. Run the program using the java command as usual.
Posted in Notes | Tagged | Leave a comment

Compare strings with == using intern()

Normally you compare strings with the equals() method. However, you can compare strings with the == operator. The intern() method discards duplicate String objects, which frees up some memory. You really would only do this if your program generates a lot of duplicate strings, as it is usually faster than calling the equals() method. Here’s how you do it:

String string1 = "here is ";
String string2 = "a string";

string1 += string2; // Make string1 and string2 reference strings that are identical

string1 == string2; // This returns false right now
string1.equals(string2); // This returns true but requires the method call every time you want to compare

string1 = string1.intern(); 
string1 == string3; // Now this returns true

Note that only strings containing variables need to be interned. For example, if you create another string String string3 = "here is ";, it would reference the same object as string1 and you would not need to use the intern() method in order to compare using the == operator.

Posted in Notes | Tagged | Leave a comment