22 September 2009, 3:22 pm
I had a couple fields on the form for adding authors/books to the DB for which I wanted to use an “auto-suggest” feature via JavaScript; one of those things where after you start typing it pops up a list of matching choices from which you can select the one you want. After trying several different scripts that I could not get to work for one reason or another, I ended up using Ajax Auto Suggest v.2.
I did, however, have to change one line of that JavaScript file in order to get it to play nicely with CodeIgniter “friendly” URLs. I just got rid of the bit that set a varname= part of the URL:
// create ajax request
//
if (typeof(this.oP.script) == "function")
var url = this.oP.script(encodeURIComponent(this.sInp));
else
// var url = this.oP.script+this.oP.varname+"="+encodeURIComponent(this.sInp);
var url = this.oP.script+encodeURIComponent(this.sInp);
Then when activating it within the page (CI view), I simply specified the CI URL to the controller/function with a trailing slash, to which the JS script then appends the value which my CI method will receive as its first (and only) parameter:
<script type='text/javascript'>
// <![CDATA[
var options = {
script:"/index.php/ajax/author_auto_complete/",
json:true
};
var as_json = new bsn.AutoSuggest('author', options);
// ]]>
</script>
17 September 2009, 5:22 pm
As part of my Book List project I am making use of the Template design pattern for my CodeIgniter database table models. The idea is that an abstract class contains the common processing needed by each class that will extend it, and it will have some number of abstract methods which essentially force each child class to set variables that differentiate each child class — in this case the database table and column names for the specific model class.
Continue reading ‘Book List Project: Using the “Template” Pattern’ »
17 September 2009, 12:31 pm

The 5.2.0 version of Komodo Edit has just been released. In addition to some bug fixes and UI features, the release notes indicate that support for PHP 5.3.0 has been added, including code completions for name spaces and syntax coloring of latest keywords (“namespace”, “use”, “as”, etc…).
(FYI, this is the editor I’m currently using, but who knows which I’ll be using a few months from now.)
16 September 2009, 11:06 am
As something I’m doing just because I want to and as a learning experience, I’m in the beginning stages of creating a web application where users can manage data on books: books they own, books they’ve read, and books they want to read. Assuming I stick with it, I figure I’ll post occasional articles here on the progress; hopefully providing some useful information on some of the issues I encounter as well as showing how my warped mind approaches such a task.
I started thinking about this a week or two ago, and posted a thread at KindleBoards.com to get some feedback on what sort of features people might like. I’m still in the process of deciding which suggestions to embrace and which to ignore. In the meantime I’ve started to forge ahead on the database design. I find that if I get the database structure right, then the rest falls into place much more easily — as opposed to making the database design fit my application code. My first major snag is trying to figure out how to deal with the fact that book titles are not unique (titles cannot be copyrighted). For details on that issue, I just started a thread at PHPBuilder.com, hoping some database expert will have a magical solution for me.
About the only firm decision at this point is that the app will be built upon the CodeIgniter framework, simply because I’m used to it, and it works. I’ve done some preliminary layout work for the front end, and have been making progress on using the Amazon.com Product Advertising API to (hopefully) provide a simple means for users to add books to their lists via a simple drag-and-drop of a URL from an Amazon web page. Unfortunately for us Amazon Kindle users, the Amazon database is currently not playing nice with Kindle books, so for now it’s limited to print book pages.
More to come soon, I hope….
8 September 2009, 9:43 am
If you run any WordPress blogs, be sure to upgrade your software to the latest version (2.8.4 as of this writing) to avoid an exploit which may place your blog or web site at risk. See the article at “Lorelle on WordPress” for more info. Either use the upgrade link on your WordPress blog’s control panel, or download and install the latest stable version.
2 August 2009, 10:21 pm
So I’ve been playing around with Komodo Edit (the free editor available from the people who make the not-free Komodo IDE), and one thing I found missing was a code beautifier (or automatic formatting, if you prefer). It’s not a feature I use a lot, but can be really useful when importing code from someone else.
So after a bit of Googling, I found this article: PHP Code Beautifer Macro+Script. I followed the instructions to install the PEAR PHP_Beautifier package, saved the provided phpbeautifer.php script in my PHP directory, installed the macro file into the Komodo Edit “Toolbox” and edited it so that it pointed to where I’d put the PHP script (using double back-slashes for the directory separators since I’m on Windows), and it worked! All you have to do is double click the phpBeautifier icon in the Toolbox window, and it formats the currently active file.
The only change I’ve made so far is to edit the phpbeautifier.php file to remove the addfilter() instruction to insert a newline before functions, as that messed up classes with any access keywords before the function keyword. I also changed the indention to 3 spaces, and added the filter for PEAR styling.
Continue reading ‘Komodo Editor: PHP Code Beautifier’ »
Strip Comments and White-Space from PHP File
Inspired by a number of web forum “conversations,” I decided to make a little tool for stripping PHP comments from files as well as blank lines and leading white-space. This is for any of you who feel you need to save a few nano-seconds when processing PHP files, or maybe want to do a little elementary code obfuscation. The main work is done by the built-in PHP tokenizer functions.
Enjoy
<?php /** * Strip comments out of PHP files * * Derived from code example at http://www.php.net/tokenizer * * Sample usage: * dir_strip_comments(getcwd(), 'test'); */ /** * T_ML_COMMENT does not exist in PHP 5. * The following three lines define it in order to * preserve backwards compatibility. * * The next two lines define the PHP 5 only T_DOC_COMMENT, * which we will mask as T_ML_COMMENT for PHP 4. */ if (!defined('T_ML_COMMENT')) { define('T_ML_COMMENT', T_COMMENT); } else { define('T_DOC_COMMENT', T_ML_COMMENT); } /** * Strip PHP comments from supplied source code string * * Also strips out blank lines and leading white-space * @param string $source * @return string */ function strip_comments($source) { $tokens = token_get_all($source); $result = ''; foreach ($tokens as $token) { if (is_string($token)) { // simple 1-character token $result .= $token; } else { // token array list($id, $text) = $token; switch ($id) { case T_COMMENT: case T_ML_COMMENT: // we've defined this case T_DOC_COMMENT: // and this // no action on comments break; default: // anything else -> output "as is" $result .= $text; break; } } } return $result; } /** * Strip comments from specified file, writing to specified directory * * @param string $file * @param string $dir */ function file_strip_comments($file, $dir=null) { if($dir === null) { $dir = getcwd(); } $target = $dir . DIRECTORY_SEPARATOR . basename($file); $source = file_get_contents($file); if($source === false) { user_error("Unable to read file '$file'"); return false; } $source = strip_comments(trim($source)); $source = preg_replace('#[\r\n]\s+#', "\n", $source); $result = file_put_contents($target, $source); if($result === false) { user_error("Unable to write to file '$target'"); } return $result; } /** * Strip comments from PHP files in directory * * @param string $sourceDir * @param string $targetDir * @param string $ext */ function dir_strip_comments($sourceDir=null, $targetDir=null, $ext='php') { if($sourceDir === null) { $sourceDir = getcwd(); } if($targetDir === null) { $targetDir = getcwd(); } $files = glob($sourceDir . DIRECTORY_SEPARATOR . "*.$ext"); foreach($files as $file) { $result = file_strip_comments($file, $targetDir); if($result) { echo "Wrote: " . $targetDir . DIRECTORY_SEPARATOR . basename($file); } else { echo "ERROR: did not write $file"; } echo "<br>\n"; } } ?>