pods_query Updates and Formatting
I’m in the process of going through the entire plugin and revising all usage of pods_query to utilize an array of objects (which is what $wpdb->get_results returns), as well as a few other updates to formatting.
Here’s a few I’m looking to get some input on –
pods_query() usage
I’m seeing pods_query used in two ways:
$sql = "SELECT ............. ";
$result = pods_query($sql);
$result = pods_query("SELECT ............. ");
Regardless of if it’s a SELECT statement, what should be the proper usage and when? If $sql is multi-line, or large it should be as $sql, otherwise it should be contained within pods_query() itself. Is that safe to assume?
array usage
How should arrays be formatted?
$variable = array('key'=>'value','key2'=>'value2');
or
$variable = array('key' => 'value','key2' => 'value2');
or
$variable = array('key'=>'value',
'key2'=>'value2');
or
$variable = array('key' => 'value',
'key2' => 'value2');
logikal16 4:23 pm on March 14, 2011 Permalink
I think the key here is readability. SQL statements are pretty important because they can often get pretty confusing pretty quick. For multi-line queries, I usually like to separate action (WHERE, ORDER, LIMIT) into a new line, like so:
SELECT a, b, c, d
FROM mytable
WHERE hello IN (‘world’, ‘dolly’)
or…
SELECT
a, b, c, d
FROM
mytable
WHERE
hello = ‘world’ OR
hello = ‘dolly’
LIMIT
1
Also, one other note about PHP ternary operators — e.g. $result = ($a > $b) ? $x : $y;
They also tend to get pretty unreadable quickly. The general rule is that if it requires any more than one set of (true / false) conditionals, to just break it out to IF / ELSE loops.
logikal16 4:27 pm on March 14, 2011 Permalink
Oh, and with associative arrays:
If you think the key/value pairs fit well into 1 line, then that’s fine:
$stuff = array(‘key1′ => ‘val1′, ‘key2′ => ‘val2′);
If not, then breaking each array element into a separate line makes it super readable:
$stuff = array(
‘key1′ => ‘val1′,
‘key2′ => ‘val2′,
‘key3′ => ‘val3′,
// This blog doesn’t show, but there’s 4 spaces to the left of each array element
);
But remember, this is your code too, so do what you think makes sense.
sc0ttkclark 4:29 pm on March 14, 2011 Permalink
Sounds good, thanks! I’ve got quite a bit of cleaning up ahead of me so I’ll keep these in mind as I go from file to file with the changes. Once I finish implementing the new $wpdb-based pods_query I’ll be running through and adjusting all references to pod_id. Also, all usage of tbl_row_id will now accept just ‘id’ going forward (‘tbl_row_id’ for backwards-compatibility).