cmurray.org

Observations on technology, business, and other weirdness.

February 21, 2007

Writer For The Blogger

Filed under: Publishing — Christopher Murray @ 12:07 pm

Writer is a really fabulous web-based implementation of DarkRoom, the distraction-free writing and editing tool used by journalists and writers of all stripes. The idea is that your entire screen is reduced to nothing more than green typewriter text on a black background (although the color scheme can be modified to your liking). This provides you a writing environment clear of things like IM and email messages, the things that keep you from focusing on the writing.

I really like it and can’t believe it works this well in a browser. It even posts directly to my blog; in fact, this submission was posted from Writer (although it does publish it in Draft mode, which requires you to later add a title). You can write in a normal sized window, or for maximum effect, go full screen using F11. If you log into the site, you also are able to save and later modify your documents (which also makes them available from different computers).

Makes me wish I had more ideas to write about today.

http://writer.bighugelabs.com/

February 2, 2007

Building Dynamic Database Inserts

Filed under: @Work — Christopher Murray @ 11:24 am

As follow up to yesterday’s post about dynamically assigning variables from database queries, below is some code that takes all incoming values from a PHP form and dynamically maps them to database columns, again, avoiding manually parsing and assigning variables for every query.

The code is simple: form input comes in as HTTP_POST_VARS, which comprise both a key (the variable name) and a value (the information in the form field). We first loop through and break those two elements out of each field and name them $key and $value. Then we create a list of the keys, or field names (which map identically to their counterpart columns in the database), in the format required for the INSERT statement. We do the same for the $values. After looping though and building our lists, we do a little pattern matching and replacement to strip off the trailing commas from each list (preg_replace). From there, we build and execute our elegant little INSERT statement. And like yesterday’s example, this works with any number of fields.

I should note that performance would be better if instead of the preg_replace I used some conditional logic, but I didn’t have time to write that yet.

while(list($key, $value) = each($HTTP_POST_VARS))
{
if($key != “addRequest” && $key != “”)
{
$sqlKeys .= “$key, “;
$sqlValues .= “‘$value’, “;
}
}
$sqlKeys = preg_replace(“!, $!”,”",$sqlKeys);
$sqlValues = preg_replace(“!, $!”,”",$sqlValues);
$sql = “INSERT into vendorApprovals ($sqlKeys) values ($sqlValues)”;
$result = mysql_query($sql);
}

Here also is the code to edit the record we created above:

$sql = “UPDATE vendorApprovals set “;
while(list($key, $value) = each($HTTP_POST_VARS))
{
if($key != “editRequest” && $key != “” && $key != “editID”)
{
$sql .= “$key = ‘$value’, “;
}
}
$sql = preg_replace(“!, $!”,”",$sql);

$sql .= ” where id = ‘$editID’;”;
$result = mysql_query($sql);

February 1, 2007

Variable Variables In PHP

Filed under: @Work — Christopher Murray @ 5:01 pm

This little bit of code here is actually pulling some 24 fields from a database table and assigning them each to individual variables for populating a PHP form (which then can be used to repopulate the database). The trick here was to name the form fields identically to their counterparts in the database. For example, the form field vendorName maps to the database field vendorName.

In many cases, you would run your query, parse out the values, and then assign them manually to variables, like this:

$vendorName = $row[vendorName];

This can be really cumbersome when you have dozens of values coming out of the database. In the case below, we use the PHP function mysql_field_name to grab the database column names. We then use a device called variable variables to assign that value to another variable. The construct is that last line, $$key = ${value}. The double dollar sign makes that variable variable the actual field as variable name rather than its own value. So, rather than seeing vendorName = ‘RedHat’, we see and can use $vendorName = ‘RedHat’. Because of this feature I can dynamically populate all the fields in the form with values right out of the database with only this little bit of code.

$sql = “select * from vendorApprovals where id = ’1′”;
$result = mysql_query($sql);

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$i=0;
foreach ($line as $col_value) {
$field=mysql_field_name($result,$i);
$array[$field] = $col_value;
$i++;
}
}
while (list($key, $value) = each($array)) {
$$key = ${value};
}

 

Copyright © 2009 Christopher Murray