Quickie OOP

Just dropping quickly here, and wanting to share a little and yet important part of software development, in web or in any platform.

I believe it is always nice to have a property in your object (or class) to hold the latest error encountered or latest command executed. Sort of like this

class test1
{
  var $last_error;
  var $last_cmd;
  // some other properties here
  function test1()
  {
    // other codes here
  }
  /**
   * the "why" for this class
   */
  function execute()
  {
    $command = $this->get_command();
    $this->last_cmd = $command;
    system($command, $res);
    if ($res)
    {
      $this->last_error = $res;
      return false;
    }
    return true;
  }
}

I realize the need for these two properties when I was debugging the CMS I was working on. It involved GraphicsMagick to process resizing, cropping and sharpening image, and executed thru PHP exec or system functions. I forgot why I arrived at using exec over the other. What I did was include these two properties in the log thru a logger method of another class in the system, which indeed, helped me get back to my sanity :D .

Blueprint CSS as base CSS, sweet!

Hacking in or modifying Blueprint CSS to match your design is easy! You will notice that the original .box class had the light blue color, but here it isn’t.

What I did is added this code below to make .box background color as grey.

.entry .box { 
  background-color: #fdfdfd !important; 
  color: #696969 !important; 
  border: 1px solid #dedede; 
}
.entry .box a { 
  color: #363636 !important; 
  text-decoration: none; 
}
.entry .box a:hover { 
  text-decoration: underline; 
}

					

Smarty template for generating cascaded UL based menu

So far, I’ve been happy how this template behaves when I pass a parent/child links into it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
< {$parent_tag|default:'ul'}{if $menu_id} id="{$menu_id}"{/if}{if $parent_class} class="{$parent_class}"{/if}>{* ul tag opens here *}
{foreach from=$links item=link}
  {if $link.class_active && $link.href == $current_uri}
    {if $link.class}
      {assign var=class value="`$link.class` `$link.class_active`"}      
    {else}
      {assign var=class value="`$link.class_active`"}
    {/if}
  {else}
    {assign var=class value="`$link.class`"}
  {/if}
  < {$child_tag|default:'li'}{if $class} class="{$class}"{/if}{if $link.id} id="link_{$link.id}"{/if}>
    <a href="{$link.href}"{if $link.target} target="{$link.target}"{/if}>{$link.text}</a>
  {if $link.children}
    {include file="links.html" links=$link.children parent_class=""}
  {/if}
{/foreach}
< /{$parent_tag|default:'ul'}>

Does something like this when rendered

<ul class="sf-menu">
  <li><a href="http://www.swerteka.com/index.php">Home</a></li>
  <li><a href="http://www.swerteka.com/PostItem">Post Item</a></li>
  <li><a href="http://www.swerteka.com">Blog</a>
    <ul>
      <li><a href="http://www.swerteka.com/BlogComments/do/viewrecent">Comments</a></li>
    </ul>
  </li>
  <li><a href="http://www.swerteka.com/Content/name/about">About</a>
    <ul>
      <li><a href="http://www.swerteka.com/Content/name/contact">Contact Us</a></li>
    </ul>
  </li>
</ul>

Basic and efficient MySQL statement

Update only columns that has changed.

Its tedious to compare old column values against the new being submitted before adding the column in the query statement. But, most of the articles I read says its the most efficient.

+---------------+--------------------------+-----------------+
| username      | email                    | lastip          |
+---------------+--------------------------+-----------------+
| admin         | admin@google.com.ph      | 127.0.0.1       |
+---------------+--------------------------+-----------------+

In this example, current $row["email"] is compared to request $email value

/* repeat this test in every column in your table */
if ($row["email"] != $email)
{
  $columns[] = "email = '$email'";
}
/* constructed sql statement would be */
$sql = "update customer set email = '$email' where username = 'admin'";

Another advantage following this convention is when there are multiple users accessing the same row. Though it sounds impossible that same row is updated at an exact same time, unless your site tops google on ranking. And even so, your SQL operation would have saved a little less resource than usual.

On-the-fly Blueprint CSS

Ok, I think there’s more to Blueprint CSS than fixed width designs. Somebody already came up with the liquid version.

So I think I’d spend some time adapting this generator and pass the four parameters needed to calculate the grid.

<script src="/getblueprint.php?width=950&cols=24&colwidth=30&margin=10"></script>

or perhaps a mode parameter to set px or percent as values

<script src="/getblueprint.php?mode=px&width=950&cols=24&colwidth=30&margin=10"></script>

Superfish for WordPress

Header.php

<!--
  replace "WP_STYLESHEET_DIRECTORY" with this 
    <?php bloginfo('stylesheet_directory'); ?> 
  Need to bring your own minified (not packed) jquery version, 
  some bug that it didn't work using WP jquery package
-->
<script src="WP_STYLESHEET_DIRECTORY/javascript/jquery-1.2.6.min.js" type="text/javascript"></script>
<script src="WP_STYLESHEET_DIRECTORY/javascript/superfish.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
        $('ul.sf-menu').superfish();
});
</script>
<link rel="stylesheet" href="WP_STYLESHEET_DIRECTORY/superfish.css" type="text/css" />
echo '<ul class="sf-menu">';
echo '  <li><a href="' . get_option('home') . '">Home</a></li>';
/** 
 * do not include links 
 * wp_list_pages('title_li=&exclude=590,155,163'); 
 **/
wp_list_pages('title_li=&');
/**
 * or use only specific categories 
 * wp_list_bookmarks('title_li=&categorize=0&category=7'); 
 **/
wp_list_bookmarks('title_li=&'); 
echo '</ul>';