Archive

Archive for the ‘code’ Category

Linked labels for jqplot

October 17th, 2009

In a typical bar-chart, the jquery plugin jqplot lets you set your x-axis labels to whatever string you want, including HTML mark-up. Imagine you have a chart showing the number of monthly downloads for prototype, jquery, and MooTools.

you might set your x-axis labels (values, to be precise) to

<a href=”http://www.prototypejs.org”>jquery</a>

<a href=”http://www.jquery.com”>jquery</a>

<a href=”http://www.mootools.com”>jquery</a>

The links get rendered in a <div> outside the canvas, but you cannot click them because the chart’s canvas extends over them.

solve it this way (using jquery for example):

$(’.jqplot-xaxis-tick’).css(’z-index’, 1000);

That sets your labels above the canvas and they can now be clicked.

Also reported at http://groups.google.com/group/jqplot-users/browse_thread/thread/bd9b9c49e609167e

 

 <

Blink in style using jQuery

September 5th, 2009

Today I released the CyclicFade jQuery Plugin. It has its own dedicated page for details, demo, and download. It is listed in the jquery plugin repository.

 

 <

Secure Integration of 3rd-Party Javascript Code

September 1st, 2009

Yesterday (august 31, 2009) the OpenAjax Alliance announced in a press release the immediate availability of javascript technology for securely integrating javascript code (widgets) from untrusted sources into your own web pages (and javascript code). It’s called OpenAjax Hub 2.0 and it is touted as the enabler of secure mashups.

There is a very similar initiative at Microsoft: websandbox at Microsoft livelabs. I have no idea if these are one and the same initiatives, or competitors, or somewhere in the middle.

My prediction is that beyond mashups and many other fantastic applications, this kind of technology will at last make the Semantic Web a reality. Why? because now data and its closely associated code (just like an object in OOP) can safely travel together from any source to any web application. The code is what gives the data some meaning even when the “mashup” does not know the semantics.  As long as we can apply this idea to as *small* a piece of data as we (architects and developers) want, we can build web 3.0.

As a result of this new technology, expect the start of a very dynamic phase of web 2.0 advances.

Read the OpenAjax Hub 2.0 Specification here.

 

 <

Separate PHP Debug Output from HTML Page Content

April 23rd, 2009

I wrote a simple class today to help me organize my debug messages in PHP, while keeping the generated HTML intact (mostly). 

Personally, I like to output all the debug messages at the bottom of my html page (structurally speaking, and layout-wise). This way I can see how the output renders for real, and if something looks fishy I have the luxury of a potentially large set of debug messages that I can read through, looking for fishy stuff.

It’s just a glorified ‘echo’ function; but one which lets you decide where to actually output the debug messages in your generated HTML. Or you can think of it as a mini-logging function, that lasts for the time of just one HTTP response; and as a programmer you can do whatever you want with the ‘log’, i.e. output it any way you like.

In fact it is similar to what placeholders do for you within the Zend_View system, i.e. prepare some output but defer when it is actually outputted. In this case, the output is generated from within the controllers, models, and helper classes though, so that’s why I had to, in some sense, duplicate what ZF already provides for views.

Here is a quick tutorial:

First, download the DebugOutput class and rename the file to DebugOutput.php

In a strategic place in your code (for instance the init method of your controller in Zend Framework), include the php file and set the type of debug output you would like (none, inlined within <pre> … </pre>, inlined within <!– … –>, or deferred to your view):

...
public function init()
{
        require_once '..../DebugOutput.php';

	if ( ... /* if in development mode */ ... )
		DebugOutput::setOutputType(DebugOutput::DEFERRED_OUTPUT );
	else
		DebugOutput::setOutputType(DebugOutput::NO_OUTPUT );
	}
...

 

Then in your code, you call the _debug() function as often as needed:

...

_debug($thisVariable, 'this variable');

...

_debug($thatVariable, 'that variable');

...

 

Before you rendering engine / view engine / smarty / Zend_View takes over, you make the debug messages available to it in the following fashion (here assuming we are using a Zend Framework Action Controller, i.e. the Zend_Controller_Action class):

...
public function postDispatch()
{
        ...
    	$this->view->debugOutput = DebugOutput::getDeferredOutput();
}
...

 

And finally, at the bottom of your layout / wrapper / main view (in this case my Zend Framework layout), the debug messages are in an array, available to your rendering engine, and so you can treat them like any other output, for instance like this:

...
<?php if (!empty($this->debugOutput)) { ?>
<div style="background-color: #ffe0e0;
	width: 96%; padding-left: 2%; padding-right: 2%; padding-top: 50px; padding-bottom: 50px; margin-bottm: 100px;
	border: 1px dotted red; overflow: auto; float: none">
<p style="font-weight: bold; color: red; text-align: center">DEBUG INFO</p>
<pre>
<br />
<?php     if (is_array($this->debugOutput))
                    foreach ($this->debugOutput as $debugElement) { ?>
<hr />
<br />
<?= $debugElement['label'] ?>:
<?= print_r($debugElement['value'], true) ?>
<?php         } ?>
</pre>
</div>
<?php     } ?>
</body>
...

 

I hope someone finds it useful or gets some better ideas from it. If so, please share :-)

 

 <

Easily Back Up MySQL Databases to Amazon S3

April 8th, 2009

tarball: dbbak.tar.gz

I wrote this script as a very simple way to take any MySQL server and back up all the databases matching a certain name pattern, using a single command and no extra parameters. Simply log onto your server, run dbbak.sh and you have a new DB snapshot on your file system, and a copy somewhere on the Amazon cloud in case your server melts down.

The cool thing is that once you are set up, you don’t have to worry about naming your backups, or where to store them. Names are generated automatically based on the date and time, and the name of the server (arbitrarily set by you). The files are compressed, and saved on S3 as well.

Saving to S3 is performed by another-s3-bash, which is included in the tarball above.

update, 2009/10/04: I cleaned up the main shell script and the INSTALL instructions. In some cases the s3 client does not seem to be able to connect to AWS S3. I have not debugged that yet.