Monday 24 March 2008

Google Maps on the fly

Adding Google Maps and dynamically filling it with goodies you have stored in a database is an exciting thing, but there are a number of alternatives available to you when deciding to do this. Firstly, GDownloadUrl(url) Can be used to go off and get a remote file for you. This remote file can be an XML file and parsed using the GXml() although this then leads to navigating the DOM to extract the data you want. Secondly, if the XML file your downloading is more specifically a KML file (very possible considering you may be producing that file from your database). You may want to just use the GGeoXML() class. This class can be passed a KML directly and theoretically get displayed on a map using map.addOverlay(GGeoXML). However, this doesn't give you access to a function you may have to create (and therefore return) a marker. This function is useful because it allows stuff to be bound to particular marker instances (like code for click events). With GGeoXML you loose this.

I keep seeming to start by plotting an arbiturary XML file full of <marker> tags. I then decide to use a standard XML file like KML, get satisfied, then remember the GGeoXML which could have done most of it for me.

Javascript: Binding and Scope

Here's quite a cool help post on the mootools forums about in Javascript.http://www.blogger.com/img/gl.link.gif

Saturday 22 March 2008

Printing Arrays in PHP

always use print_r(); to debug an array. I guess echo and print just produce array.

When displaying stuff in web pages use a foreach loop.

VirtualHosts in Apache, Leopard

There's a few key files when playing with Apache WebServer. They are all .conf files, the most important being http.conf. In Leopard this is at etc/apache2/. Editing this file is pretty straight forward, it's very templatey - find out where it's done once, then copy, paste and edit.

Virtual hosts are a good thing to set up if you're working with a lot of websites. People talking about enabling the following line beginning with Include in the http.conf file:

# Virtual hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf

This is fine, but it doesn't really do anything, it just gets Apache to pull in that extra configuration file when starting up. In fact, you can keep that file commented out (not used) and put virtual directory right there, in the http.conf file. Here's the basic code for setting up a virtual host

<VirtualHost *:80>
#ZDS Name: redmonkey
ServerName redmonkey
DocumentRoot "/Users/michele/Sites/redmonkey"
ServerAlias redmonkey
ErrorLog error.log
Options Indexes MultiViews Includes
</VirtualHost>

I'm sure loads can be changed, and I'm not sure what all of it does yet, but get this in and edit the rest.

Once this is done, you need to setup your hosts file. This can be found in /etc/ and it's simply called hosts (note: no file extension). Here add the following:

127.0.0.1 redmonkey

The IP address is equivalent to "localhost" and then a name of your new hosts. From then on you should be good to go with your new hosting directory in Apache on Leopard.

Thursday 20 March 2008

Unix Aliases

Absolutely awesome. If you have a command you use over and over again, chuck it in a login script and give it an alias.

When using the bash shell, include aliases in ~/.bash_login (create the file if it doesn't exist already). Some useful aliases I've already added in macOS are the following:
  • alias ll='ls -la'
  • alias clr='clear'
  • alias here='open -a finder .'

The last one is really cool. It opens a finder window at the current terminal location.

MySQL wrapper class

I started writing a wrapper, then I found this PHP MySQL wrapper class prebuilt. It seems very good and the site includes lots of cool example of how to use it. Why bother trying to re-invent the wheel? Well done guys.

SQLite

SQLite seems to be an alternative to MySQL. Sure it has many benefits, by i'm sticking to MySQL for the time being.

Apparently it's fully OOP though so you can do cooler things with exception classes, and it comes with PHP 5.

Useful Links

You may want to re-write the blogs html and put some cool useful links in there, and you could make it look a bit better.

Also Im using the google reader, you subscribe blogs to ur reader and it consolidates and displayes all unread blog posts. You can then throw a google reader widget onto ur igoogle page and see all the new posts from there. I should'nt miss any posts now...

Smarty: A PHP template Engine

Ok Smarty. All you really need is everything in the libs/ directory. When copying in unix, remember to add option -r to go into the internals/ and plugins/ directories within libs/. With those files saved somewhere, your PHP has to find one file in particular-Smarty.class.php (they also seem to be quite strict about the capital "S" in "Smarty"). That file needs to be referenced with an absolute path. This can be done a number of ways, look them up at Smarty's documentation. One cool way I've found so far is this:

// Use the absolute path for Smarty.class.php
$base_path= basename(dirname(__FILE__));
require($base_path.'/Smarty/Smarty.class.php');

Of course this assumes that you're writing this in a file which is in the document root of your webserver, and that the Smarty files are located further down in the folder Smarty/. The require_once is similar to PHP's include statement apart from it will result in an error if it can't find the file (include will just try and not care if it can't) and the _once part ensures there's no errors if you accidentally try to require it again.

Within that Smarty directory, make four folders: templates, templates_c, cache and configs. Link these files up with your code with

$smarty = new Smarty();
$smarty->template_dir = $base_path.'Smarty/templates';
$smarty->compile_dir = $base_path.'Smarty/templates_c';
$smarty->cache_dir = $base_path.'Smarty/cache';
$smarty->config_dir = $base_path.'Smarty/configs';

Put this immediately after the required_once statement. Then start making templates and go.

Wednesday 19 March 2008

PHP: the ? operator

The ? in PHP is shorthand for an if..else statement. It works like this
{expression} ? return_when_expression_true : return_when_expression_false;
Notice the ? and the : to separate everything apart.

Casting in PHP

Casting seems relatively straight-forward:
$test = 1234;
$test_string = (string)$test;

Simple.

Unix pipes and other commands

Pipes "|"are really cool. They redirect output from one command and pass it as input into the next command. A useful command to use in this situation is grep which pattern matches line by line and displays that line if the match succeeds, but grep -v inverts how the command works, and displays every line that doesn't match the pattern, making it useful as a filter...very cool.

Tuesday 18 March 2008

MySQL basics

The USE database command sets the default database for your stuff. All operations from that point on are applied to that database. When declaring numeric types, a number in brackets called a width specifier which tells MySQL to pad the field when retrieving it. With floats and decimals a precision parameter can also be specified such as FLOAT(7,4). Marking a field with the ZEROFILL attribute will force MySQL to pad the value with zeros. UNSIGNED forces only positive numbers.

The alter can be used in a number of different ways to change name of fields within a table. If you try to convert field types using CHANGE, MySQL may result in errors. Try using IGNORE to get round them.

Use mysqldump to backup and restore databases or tables. Delete stuff from databases and tables using DROP.

Hello Michele!

Im now an added contributer to the blog

.inc files and libraries

Use require_once() to include files to use as a library. These files get interpreted by the interpreter but aren't called directly. They are conventionally given a .inc file extension, and they allow you to call functions and stuff hidden inside them. Quite useful.

Monday 17 March 2008

mod_rewrite

First thing to get your head around is the fact that it's kinda backwards. Messy URLs still exist, you're just translating them from nice to messy for your code to understand.

After that, it's quite simple really. Regex expressions: from, to. Use ^ and $ to mark the beginning and the end of the expression to match. Here is a good mod_rewrite cheat sheet although there's loads of help online. Try to set a RewriteBase to set the base directory within the site

the [R] flag will redirect the user and the URL in the browser window will change automatically. The [L] flag will redirect behind the scenes but the users won't know. Use this for query-string stuff. Remember to turn the engine on (as it's off by default) with RewriteEngine on.

The only other thing I've discovered so far is there seems to be a default index being made by Apache which I haven't been able to disable yet. This default indexing causes Apache to ignore my rules and display any file in a directory by default if they share the same name i.e. if I have rules like

RewriteRule ^$ catalog/ [R]
RewriteRule ^catalog/$ hello.php [L]

and within the directory there's also a file called catalog.php, Apache seems to be ignoring the redirect to hello.php and just displays catalog.php as the index. I know this because if I change the filename and the redirect directory to something else I have the same affect. I'm still working on this, no idea how to do it.

All of this can be written in a .htaccess file.

Apache settings

Main files: httpd.conf and .htaccess. The first sets some site-wide settings. It seems to be similar to web.config in .NET. .htaccess works on a per-directory basis.