Using PHP to write PHP

I ran into a situation recently where I had a very large array object that was not going to change. It was the polygon coordinates for the world countries to be used in KML provided by Thematic Mapping.

I didn’t wan’t to pull this data out of MySQL every time I went to build a map, I wanted to use one giant static array instead. So to write the array back out in a re-usable fashion I developed the following code which can take any array, of any depth, associative or index based, and spit out corresponding PHP that you can copy and paste back into your script.

function phpPrintArr(array $arr) {
	echo "array(".PHP_EOL;

	$c = 1;
	$total = count($arr);

	foreach($arr as $i =>$v) {
		$iq = is_numeric($i) ? '' : '"';
		$vq = is_numeric($v) ? '' : '"';
		$e = $c < $total ? ',' : '';

		if(is_array($v)) {
			echo $iq.$i.$iq.'=>'.PHP_EOL;
			phpPrintArr($v);
			echo $e.PHP_EOL;
		} else {
			echo $iq.$i.$iq.'=>'.$vq.$v.$vq.$e.PHP_EOL;
		}
		$c++;
	}

	echo ")".PHP_EOL;
}

Oh, and for anyone who is interested in the array of country polygons this produced from the Thematic Mapping Engine data, here is the rather large array. Feel free to use it in your own KML project.

Tags: , , , ,

HBase JSON-RPC Bridge updated, allows scanning

The HBase JSON-RPC bridge has been updated to allow scanning on non-key columns. Now, passing a filter parameter creates a scanner using a RegexStringComparator. Results are returned in 100 block chunks until null is returned, in which case the ResultScanner is closed

Here is an example query that is now possible:

http://localhost:8080/hbasebridge/rpc?debug=true&data={"method":"lookup","params":{"table":"hbasetable","filter":"searchstring"}}

Tags: , , , ,

Bible Flashcards for Android 2.0

It’s been a while since I released Bible Flashcards version 1.0. Long enough in fact that I started getting emails asking whether I intended on updating past the 1.1.4 release.

Well I am proud to announce version 2.0 which includes a lot of requested features and a lot of bug-fixes and improvements. For this release I decided to take my time and re-factor the way various screens interacted, making them more modular and self-contained which should translate into fewer force closes. I also unleased the monkey on my app which helped me improve my code even more.

So without further ado here are the major features included in this release:

  • Ability to mark cards learned/unlearned
  • Ability to choose a random card
  • Cards now cycle so that if you are on card 1 and you attempt to go backwards, you are taken to the last card. Likewise if you are on the last card and you attempt to go forwards you are taken to the first card.
  • Menus also cycle so that if you hit the back button you will go from the active card view to the lesson chooser to the lesson set chooser and back to the card view.
  • Preferences to control settings such as card text size, whether to display learned cards, and a button to remove all learned cards from the internal database.

Some interface items had to be chopped to make room for these improvements. So if you are wondering where the next/back buttons are, they have been removed in favor of the more intuitive gesture controls. Swipe right for next card and left for previous card. Tapping the card flips it over.

I also want to give a special thanks to everyone who contacted me with words of encouragement and everyone who brought to my attention things that were broken and things that could be improved upon.

This release also includes a new lesson set, RossWords, which is being put together by  Samuel Rogers as he takes Hebrew this year. If anyone else is feeling generous  and would like to help out, feel free to contact me about a lesson set that you could help me improve upon or create as Samuel is doing.

Here are some screen shots from the new version:

And as usual, you can find Bible Flashcards in the Android Marketplace by searching for “Bible Flashcards” or by scanning the barcode below:

Tags: , , ,

A Cascading Style Sheets (CSS) Beginner’s Tutorial

Learning CSS can be a bit daunting if you’ve never encountered it before. Likewise, if you’ve only had limited exposure to CSS, the various ways browsers implement various aspects of the CSS standard (or make up their own) can leave you with the impression that it is all a giant hairy mess. So to help out, I’ve compiled a list of resources to make the learning curve not quite as steep for beginners and to hopefully help tame the CSS wilderness for novices.

First, here is a pretty good and in-depth video on HTML and CSS basics:

Next we have several handy beginner’s tutorial sites:

Finally, here are a few CSS frameworks designed to help make CSS a lot easier by providing a standard system that takes care of much of the common ugly quirks found in CSS:

As a bonus, here are a few inspirational sites to help give you an idea of what CSS can do if applied properly:

Tags: , , ,

Simple JSON-RPC updated to 0.9.7

Simple JSON-RPC has been updated to 0.9.7. This new version includes the ability to enable using full class names rather than simple method names. This makes using multiple classes with the same public method names possible.

To enable this functionality simply add an init-param to your web.xml file like:

<init-param>
	<param-name>use_full_classname</param-name>
	<param-value>true</param-value>
</init-param>

With this feature enabled, you then call methods via their full classname + method name separated by a dot (this nomenclature is for both static as well as non-static methods, the framework handles the particulars in the back-end).

Special thanks to Stephan of Cross Pollinate for suggesting this improvement!

Tags: , , ,

Bible Flashcards for Android 1.0

Bible Flashcards is an Android application based on the data files provided by the Crosswire Bible Society‘s Flashcard application. It contains flashcards for both Greek and Hebrew along with appropriate fonts for proper display.

I’ve also included an additional lesson set titled “greekBasics” which includes flashcards for the Greek alphabet.

Here are some screenshots:

You can find Bible Flashcards in the Android app Market by entering “Bible Flashcards” or by scanning the barcode below:

Additionally, if you are interested in helping out with new lessons or if you would like to provide feedback, please feel free to email me.

Tags: , , , , , , ,

Learning Languages: Java

The first thing you’ll need to consider is the development environment you want to use primarily. This is important as it will have an impact on how you run through tutorials and examples later on.

Environments can be broken down into two broad categories; command-line or a visual IDE. Both have their merits and you’ll eventually need to be familiar with both (especially if you expect to be releasing production code or participating in any well-maintained development environment).

The most common command-line environments are Maven and Ivy. Both come with a somewhat steep learning curve (which, unfortunately is unavoidable) but both are well worth investigating as they are both very common in production environments.

Starting out, however, you’ll most likely find that using a visual IDE will help you get right down to learning and compiling example code fairly quickly.

There are several common IDEs; IntelliJ, NetBeans, and Eclipse are all great ones that I’ve seen used in production. My favorite hands-down is Eclipse, especially since it also has configurations to help you develop in other languages such as PHP (Aptana) and C/C++.

For general basics and a broad overview of Java; I would recommend you take a look at the Java Beginner site.

There’s also several handy video tutorials (mostly using Eclipse) on YouTube such as this one:

Once you get the basics down, I’ve found that working on a full project helps. A great place to start would be to help our with an existing open-source project like JSword.

Tags: , , , ,

Javascript implementation of Java’s String.hashCode() method

Here is a direct replacement for Java’s String.hashCode() method implemented in Javascript.

I wrote this function to fulfill a requirement at work. Apparently, the back-end engineers thought hashCode() was a standard function. One of the hurdles for this project was not only figuring out how to translate the mathematical formula used in Java to generate hashCode()’s but also how to force Javascript to use 32bit integer math (no small feat).

Fortunately, I discovered that Java supports bitwise operators which are constrained to 32bit integer math.

So here’s the resulting String prototype in Javascript. With this prototype you can simply call .hashCode() on any string, ie. “some string”.hashCode(), and receive a numerical hash code (more specifically, a Java equivalent) such as 1395333309.

String.prototype.hashCode = function(){
	var hash = 0;
	if (this.length == 0) return code;
	for (i = 0; i < this.length; i++) {
		char = this.charCodeAt(i);
		hash = 31*hash+char;
		hash = hash & hash; // Convert to 32bit integer
	}
	return hash;
}

Tags: , ,

HipHop for PHP

Earlier this year Facebook developers caused quite a stir in the PHP community by releasing HipHop, the central piece in their high-performance arsenal.

Here is the video of the initial announcement along with some juicy technical details:

If you are interested in seeing how your PHP project fares when run through HipHop, check out the official HipHop project page.

With tools like HipHop, scripting languages like PHP are no longer subject to the charges of gross computational inefficiency. In short, it’s a great day to be a web developer.

Tags: , , , ,

Native Sword libraries for Android

I’ve spend quite a bit of time recently figuring out the best approach for incorporating some form of Sword libraries1 into my Android application. After an unsuccessful attempt to get the pure Java implementation, JSword, to work2 I decided to see if I could, instead, use the Android Native Development Kit and wrap the C/C++ library in a Java Native Interface.

After doing some digging on the web I found out that Troy over at Crosswire had already begun a project named “bishop” whose aim was to provide a Java native interface to the sword library. Just what I was looking for!

While the process of building the library and corresponding java source files is more than what I want to get into here (though I may later on as I hopefully help contribute to the maturity of the project) I wanted to share my findings with anyone else who, like me, is interested in bringing more open-source Bible applications to the Android platform.

So without further ado; Here is a jar file and corresponding JNI library you can use in your own Android project to harness the power of Sword in your own Bible-related apps.

  1. Sword is an excellent suite of libraries for accessing a large array of Bibles and Bible-related modules stored in an open format. []
  2. The fault here was not with the JSword project per-se, the fault really lies with the limited Java environment provided by the Dalvik JVM. []

Tags: , , ,