10 Nov

scramble solver

A few days ago I wrote a WordDrop solver. Today, I’ve amended that code to produce a Scramble solver.

here it is (code) – what I do is to have that page open, and the Scramble game open, in two windows side-by side. Then start a new game, type the letters into the top input box. That’ll fill in the grid. For “qu”, type “q” in the input box and correct it in the grid. Then click “do it” and cheat your way to a high score.

Also discovered a small bug in my binary search algorithm 😉 I’ve fixed it in this new script but couldn’t be bothered with the last one – I’m done with that.

09 Nov

cheating at worddrop

Yesterday, I cheated at WordDrop, which is a Facebook game. I already had the highest score of my own network of friends, so I feel okay about this. But now that I’ve cheated, the game’s no fun any more!

WordDrop is a game where an 11×8 grid of letters is shown, and you build up words from that grid, where each letter should be linked to the one before it, with longer words getting higher scores.

I wrote a little script which basically plays the game itself, and comes up with the longest words possible.

My current score on the game is more than 500,000, and that was for a single 10-letter word. The second word the program came up with was “reselected”, which was not in the WordDrop dictionary. I just stuck with the first word and submitted that. With one word, I got a score that was about 8 times higher than my previous high score.

my script, and all files – don’t worry about the .php file – I just wrote a small scripty bit to print out the table – just download the HTML and JavaScript if you want your own copy.

26 Oct

jQuery 1.3 With PHP: Datatables chapter is online

Packt have put another chapter of my book online. This time, it’s the datatables chapter, which discusses how to use the Datatables plugin to organise very large data tables.

The chapter discusses how to make a large (over two million rows) database usable through an ajaxified table.

The Datatables plugin supports search, sorting and pagination.

24 Oct

jQuery 1.3 With PHP: calendars chapter is online

Packt have put one of the chapters of my book, jQuery 1.3 with PHP, online as an article.

I mentioned this chapter in June, and provided a small demo and the source.

I’ve had a few requests for example code on how to do this using a real database. I’ve been meaning to make the time to do that. I’ll get that done hopefully tomorrow.

In the meantime, though, please enjoy the article.

By the way, the book went to the printer only a few days ago, so if you want to avail of the pre-order price, now’s the time.

27 Sep

KFM 1.4 released

It’s been almost a full year since the last release, 1.3.1, and I haven’t done much on KFM in that time.

Benjamin ter Kuile, though, has! Today, we release KFM 1.4

In the time that I’ve been busy with other stuff, and procrastinating on KFM, Benjamin has been working diligently, adding features and making the core files more stable.

Here is a short list of improvements, gleaned from the SVN logs over the last year. I’m sure I’m missing a lot.

  • jQuery upgraded to 1.3.2 (faster, better, stronger)
  • you can edit multiple text files simultaneously
  • context menu code improved to help it work in Chrome, Opera and Safari
  • file icons are now created as a montage, improving display speed
  • a lot of functions converted to “lazyload” – improves startup speed
  • images are also lazyloaded – if an icon is not visible on the screen, its thumbnail is not loaded
  • lots of bug-fixes

Not many new features, as KFM was basically complete.

1.4 is the last 1.x version which will be released. We’re done with the code-base, and ready to start on a much more ambitious project.

We’re going to rebuilt the entire system from scratch in a modular way, adding functionality that would be very difficult to shoehorn into the present system. The new version will be called KFM 2, and I’m really excited about it. I will be starting the coding for it very soon, as soon as I’ve finished with the release of my book, jQuery 1.3 with PHP.

For those of you that are already using 1.3; 1.4 is /faster/, but 1.3 already completes the initial spec, so only upgrade if you find that 1.3 is slow. New users should install 1.4.

18 Sep

jQuery 1.3 With PHP: cover mockup

6989_MockupCover

I’ve been sent a mockup for the book’s cover. The suggested title of the book is “jQuery 1.3 with PHP”. The working title was “PHP and jQuery”. Which do you prefer?

The book has been completed, in that all the chapters are written, and it’s in the final edit phase at the moment. This involves Packt having a technical editor try everything in the book just to iron out any kinks. It’s already been gone over by three other reviewers, and the only problem appears to have been with the File Management chapter, where the web-server was IIS on Windows. That should be solved by the time the book comes out.

I’ve learned a lot while writing this book. A major point that keeps raising its head is that I keep using colloquialisms and aphorisms (ha! “raising its head”), and those are not globally understood. Another is that I keep using British spelling, but it’s expected that most readers will be American.

From a coding point of view, I tend to write compact code with comments only appearing where something is obviously confusing, but I’ve tried to put proper comments in the book whenever any reviewer asked a question about the code.

Anyway – I expect it will be in PDF form in only a few weeks! I’m looking forward to hearing what people think of it.

On a funny note, I was working on something in work recently, and was trying to figure the best way to do it, when I suddenly remembered I’d written a whole chapter on it, so went and read what I’d written! I’ll be keeping a copy of the book on my own shelf 😉

This kind of thing is always happening to me – I would need to solve some problem (hooking an OKI B2200 printer to Linux over Samba, for example), go searching for the answer, and find that I’d written the solution for it a year or two previously…

By the way, KFM 1.4 will be released next week. It will be the last 1.x version. We (Benjamin and myself) are starting a total rewrite after that, which will become KFM 2. It’s going to be massive!

EDIT: 2009-09-18 Wow, that was quick! The book is already available to pre-book

22 Aug

formula for figuring out keys

I was just looking at scales and trying to memorise how many sharps and flats each key has.

In short: count the sharps. multiply by 7. divide by 12. add the remainder in semi-tones to C. that’s the key. For flats, subtract the remainder from C.

The usual way to do this is to use the “circle of fifths” diagram:

In that, you count clockwise from C to find keys with sharps in them, and anti-clockwise for flats.

For example:

This key has 3 sharps, so you count three keys to the right of C, and that makes it A major.

If you want the minor key, then do the same, but then subtract 3 semi-tones from it, and you get F#m.

Now, what if you’ve a crap memory like me? There’s no bloody way I could remember a complex diagram like that.

Simple – let’s look at another variant of the diagram:

In this diagram, in order to figure out the key, you follow the lines; anti-clockwise for sharps, and clockwise for flats. For a key with 3 sharps, you start on C, then follow the lines 3 times, through G and D to A.

Yes, it’s another diagram, but look at the lines – they’re perfectly regular, which means that a formula can be built from it.

The formula is simply this: sharps * 7 % 12. Then add the result in semi-tones to C and you get the answer.

Examples:

CC#
D♭
DD#
E♭
EFF#
G♭
GG#
A♭
AA#
B♭
BC
0123456789101112
  • 1 sharp: 1*7=7. 7%12=7. 7 is G
  • 2 sharps: 2*7=14. 14%12=2. 2 is D
  • 3 sharps: 3*7=21. 21%12=9. 9 is A

For flats, subtract the result from 12 to get the answer:

  • 1 flat: 1*7=7. 7%12=7. 12-7=5. 5 is F
  • 2 flats: 2*7=14. 14%12=2. 12-2=10. 10 is B♭
  • 3 flats: 3*7=21. 21%12=9. 12-9=3. 3 is E♭
02 Aug

jquery remoteselectoptions plugin

Based on some work I did for chapter 3 of jQuery 1.3 with PHP, I’ve created a plugin to encapsulate the remote selectbox trick.

The idea of this is that in a lot of cases, there may be huge select boxes in your forms, but the selected value might not ever need to change. For example, if you have a country list, and you’re pretty sure that the user is in Ireland, then it might be pointless to have a full list of countries in there if it’s unlikely to be changed.

demo
country-select

Notice that when you click the select box, it’s populated with countries. However, the source of the page does not include those countries in the HTML. So how does it do it?

How it does it is to add a focus event to the selectbox, which populates it only when it’s actually about to be changed. In this case, the missing options are in a file named countries.html which is 9k in size. So, by using this trick, we’ve saved 9k in bandwidth. Multiply that by the amount of large selectboxes in your forms, and it could be considerable.

Look at the source of that page:

<html>
	<head>
		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
		<script src="jquery.remoteselectoptions.min.js"></script>
		<script>
			$(document).ready(function(){
				$('select[name=countries]').remoteselectoptions({
					url:'countries.html'
				});
			});
		</script>
	</head>
	<body>
		<em>select a country</em>

		<select name="countries"><option value="IRL">Ireland</option></select>
	</body>
</html>

All that’s needed is to include a single option in there to indicate the default, then run .remoteselectoptions() on the selectbox, telling it what url it should grab the options from.

In case you need to do some checking on the server-side based on the current selected item, the script calls the option source with a selected GET parameter set to the current selected option’s value. You might set the url to a PHP script for example, which would build up the option list based on that selected item.

Download the source here (4.5k)

Rate it here.

23 Jul

jQuery: nested sortables

As I said in my last post, I was pretty sure that the code in my last chapter was correct and that the problem was with jQuery’s Sortable itself.

I spent the last few hours studying the Sortable code and think I’ve fixed it.

Here’s the example from last post showing the problem, and here’s the corrected version.

If you want to patch your own copy of Sortable, here’s my patch file.

I think there’s actually a moral here – if you want to make sure that something is fixed, then you should knuckle down and do it yourself, as the jQuery developers really do have better things to be doing.

Buy the book! jQuery 1.3 with PHP

22 Jul

jQuery 1.3 With PHP: chp8, drag/drop

For chapter 8 of jQuery 1.3 with PHP, I wrote about drag and drop. jQuery has a few different ways to do drag and drop. The most fundamental way is to use the Draggable and Droppable interfaces, but as it happens, most of the things you might want to do with a CMS can be done just with the Sortable interface.

The examples I wrote for this chapter are:

  • Sorting a list and saving the result. demo, download
  • Sorting a tree-based list (navigation menu, for example) and saving the result. demo, download
  • Dragging items from a list of contacts into a list of people to send emails to. demo, download

The second one worried me for a while, as it kept behaving erratically on me – I would sometimes drag an item and it would simply vanish. It turns out that it’s a bug in jQuery UI’s Sortable that should be fixed by the time the book is out, so it will work better then. I’m not worried about its behaviour right now as I’m certain this will be fixed.

I only used Sortable in this chapter, as I was trying to think of a case where Sortable couldn’t be used, and the only cases I thought of were a bit too advanced for the book – the book’s supposed to be for PHP developers that are not absolutely rock solid in JavaScript and those examples might be a bit daunting. Maybe if there’s a second edition I might expand on it with more advanced examples.

An advanced example might including dragging a shopping item into a basket. You could not use Sortable with that because usually, what you are dragging is a full description of something, and when you drop it into the basket, you want it to change to a compact description. Coding is involved there, and that’s when it gets bogged down. Writing about that sort of complex case would mean diving into JavaScript and away from the core subject of the book, which is the interaction of PHP and jQuery.

update: I fixed Sortable. Here’s an example of nested sortables using the corrected code: demo.