Clear and simple Javascript, CSS, AJAX and PHP snippets. These are snippets of code that I re-use everyday and find extremely useful for site development. You will find functions, commands and one-line strings that make coding a bit easier.

Installing Drupal 7 on 1and1 servers

You will need a 1and1 hosted package that supports PHP 5.2+ and MySQL 5.xx to run Drupal 7

1- Download Drupal 7.xx

2- Uncompress Drupal

3- Create your site's config folder in the sites/ directory. Just copy the default/ directory and call it mysite.com/, then rename default.settings.php to settings.php inside mysite.com/. This should be the path of your resulting config file:
drupal/sites/mysite.com/settings.php
duplicated from
drupal/sites/default/default.settings.php

4- To use PHP5 to parse .php files add this line to the top of your .htaccess file:

PHP: Breaking Out of Multiple Nested Foreach and While Loops

I'm always encountering problems that require multiple nested foreach loops but I always devised ways of avoiding these nested loops. I avoided them because I needed to break out of the outer loops and wasn't able to.

What I needed was *continue N;* and *break N;*

HTML Select Box: How to get the value from a pull-down field onchange

The onChange event handler can be used to get the value from a select input list in several ways:

Using plain Javascript:
<select onchange="alert(this.options[this.selectedIndex].value)">
<option value="status">Status</option>
<option value="type">Type</option>
<option value="quantity">Quantity</option>
<option value="date">Order Date</option>
<option value="order_number">Order Number</option>
</select>

MySQL: Delete duplicate entries

I always encounter database tables with duplicate data. I used to create a function using PHP to compare each row with the rest of the table entries but recently I've been using this SQL query:

delete from books
USING books, books as vtable
WHERE (books.id > vtable.id)
AND (books.title=vtable.title)
AND (books.author=vtable.author)

1. Here you tell mysql that there is a table named "books".
2. Then you tell it that you will use "books" and a virtual table with the values of "books".

MySQL INSERT WHERE NOT EXISTS

Sometimes you need to check to see if the record is a duplicate before inserting it into the database. The simplest way is to do a SELECT query first and if that query returns a row, skip the INSERT query.

INSERT INTO table (field) VALUES ('value') WHERE NOT EXISTS (SELECT id FROM table WHERE field='value')

This query makes total sense except that it returns an error.

The proper way of doing this using a single query is by utilizing the DUAL dummy table, as so:

Xpath Encoding is Creating Weird Charters

If you're printing out variables directly to the browser that have non-english text you will probably see unreadable gibberish. This is probably not the encoding process, such as Xpath, that you may be using but just your browser. The real character is still there, but your browser is translating it incorrectly. So all you need to do is tell the browser what character set to use.

In PHP Just use:
header('Content-Type: text/html; charset=utf-8');

or you may use the equivalent meta tag:
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">

How to Remove Dotted Border from Links

You may notice this more on image links than text links because it stands out like a sore thumb. Whenever you click on an image link it displays a dotted border around it and when you hit the Back button to return to the page the border is still there. This is annoying because it makes your slick design a little less slick, specially in a dark theme. This is supposed to be a usability "feature" in Firefox, but it is more annoying than usable.

To remove this dotted border from your image and text links add the following to your css:

a:focus
{
outline: none;
}

How to Remove the Image Toolbar in IE 6

This image toolbar appears in IE 6 whenever you hover over an image for a couple of seconds. It lets you save, print, resize and send the image to a friend. Although you cant prevent someone from doing what they please with your images you can keep your interface/design looking as slick as possible by disabling the image toolbar. You can disable the image toolbar globally or disable it for specific images. The code is below:

Disable the image toolbar for specific images:

<IMG SRC="image.jp GALLERYIMG="NO">

Drupal Installation and Configuration Checklist

This is a quick checklist of tasks that you should do before and after the installation:

Turn Register Globals Off
- Add this line to the top of your .htaccess file: AddType x-mapp-php5 .php

Add Clean URL Support (on 1and1 and other hosts)
- Uncomment this line in your .htaccess file: # RewriteBase /

Get Language/Translation pack
- http://drupal.org/project/Translations

Set up default email addresses
- noreply@example.com, admin@example.com

Create database and user

INSTALL

Update User Settings
- If you have a private site disable the user signup

Using Clean URLs with Drupal on 1and1

Clean URLs are very useful for SEO and make a site look a bit more elegant, as well as giving the users a sense of where they are even if its not a physical location.

The trick to getting clean URLs to work on 1and1 is to go into Drupal's .htaccess file and uncomment the following line:
# RewriteBase /
should be
RewriteBase /

or if you have an older version of Drupal:
# RewriteBase /drupal
should be
RewriteBase /

You have to do this because your site is running in a VirtualDocumentRoot at http://example.com

Syndicate content