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’ »
15 July 2009, 8:57 pm
I just discovered FirePHP today (via a discussion at PHPBuilder.com), a set of PHP classes along with a Firefox add-on which allows you to debug your PHP scripts through the Firebug console (a separate Firefox add-on you’ll also need to install if you haven’t already done so).
Essentially, what it does is allow your PHP server-side scripts to output debug information to your browser via a set of HTTP headers which are then intercepted and displayed by the Firebug console window. This way, instead of doing a var_dump() or print_r() to view a variable or object in the middle of your HTML output (or routing it to a log file on the server), you can view that info separately in the Firebug window.
Here’s a quick example of the most basic usage:
Continue reading ‘Debugging with FirePHP’ »
28 April 2009, 3:44 am
Upon being motivated by a “PHP style critique” discussion at the WebDeveloper.com forums, I dug around the manual page for mysqli_stmt::bind_result(), and noticed an interesting suggestion for the use of the call_user_func_array() function in combination with the bind_result() method (see the user note by “hamidhossain”).
The essence of the technique is to use call_user_func_array() to call the bind_param() method, supplying the list of variables to be bound via an array which has its elements defined as references to the actual variables you want bound. In the following example, I’m referencing them to a class variable named $data which is an associative array where the keys are the field names for the database table being operated upon.
Continue reading ‘MySQLi: Avoid Explicitly Listing Every Column in bind_result()’ »
5 April 2009, 10:14 pm
As I discussed earlier, I’ve been playing around some with NetBeans 6.5 for PHP IDE. While overall I have liked it a lot, one thing that was holding me back was that it did not seem to be possible to simply open a file for editing unless it was part of a defined project.
I figured it must just be my ignorance, so today I did a little googling and found out that there is a “Favorites” window you can open and use that for “random file access.” To access it, either select the “Window” menu item and then “Favorites” in the drow-down menu, or just type Ctrl+3. This adds the Favorites window in the left pane (at least in my NetBeans configuration).
Continue reading ‘NetBeans: Opening a Non-Project File’ »
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"; } } ?>