Return PHP Data Arrays as a Variable

Sometimes you need to store data but you don’t want to store it in a database. For example, many configuration data is stored in simple text files. Here’s one way to store data in a structured format (using an array) in a text file and have the data easily in a local variable for use.
[cc lang=”php”]
// DATA.PHP
array (
“fieldname” => “firstname”,
“rule” => “required”,
“error_msg” => “Please fill in first name”,
),
“rule2” => array (
“fieldname” => “email”,
“rule” => “required”,
“error_msg” => “”,
)
);
?>
[/cc]
[cc lang=”php”]
// INDEX.PHP

[/cc]

Javascript Tips

 

Put all your JS within an anonymous function using the following template

(function ($) {
	'use strict';

        // global variables go here
        var foo, bar;

	// put code that can be executed before the document is ready, e.g.
        $(window).on('hashchange', function() {
             foo = true;
        }

        $('#content').on('click', "a", function() {
             foo = false;
        }

        // functions go here
        function filterByCheckbox() {

        }

        function validate() {
             // some code
             $("input").trigger("validationPassed");
        }

        // put code that must/should be triggered after the document is ready
        $(document).on ("ready", (function () {
		// on load code goes here, e.g.
		filterByCheckbox();
                foo = true;

		$('div.reports ul.tabs').append("abc");

                // on event handlers (click, focus, submit, etc) go here, e.g.
		$("select").on("change", function() {
                      // something
		});

		$("select").on("blur",validate);

                $("select").on("validationPassed", function() {
                      // something
		});
	});
}(jQuery));

Put each separate code block that serves a separate purpose in a separate template above. Since Javascript is asynchronous, it can execute each code in each block / template simultaneously instead of one at a time.

Never trigger events using element.event(). Always use jQuery to handle events in a cross-browser way, e.g.

BAD

window.hashchange = function() {

}

GOOD

$(window).on('hashchange', function() {

});

For events, always use the jquery “on” and “off” functions to attach/bind and detach/unbind events. .live() is deprecated so use “on” with a subselector.

Normally, when you bind an event handler to an element, you do something like:

$("a").on('click', function (event) { ...

When binding event handlers to elements that are dynamically-generated, you’ll want to do it when the element is in the document, so use this format:

$(document).on('click', " a", function (event) { ...

CSS Tips

  1. Define CSS reset first
  2. Define base styles second, e.g.
    ul, h1-h6, p, b, etc
  3. Define specific styles that may override base styles

Use classes instead of IDs to be safe and make coding easier.

For each page, add an ID to the body element that mirrors the URL to the page, e.g

www.somesite.com/products/fish/(index.php or index.html)

<body id=”products-fish”>

www.somesite.com/products/kites/

<body id=”products-kite”>

This will make it easier to override styles in specific pages without having naming conflicts.

Photoshop Grids to Create CSS Sprites

When creating sprites containing many images, it can be hard and time-consuming to find the position images in the sprite. To simplify this process, create a Photoshop file with a grid of horizontal and vertical guides spaced X pixels apart from each other and turn Snap To Grid on.

Here’s a grid you can use …

Coming whenever I have time (which is not often).

Printing CSS Sprites

Using a sprite image to consolidate all images into one file decreases download time because it reduces the number of HTTP requests required. However, sprites are used as background images and browsers don’t print background images (unless you manually tell them to do so). One workaround is to use jQuery to convert all background images into regular <img> tags. Here’s an article that explains how to do it:

http://quickleft.com/blog/printing-css-sprites

CSS Style Best Practices

When writing CSS styles, the following rules make for easy code management and more efficient processing.

#id
Only reference one ID name in a style.
Good: #breadcrumbs { … }
Bad: #content #breadcrumbs { … }

tag.class
When reference classes, prefix them with the tag they are applied to.
Good: ul.features { … }
Bad: .features { … }

tag
If a style can be applied to the tag without a separate class or ID, then just style the tag. e.g. a { … }

Other good practices are to

  • use a CSS reset
  • use Modernizr to accommodate non-supporting browsers
  • name your styles symantically (according to type of content, not the style), e.g. feature-links as opposed to red-links

Outsource Removal of Background Images

If you have an online retail store, you probably have many product pictures. You can hire a professional photographer to take and clean up photos, at an expensive cost, or you can do it cheaply. Here’s what I do that’s pretty cheap:

  1. Take pictures in my garage against a solid color background (e. g. a white wall)
  2. Hire someone in India via Elance.com to remove the background
    I pay around $0.50 per photo and my guy completes about 100 photos in a couple of days
  3.  Clean up the images in Photoshop
    Using “Auto Tone” and manually adjusting the Levels is usually sufficient
That’s it!

How To Easily Align Objects in Photoshop

To align any object in Photoshop relative to a bounding selection, do the following:

  1. Click on the layer containing the objects you want to align
  2. Using the selection tool, create a selection.
    Normally, if you want to align objects relative to the entire canvas area, select the entire canvas area (CTRL+A)
  3. Click Layer > Align Layers to Selection and then click on an alignment option

This will leave you with perfectly aligned objects.