PHP aaaaarrrrrggggghhhhhh

Things I hate about PHP No. 512 (notwithstanding the fact that in comparison to many other things I like PHP):

Inconsistency in parameter ordering

Say I want to find a small thing in a big thing, like a piece of a string in a bigger string. I can use strpos and you pass in the string you’re looking in first, and the string you want to find second. So strpos(“PHP aaaarrrgggh”,”PHP”) returns 0 (or false, unless you’re using ===, but I’m sure that’s been ranted about enough).

But if I want to find, for example, a key in an array I pass in the key I’m looking for first and the thing to look in second. So I have to remember the exact syntax of every single miniscule stupid little command and which order to pass parameters in because there’s absolutely no consistency.

Opacity in Firefox, Safari and IE

Internet Explorer has suppored opacity for a long time but it’s also possible to change the opacity of a layer in Safari, and both newer and older versions of Mozilla (including Firefox).

The code for setting the opacity of a layer to 50% (in JavaScript) is:

  • For IE/Win: layer.style.filter = “alpha(opacity:50)”;
  • For Safari (pre version 1.2), Konqueror: layer.style.KHTMLOpacity = .5;
  • For older versions of Mozilla and Firefox: layer.style.MozOpacity = .5;
  • For Safari 1.2, newer Firefox and Mozilla using CSS3: layer.style.opacity = .5;

The main difference now between IE and the other browsers is that in IE opacity is specified as 0 to 100 whereas in Firefox and Safari it is a decimal from 0 to 1.

A new design for this blog

It’s taken a few evenings and bits of the weekend, but last night saw the launch of a new design for this blog. What with the categorisation I’d added recently, the right hand column with all the navigation in it was getting a bit cluttered so I’ve now gone 3 column and hopefully everything fits at an even height on the screen a bit better.

The main thing of note is the large background image, though, which is randomised so you should get a new one each time you view the site (the current image is stored in a session cookie) – close down your browser window and launch the blog again and it should give you a new image. The images in the library will grow over time as they’re just being pulled randomly out of a single directory. Alternatively you can click on this link to get a new image. At some point I’ll add a button to the site to do that.

Change the background image

More JavaScript haiku

I first published a JavaScript haiku called ‘Fear of tomorrow’ here. Well, I’ve written some more. Apparently these are more likely to be classed as Senryuu, but what do I know. Anyway, here are some others that I’ve written, with the code first and the way it’s supposed to be read following it, and then an explanation of the ‘meaning’:

A baker’s dozen?

var i=1;
while(i<13){
 alert(i++);
}

var i equals one
while i is less than 13
alert i plus plus

Although on the face of it just a script which displays numbers from 1 to 12, there is some ambiguity of meaning in the use of i++ which causes the reader to think and question whether the value of i will be alerted before being incremented or the other way round. It is, of course, alerted before being incremented but this moment of doubt reflects the author’s questioning of the logic behind the term “baker’s dozen” (being 13) and the discord it has with a ‘true’ dozen of 12 and the indecision as to which one to count up to.

The value of being together

i=.5;
u=Math.random();
alert(u+i);

i equals point 5
u equal math dot random
alert u plus i

This short piece raises questions of the value of two people joining together and whether the sum is greater or smaller than average. The first person (i) is an ‘average’ person and if the second person (u) is of above average value then the total will be greater than one. If not, then the value will be below one. This poem can be applied to anyone’s life and if you give yourself a score of 0.5 and then rate your partner accordingly the same equation can be used to determine whether you should dump them or not.

Eight days of sailing

var course="straight";
navigator==true;
alert(course.length);

var course equals straight
navigator equals true
alert course dot length

A straightforward nautical tale where the navigator confirms that the voyage will consist of eight days of straight sailing.

More dubious works will doubtless follow…

PHP 4 foreach as references

PHP 4 doesn’t seem to create references for objects in foreach loops. e.g. the following will not change the original objects:

foreach($placeholder as $contentBlock)
{
 $contentBlock->setPosition(1);
}

The value of the position property in the original object will remain unaffected. This is quite rubbish. To get round it you need to get all the array keys and then create a separate variable to store a reference to the object you want to change:

foreach($placeholder as $key => $value)
{
 $contentBlock =& $placeholder[$key];
 $contentBlock->setPosition(1);
}

The variable $value isn’t used at all but if you’re stuck using PHP 4 then this is the best you can do. Luckily PHP 5 works much more sensibly, but we don’t always get a chance to use that so I’m stuck with this slightly messy alternative.

Foreach in the PHP docs

PHP 4 annoyances ( = rant)

After working with PHP 5 for quite some time now, I’ve had to go back to PHP 4 to develop a CMS for a client’s site where we don’t have much over the hosting environment. Going back to the old version has annoyed me quite a few times already and it’s probably made worse by the fact that I never really did much PHP 4 anyway (I’m quite new to the language, even though I have been programming in it on and off for 2 years) and lately I’ve been working in a combination of PHP 5 and C# (as well as various others) and generally being a bit more object oriented about everything.

First off there’s the whole not really object orientedness of PHP 4. PHP 5 isn’t that object oriented, but PHP 4 is miles off. Passing variables by value instead of reference unless you put stupid & symbols everywhere… whose idea was that? Also it’s obviously a bit hacked together behind the scenes, as exemplified by the fact you can’t do things like $this->getThing()->getOtherThing() without it barfing. Instead you have to put the returned value from the first thing into a variable and then call the next method… not a good sign in an interpreter.

You can just about force yourself not to access member variables of objects directly, even if you can’t set things to be protected or private, and make get() and set() methods everywhere, but sometimes it’s really useful to put things into static classes. Database routines would be really useful, for example. Instead I’ve taken to creating a great big global variable to hold an object and refer to it everywhere and use the member variables of that object to store other useful objects. At least methods can be called statically, but without static member variables they’re not as much use. (I use a similar technique when programming in Lingo to save having to reference all my globals in each script: store them as properties of another object that keeps state for the whole application.)

And very little XML support either… Even ASP could handle XML well, and even XSL, four or five years ago, and ASP as we all know is the lowest form of programming language ever invented. (Okay, there are worse languages than VBScript, but not many that site behind so many important web sites. I once worked on a half million pound project that was built in ASP… Scary!)

Rant ends

Adding categories to Blogger blogs

27th September 2005 update

I’m trialling a new piece of software called Caterblogger. It will add categories to any Blogger blog. You can register interest in it on the site.

The original post follows below:


Blogger is really ease to use and set up, and manages individual posts really well. It’s also useful to be able to send posts in by e-mail, and there are quite a few different ways of configuring how posts are displayed. With some HTML knowledge, the template can be made to look exactly how you might want it. It also manages things like archiving particularly well, and the way posts are published with filenames matching the page title (mostly) is really good for being found in the search engines. What it doesn’t do, though, which would be really useful, is to allow posts to be put into categories. I was keen on implementing it on this blog, however, and thought that with a bit of code I could work it out. So I did, and the results can be seen on the right hand side of the page under the ‘Categories’ heading.

It’s not exactly straightforward, though. To get it working using the method I’ve used you’ll need the following:

  • A blogger account (duh)
  • Your own hosting that supports ASP.Net pages, where you can also change file permissions to allow IIS/ASP.Net to write to the file system
  • To get it working without recompiling, the pages will need to be served from the web root (since I haven’t bothered adding anything to web.config, which I really should do…)
  • Some knowledge of ASP.Net and preferable C#
  • The ability to edit your own blog’s HTML templates
  • Not be afraid of embedding XML in your posts, and troubleshooting errors
  • To customise the category layout: knowledge of CSS and, preferably, XSL

So, it’s not for the unprepared. I mean it – without a fairly good idea of what you’re doing, you’ll probably end up doing more harm than good. (I’d recommend installing it all locally first either way.) I’m also unlikely to be much help if something doesn’t work, so this is all at your own risk.

The principle behind my Blogger categories

I’ve written the code to make it as simple to put posts into categories as possible once the template has been set up. A post is put into a category by placing an XML tag into the posting text. The format of these categories is


Once a new post is published it is posted to the homepage of your blog by Blogger. My code registers that the blog content has changed and parses through all of the postings in the various subdirectories (by looking at files in subdirectories ending in .aspx and not containing the word ‘archive’ in the filename). It looks for the

tags and pulls out all of the category names that are contained in it and builds a new XML document containing a list of all of the categories and all of the files that are within each category. Each post can be in multiple categories.

The advantage of this is that categories don’t need to be set up beforehand – as soon as a post is published in a category, that category is created. The downside is that you need to make sure you spell the categories consistently.

Setting up Blogger categories

The files that are needed are in the zip file which you can download here. You’ll need to make sure that either IIS or the ASP.Net process has write permissions to the root of the blog, depending on the user that IIS is being. If you get a file permission error and change web.config to show you what’s going on then you’ll see which one you need to set up. Otherwise, as these files are, if deployed correctly onto a new web application at root level they should work straight out of the zip. However, you’ll get my blog content (as captured at a point in time since index.aspx is not going to update) with links to my images. If you get a compilation error at this stage then fix it now before you go any further. (Possible errors would include file write permissions or path problems. Anything else may be more serious.)

Once the code runs without your blog being connected to it, what you need to do to get it working is:

  1. Add code to your own Blogger template
  2. Update the HTML of postsByCategory.aspx to suit your own blog
  3. Change your Blogger settings to publish all files out as .aspx
  4. Add category XML to your posts
  5. Republish each post of your blog
  6. Check for XML errors and tidy up
  7. Update the category listing XSL files to match the look and feel of your blog.

1. Add code to your own Blogger template

Firstly, the page needs to be made a true aspx page. Add


to the top of the page.

Next, the category list and other information needs to be generated within each post. A small piece of code needs to be added to your posting body. This looks like the following:

" />
" />

And is wrapped around your existing tag.

What this does is wraps the whole post in an XML block (this will create invalid XHTML, but Blogger does that anyway most of the time) and add some extra information such as the title and the date of the post as XML nodes containing Blogger tags.

You’ll also need to add an asp:Literal to your template to display the categories list. This is a simple tag which can be added wherever in the HTML you want the category list to appear:


2. Update the HTML of postsByCategory.aspx to suit your own blog

postsByCategory.aspx is a standalone file so updating it should be an easy HTML job. You shouldn’t need to change any .Net code

3. Change your Blogger settings to publish all files out as .aspx

See my previous post Publishing with aspx extension from Blogger.

You’ll also need to change the post timestamp format to English date, e.g. 27.07.05 (for 27th July 2005), as this is used for ordering posts (in reverse date order) in the category list. This is found in the Blogger console -> Settings -> Formatting as Timestamp Format.

4. Add category XML to your posts

As mentioned earlier in this post, to put a post in a category you just need to type

(where ‘Category name’ is the name of the category you want to appear) in your post. This can be anywhere within the text of the post. I tend to put it at the beginning so it’s easier to find, but it doesn’t really make any difference.

5. Republish each post of your blog

This is a pain but it’s the only way to get each file published as an aspx file. You’ll need to do it if you’re putting your posts in categories in any case. Each post will need to be republished individually – you can’t do a ‘Republish all’.

6. Check for XML errors and tidy up.

Once you view any page on your blog, a file called categories.xml will be published in the web root. Download this and open it up in a text editor or any other viewer. As well as category nodes, there are likely to be a number of nodes – these show which files contain invalid XML in the posts so you should take a look at them and try and fix the error. The code has been written to skip any errors so you should get a category list generated as long as at least one post is valid. The file should be generated regardless since it also contains error nodes. Errors are often caused by invalid characters in post bodies (e.g. pound symbols not being inserted as character codes) or, slightly more annoyingly, double quotes in post titles (since the post title is contained in an attribute node in the template in our little XML block).

7. Update the category listing XSL files to match the look and feel of your blog.

This should be fairly straightforward even if you don’t know XSL well, just don’t break the templates. The HTML is very simple and you may even be able to get away with just changing the CSS instead.

That should be it

In theory, that should all work. In practice, I’m willing to be about 75 things have gone wrong so far and now you wish you’d never started. For the really keen (or more likely for bug-fixing) you can download all the source files here (and let me know if anything’s missing). Best of luck!

Download the Blogger categories compiled files
Download the Blogger categories source files

Publishing with aspx extension from Blogger

I wanted to publish my blog with something other than the .html extension and after some experimentation and hunting around I found this:Blogger Forum – Forum

The way to do it is to change the main page extension (from index.html to whatever, e.g. index.aspx) and do the same with the archive page. Then you need to go into each post individually and re-publish it. It’s a bit of a pain, but does work.

A handy tip: download all of your pages (in html format) first and then re-upload them afterwards so that all inbound links actually go somewhere.