Archive for the 'Snippets' Category

05th Jul 2007

www vs. non-www 301 redirect

If you would like to force all your incoming URLs to have www at the front for SEO reasons, create an .htaccess file with the following content in the root directory of your website.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

What is 301 redirect?

301 redirect is the most efficient and Search Engine Friendly method for webpage redirection. It’s not that hard to implement and it should preserve your search engine rankings for that particular page. If you have to change file names or move pages around, it’s the safest option. The code “301″ is interpreted as “moved permanently”.

Source: How to Redirect a Web Page

Technorati Tags: , , ,

Posted in Tutorials, Snippets | No Comments »

21st Jun 2007

Error logging in PHP

One of the most important tasks of any developer is to know what errors occur in his application, because it is impossible to fix them if you don’t know if they exist in the first place. Although you may think that your application is perfect and bugs free, you can never be 100% sure because users might not operate the application in the way you expect them to. And this is when error logging come in, where you record any errors that users encounter in a log file which can be used to improve your application. In PHP, you can control how errors are handled and reported. The following is an example of how errors may be logged:


// Error Reporting settings
ini_set('error_reporting', E_ALL ^ E_NOTICE); // log all errors except notices
ini_set(’log_errors’, true); // enable error logging
ini_set(’html_errors’, false); // disable html errors
ini_set(’error_log’, ‘/home/directory/to/your/error_log.txt’); // save all the errors to a log file
ini_set(’display_errors’, true); // do not display errors on screen

Related articles:

Technorati Tags: , , ,

Posted in Tutorials, Snippets, PHP | No Comments »

14th May 2007

Customising category.php in WordPress

I am using WordPress as a CMS for one of my websites. For that website, instead of having a default category page, I wanted to have a customised category page which lists titles of all posts under that category. And I also wanted to display a category image if it exists in /wp-content/uploads/images folder. And because there was no plugin which satisfies my requirements, I created a custom category.php file in my theme folder. Here’s a snippet of my customised category.php.


<?php if (have_posts()) : ?>
<h2><?php single_cat_title(); ?></h2>
<?php
$current_category = single_cat_title("", false);
$image = ‘/wp-content/uploads/images/’ . strtolower(str_replace(’ ‘, ‘-’, $current_category)) . ‘.jpg’;
if (file_exists(ABSPATH . $image)) {
echo ‘<img src="’ . get_bloginfo(’url’) . $image . ‘" alt="’ . $current_category . ‘" />’;
}
?>
<ol>
<?php while (have_posts()) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></li>
<?php endwhile; ?>
</ol>
<?php endif; ?>

What does the above code do?

1. Print out the title of current category
2. Assign the title of the category to a variable called current_category
3. Specify a relative file name for category specific image where image name is the title of the category in lower case with spaces being replaced by hyphens (-)
4. Check whether the specified image above exists on server
5. If it does, print out the image
6. In the post loop, print out titles of all posts under the current category in an ordered list with a link to the individual post page

Technorati Tags: , , ,

Posted in Snippets, PHP, WordPress | No Comments »

18th Apr 2007

CSS Hacks (for IE 7, IE 6, Opera, etc)

The following CSS selectors are considered workarounds and hacks for specific web browsers.

IE 6 and below
* html {}
IE 7 and below
*:first-child+html {} * html {}
IE 7 only
*:first-child+html {}
IE 7 and modern browsers only
html>body {}
Modern browsers only (not IE 7)
html>/**/body {}
Recent Opera versions 9 and below
html:first-child {}

Source: CSS Hacks

Usage
If you want to add 5px padding to a div element called #comments specifically for IE 7, then you can use the following hack:

*:first-child+html #comments {
padding: 5px;
}

But if you want to apply the padding just for IE 6, then the following will do the trick:

* html #comments {
padding: 5px;
}

Note that CSS Hacks are not recommended due to their dependence on browser bugs and therefore they should only be used as the last resort.

A more comprehensive article on CSS Hacks can be found here.

Technorati Tags: , , , , ,

Posted in Snippets, CSS | No Comments »

02nd Mar 2007

LEFT JOIN and RIGHT JOIN in mysql

Use LEFT JOIN when you want to return all rows from left table regardless of whether they have any associated rows on right table.

Use RIGHT JOIN when you want to return all rows from right table regardless of whether they have any associated rows on left table.

SELECT
  users.`firstname`,
  pets.`nickname`
FROM `users`
LEFT JOIN `pets`
ON users.`id` = pets.`user`";

The above query will return all rows in users table regardless of whether they have any pets associated with them or not.

Technorati Tags: , , , ,

Posted in Snippets, PHP, Random | No Comments »