Subscribe

I have moved to newlyancient.com and will be writing regularly there! Content on this domain is no longer updated, but will be maintained as an archive in its original form.

Tag Archive for 'php'

Review of AJAX and PHP: Building Responsive Web Applications

This review has been cross-posted from the Unofficial Dreamhost Blog, which graciously provided me with a free review copy.

AJAX and PHP

We’ve all heard about that powerful little engine running the latest and greatest websites: AJAX. As developers, many of us probably understand the basics of AJAX, since the technologies behind it (JavaScript and XML) aren’t exactly new. Yet, we’re hesitant to take that first step into using the technology. That’s where AJAX and PHP: Building Responsive Web Applications steps in with a thorough explanation of AJAX and a plethora of satisfying use cases. With a promise to teach us "how to create better PHP Web Applications by using AJAX technologies to their full potential," we are given a sherpa to help us up the pinnacle of AJAX.

Though the book assumes a basic familiarity with JavaScript and PHP, plenty of attention is paid to the basics. The first chapter delves into some history of the web and an explanation of the buzz around AJAX. It concludes with a quick AJAX application to get you started right away. With a wave of a wand, chapter 2 appears with a solid grounder in the magic of the DOM and smarter JavaScript. Chapter 2 also integrates a primer on XML and its power. With the client satisfied, chapter 3 delves into detail upon the server end of things with lessons upon PHP and MySQL. For the noobs among us,  an appendix is included which details setting up the environment with Apache, MySQL, and PHP. Whether you have never touched a line of PHP in your life or are simply looking to expand your skill set to the client side, "AJAX and PHP" has the basics you need.

With the nitty-gritty technical aspects out of the way, the rest of the book is devoted to various case studies. This is where the true value is found: the examples are both practical and exciting. With the mundane form validation and autosuggest out of the way, even more exciting case studies are introduced. In Chapter 5, we learn about building an AJAX-based web chat application. In Chapter 7, we are introduced to the power of SVG (scalable vector graphics) and how it can be combined with AJAX. For me, the most interesting chapter was chapter 8 which addresses a critical issue in our data-rich world: presenting long tables in an eye-catching and usable manner. Here, we are given a primer on XSLT and grid-based display. Throughout the book, you find sprinkles of useful examples which will certainly come in handy with the continual emphasis upon cloud computing in today’s marketplace.

Despite the strong case studies, "AJAX and PHP" falls short in some critical areas. Most importantly, there is no attention paid to the plethora of JavaScript libraries out there. This is unfortunate, since most of us will end up using a library to automate the simple, repetitive tasks. Additionally, the code samples often are far longer than needed, sometimes stretching over many pages. That being said, the book’s companion site offers downloads of all the case studies to get you started. Overall, AJAX and PHP: Building Responsive Web Applications is a strong primer on AJAX and a useful reference for the variety of practical AJAX applications.

Quick and Easy WordPress Redirection

As I was working on my new contact form, I thought of the fact that many people might try to access it by simply typing in http://myfla.ws/contact. Therefore, I set off to find a solution which would easily redirect those people to my actual contact page. When looking, I had a couple of requirements in mind:

  • The script must be bundled in a WordPress plugin.
  • It must not interfere with any other part of my blog. (ie. It can’t require custom fields to function)
  • The management panel must be extremely easy to use.

When I was unable to find any plugin which met all of these requirements, I ended up spending a couple of hours to script one of my own. After some PHP work and practice in MySQL troubleshooting, I put together Permacop (short for Permalink cop). If you noticed, that link itself is an example of the plugin since it links to http://myfla.ws/permacop but redirects to http://myfla.ws/projects/permacop/. Using Permacop any blog can have its own personal traffic directer.

WordPress Page Slugged

As I was working away on the next rendition of this blog, I came across a slight problem. In the navigation, I wanted each tab to be a different color. To do this, I needed to assign a unique ID to each tab. No where in the core code of WordPress could I find a parameter to add this. Since I often change the name and order of pages, creating a static list was not an option. Even a quick Google didn’t turn up anything useful. So, I gathered my courage, wrote my will, and dived into the WordPress core. Using the search feature of Eclipse, I was able to track down the relevant class in about ten minutes. Once there it was a simple matter to change the output by adding an additional tag.

I am running WordPress 2.1 and the code may be different in other versions. In fact, there’s a 99% chance it is.

  1. Search around in complex code for 10 minutes. Done for you. :)
  2. Open wp-includes/classes.php in your favorite PHP editor.
  3. Go to line 512 (approximately) and replace this code:
PHP:
  1. $output .= $indent . ‘<li class="’ . $css_class . ‘"><a href="’ . get_page_link($page->ID) . ‘" title="’ . attribute_escape($page->post_title) . ‘">’ . $page->post_title . ‘</a>’;

with this code:

PHP:
  1. $output .= $indent . ‘</li><li class="’ . $css_class . ‘" id="t_’ . attribute_escape($page->post_name) . ‘"><a href="’ . get_page_link($page->ID) . ‘" title="’ . attribute_escape($page->post_title) . ‘"><span class="title">’ . $page->post_title . ‘</span></a>’;

which will render something like this:

HTML:
  1. </li><li class=“page_item” id=“t_about”><a href=“http://myfla.ws/about/” title=“About”><span class=“title”>About</span></a></li>
  2. <li class=“page_item current_page_item” id=“t_writing”><a href=“http://myfla.ws/writing” title=“Writing”><span class=“title”>Writing</span></a></li>

Using this same syntax, you could add other pieces of information to the navigation list. For instance, to display the ID of the page in the title of the link you could do something like this:

PHP:
  1. $output .= $indent . ‘<li class="’ . $css_class . ‘" id="t_’ . attribute_escape($page->post_name) . ‘"><a href="’ . get_page_link($page->ID) . ‘" title="’ . attribute_escape($page->post_title) . ‘ [’ . $page->ID . ‘]"><span class="title">’ . $page->post_title . ‘</span></a>’;

Although this solution is easy enough to implement, it is not elegant since it requires modification of the WordPress core code. In the future, it would be great for wp_list_pages() to be added to the pluggable functions of WordPress. This would make it very easy to create a plugin which does this same thing and is future proof. By modifying the WordPress code, you can add custom CSS to each link in a dynamic navigation setup.

You will need to change this code each time you update WordPress. Otherwise, you will lose the custom IDs.