Archive for the ‘Programming’ Category

Disable WSDL caching in PHP

Monday, September 22nd, 2008

If you are finding that your SOAP web service isn’t updating when you make changes you have to add the line ini_set("soap.wsdl_cache_enabled", "0"); to both the Server and the Client.

i.e.

<?php
class SoapController
{
 function HelloWorld($name)
 {
  return "Hello ". strrev($name) ;
 }
}
ini_set("soap.wsdl_cache_enabled", "0");
$server = new SoapServer('http://example.com/soap.wsdl');
$server->setClass("SoapController");
$server->handle();
?>

and

<?php
ini_set("soap.wsdl_cache_enabled", "0");
$client = new SoapClient('http://example.com/soap.wsdl', array('trace' => 1));
print_r($client->HelloWorld("Damian"));
?>

Escaping a Plus in Javascript

Friday, September 19th, 2008

I got stuck trying to send a string that contained a “+” via Javascript to a Flash component. Using “escape()” didn’t help as it continued to come through as a space. The workaround I discovered is to use “encodeURIComponent()” instead.

Pointy

Tuesday, September 16th, 2008

I do web development for a living. Almost two years ago I made the switch from being almost exclusively Microsoft and .NET to open source and along the way had to learn PHP (and a bit of Python for a while there too).

I’ve played around with many different web frameworks and been frustrated that they often force you to learn an alternative to SQL and/or some kind of restrictive templating language as well as the fact that most of them are too cumbersome for the types of sites that I’m generally required to produce.

So, for the last six months or so I’ve been refining my own MVC web development framework with the goal of keeping it lightweight, high performance and only using standard PHP and SQL where needed. And most of all, open source.

I present my baby, Pointy (the tiny but sharp development framework), which I’ve made available under a Creative Commons license. If you’re a PHP developer and you want to dabble with Pointy, please feel free to pay a visit, download it, play with it and if you find it useful go ahead and use it. I hope it’s as useful to you as it has been to me.

Calculate Your Blogging ROI

Friday, May 9th, 2008

Ever wanted to know what blog posts you write require the least effort and get the most comments? No? Well I did and I threw together a bit of SQL to help me identify the areas I can improve upon if I’m to become a serious challenger for the title of the Laziest Blogger Ever™:

SELECT p.post_title, ROUND((SUM(LENGTH(w.comment_content))/LENGTH(p.post_content))*100) AS roi, LENGTH(p.post_content) AS post_length, SUM(LENGTH(w.comment_content)) AS comment_length FROM blog.wp_posts p INNER JOIN blog.wp_comments w ON w.comment_post_ID = p.ID GROUP BY p.ID ORDER BY roi DESC LIMIT 10

It returns the top 10 blog posts ranked by the percentage return on a post measured by the number of characters invested in the opening post compared to the number of characters returned in the comments.

My top 3:

  1. Free Will (43,774% ROI)
  2. Last Western Heretic (5,304% ROI)
  3. The Location of Jesus (5,223% ROI)

CSS max-width proportional scaling in IE6

Wednesday, March 19th, 2008

The CSS property ‘max-width’ and ‘max-height’ doesn’t work in Internet Explorer 6. Here’s a workaround:

.myclass
{
width:expression(this.width > 100 ? (this.height > this.width ? (this.width / this.height) * 100 : 100) : true);
}

What does this do? It executes a bit of javascript within the CSS that goes along the lines of “If the width is greater than 100 pixels then set the width to 100 - unless the height is greater than the width, in which case scale it down to the difference between the width and the height - otherwise just leave it as it is”.

This means that any element that is of .myclass will fit proportionately into a 100×100 area (change the 100 for whatever max-width or max-height you require).

I hope this saves someone from having to figure this out in the future.

Some Handy Regular Expressions

Saturday, February 9th, 2008

At least something
/.+/

A positive integer
/^\d*$/

A decimal number
/^\d*\.?\d/

A valid email address
/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/

A valid web address
/^(http:\/\/)?\w+([\.-]?\w+)*\w+([\.-]?\w+)*(\.\w+)+(\/)?(\w+)?$/

A hexadecimal colour (6 chars)
/^#?[0-9A-Fa-f]{6}$/

How to use in Javascript:
var testString = "ff0000";
var pattern = /^#?[0-9A-Fa-f]{6}$/;
if (testString.match(pattern)) DoSomething();

How to use in PHP:
$testString = 'ff0000';
$pattern = '/^#?[0-9A-Fa-f]{6}$/’;
if (preg_match($pattern, $testString) DoSomething();

How to use in C#:
string testString = "ff0000";
string pattern = @"^#?[0-9A-Fa-f]{6}$”;
Match m = Regex.Match(testString, pattern);
if (m.Success) DoSomething();

How to increase the number of recent comments in WordPress

Monday, January 21st, 2008

If you have direct access to your database you can change the number of recent comments that are displayed on your homepage in WordPress. I use phpMyAdmin to manage my database. Here’s what you need to do:

  1. Go to the wp_options table in your database
  2. Browse the data in this table and find the option with the name ‘widget_recent_comments’
  3. Edit this option and change the last number (it should be ‘5′ by default) to the number of recent comments you would like to display: a:2:{s:5:"title";s:0:"";s:6:"number";i:5;}

Of course, it’s quite likely that there is a setting for this in the admin panel that I’ve overlooked - I was unable to find it.

Scrollable Divs

Friday, January 11th, 2008

Kingston Flyer

I’ve been HTML-ing and CSS-ing for years now and had never come across this before: To make the content within a div tag (or any other block tag) scrollable all you need to do is fix the width and/or height and add overflow:scroll; to the style for the tag.

You can also set individual scrollbars with overflow-x:scroll; or overflow-x:hidden; and overflow-y:scroll; or overflow-y:hidden;.

I can’t believe I missed out on this one. Perhaps I need to go back to the CSS specification and work through each attribute one by one.

Search and Replace text in files with Python

Thursday, November 29th, 2007

#!  /usr/bin/python2.5
import os

mydir = "/path/to/directory"
mysearch = "text to find"
myreplace = "Text to replace"

def doReplace(filePath):
    fin = open(filePath, "r")
    s = fin.read()
    fin.flush()
    fin.close()
    fout = open(filePath, "w")
    s = s.replace(mysearch, myreplace)
    fout.write(s)
    fout.close()

for root, dirs, files in os.walk(mydir):
    for f in files:
        name, ext = os.path.splitext(f)
        if ext == '.html':
            doReplace(root + '/' + f)

Explanation: This will find all files ending in .html in the directory specified in mydir along with all matching files in any subfolders and will replace the text specified in mysearch with the text in myreplace. It’s only been tested on Linux but with a bit of tweaking will run on Windows and Mac.

The Problems With MVC Frameworks

Wednesday, October 3rd, 2007

After experimenting with CakePHP, Zend, Ruby on Rails, Django, Turbogears, Pylons and DotNetNuke I have given up on lumbering MVC frameworks. The kind of work I do is either too small or too specialised and using a MVC framework is either massive overkill or I have to spend days trying to hack the code to join a database table in just the way I want it or connect to a webservice.

This kind of sweeping statement is not going to earn me a lot of friends. The people who are into these frameworks are devout to say the least.

Probably my biggest gripe is that the whole idea of a MVC framework is to have separation of Model, View and Controller but if you ever build a site using one of these frameworks and attempt to uncouple these components you’ll quickly see that the touted separation is not all it’s cracked up to be.

The idea of having a templating language is inspiring but the fact that there is no defined standard for templating means that Smarty only works with PHP, Kid only works with Python and so on. I take it back; XSLT is a standard but it’s got to be the ugliest, most convoluted language out there.

Defining your model is exciting when you’re starting a Hello World project from scratch but can be pretty tiring when you’ve had to make it fit an existing database that doesn’t conform to the pluralised, *_id-ised requirements of your particular framework. And on top of that you have to relearn your particular framework’s substitutions for the SQL you’ve already had to learn. “DRY” anyone?

Don’t get me wrong, the MVC way of working makes a lot of sense. I use mod_rewrite call a Controller file which in turn feeds data from my Model class to my View class. But I don’t make my Model speak anything other than SQL if I’m accessing a MySQL database and I use <?php ?> blocks in my templates if I’ve chosen PHP as my language du jour. Why learn two additional, less powerful languages?

I agree that there are many situations where you would be better off developing a large website using a traditional MVC framework but I would be under any illusion that it’ll be any more flexible a year down the track.

For me, redemption would come in the form of a standardised templating language and a standardised set of classes for models that easily integrate into existing databases as well as being able to generate new ones.

If you’re creating a small or a fiddly website then I would advise you separate your data from your logic from your presentation but steer clear of the lumbering behemoths that occupy the MVC space at the moment.