Archive for the ‘Software Development’ Category

I thought I’d be spending a few hours creating a nice reusable way to turn a flat query result into a hierarchial array so I could use it on things like trees or HTML or whatever. When I wrote it out on paper though I felt stupid because the solution was extremely simple.

Assume you have a query result or data like this:
container1 parentA child11
container1 parentA child12
container1 parentB child13
container2 parentC child14
container2 parentC child15
and so on.

every column represents a parent of the column to its right. Duplicates in the columsn let you know which things get grouped together. The solution to turn this into a hierarchy is:

$arrTree[container][parent][child]

you can follow the same pattern indefinately, change the values and the keys as needed. The simplicity in the solution is that by using the columns as the keys it automatically creates them as unique and puts the children under them correctly. I hope this save someone else the paper epiphany :)

27
Mar

Config Defaults with OverRides pattern

   Posted by: admin

This may be obvious to some, but I have approached the same problem in many different ways without thinking too much about it over the years. In an effort to write cleaner easier code to read I settled n a pattern I am happy with, so I’d like to share.

The typical problem: You have code (Language doesn’t matter much) that accepts a number parameters / inputs. You can;t always rely on those inputs being there, so you need to have defaults. There are several things to check for before going to your default settings. Might be check if the input exists, if it matches certain values, and if it’s a certain variable type.

you could end up writing it like this:

<?php
if(isset($_POST[‘display’])){
    if($_POST[‘display’] == ‘[]‘){
        $display = ‘default’;
    }else{
        $display = json_decode($_POST[‘display’]);
    }
}else{
    $display = ‘default’;
}
?>
 

it’s not THAT bad, but it definitely has more conditional branches than necessary, and when you have several variables to do that sort of thing to, it gets ugly fast.

the alternative I like is to set your default, run your check on the input, and only override the default if it passes all the checks. Like so:

$display = ‘default’;
if(isset($_POST[‘display’])){
    if($_POST[‘display’] != ‘[]‘){
        $display = json_decode($_POST[‘display’]);
    }
}
 

Then when you have several variables / inputs you can define all the defaults first and together in a “Configuration Defaults” section then have all of the input override check after.

Hopefully the process of writing this post will prevent me from ever using the much uglier method. Enjoy.

20
Feb

more jqGrid patterns

   Posted by: admin

I have been using jqGrid a lot, and thought I’d post a few of the solutions / hacks I came up with that were useful:

/* replace grid values */
    ,loadComplete: function(){
        var ids = jQuery("#grid").getDataIDs();
        /* for each row loaded */
        for(var i=0;i<ids.length;i++){
            var cl = ids[i];
            /* get the row data, this works best when you have to do this for multiple columns, otherwise you might use getCell  */
            var objRowData = jQuery("#grid").getRowData(cl);
            /* for each column, give it a replacement or function that modifies the value */
            jQuery("#grid").setRowData(cl,{
                ntlm:renderCheckmark(objRowData.ntlmHidden)
            });
        }
    }

/* Allow resort of rows. */    
    ,loadComplete: function() {
        jQuery("#grid").tableDnD({
            onDrop:function(objTable,objRow) {
                /* get the resulting order */
                var rows = jQuery(‘#grid’).getDataIDs();
            }
        }
    }
   
/* Add something in the middle of the navbar where the inserted position is relative to the "eq(1)" */
jQuery(‘#gridPager td.nav-button:eq(1)’).after(‘html stuff to add in’);

/* Use the nav bar for buttons but hide the pager. CSS */
#grid  #first, #grid  #prev, #grid  input.selbox, #grid  #sp_1, #grid  #sp_2, #grid  #next, #grid  #last{display:none;}
 

Only show the pager when there are enough rows to use it.

,loadComplete: function(){
        /* Dynamically show the pager if it’s needed */
        if(jQuery("#tableGroup").getGridParam("records") > jQuery("#tableGroup").getGridParam("rowNum")){
            jQuery(‘#tableGroupPager’).find(‘#first, #prev, input.selbox, #sp_1, #sp_2, #next, #last’).show();
        }else{
            jQuery(‘#tableGroupPager’).find(‘#first, #prev, input.selbox, #sp_1, #sp_2, #next, #last’).hide();
}
}
 
24
Nov

PHP CRUD template for jqGrid

   Posted by: admin

I have been using the JQuery for quite a while. JQuery does not have an official plugin for grids, there are several of them out there. I tried out a few and settled on jqGrid   I am very pleased with it in every way. I’ve used it in several projects and it’s always been very flexible and extendable for my needs. I’ve been able to easily add after effects like color sorting, drag and drop reorder etc. with ease. Writing the PHP for the typical CRUD (Create Read Update Delete) functions is very easy but is so boring that I had to do SOMETHING that would both give me a bit of fun and give me smoething to make this part of the process faster for me in the future. So I have a .php template here with it, I can change a few variables around and apply it to any MySQL operation that interacts with jqGrid.

define

$crudColumns =  array(
    ‘id’=>‘id’
    ,‘title’=>‘title’
    ,‘icon’=>‘icon’
    ,‘description’=>‘description’
    ,‘parent’=>‘parent_list_id’
);
$crudTableName = ‘list’;
$postConfig[‘id’] = ‘id’;

and it’ll create the select, search, paging, insert, update, delete interactions for jqGrid for you.
it also has some convenient spots to edit the sql for additional column / query manipulation. At a minimum it should serve as a good starting point for working with jqGrid beyond the examples they provide (Which are very good and helpful already). If you have any updates or suggestions for this please let me know.
-enjoy

12
Nov

SimplePie RSS Feed library

   Posted by: admin

SimplePie is a PHP development library for reading RSS. It’s amazingly simple, well documented and full featured. I have written 4 RSS readers for portals and virtual desktops using it, and it’s alsways been a pleasure to work with. Here is the link:

http://simplepie.org/

When would you use this? Anytime you want to pull an RSS feed into a page for a decision or display. Almost everything has an RSS feed. You can pull in multiple feeds, have them automatically cached and aggregated, and choose a sort method. It will also automatically find RSS feed URLs if you give it a URL to any site. Combine this with something like Dapper: http://www.dapper.net/ and there’s no limit to what you can integrate into your project!

Here’s some sample code to show how easy SimplePie is:

<?php
require_once(‘SimplePie/simplepie.inc’);

$feed = new SimplePie(array(‘http://suddendevelopment.com/?feed=rss2′,‘http://LegalizeThought.com/?feed=rss2′));
$feed->handle_content_type();

$htmlOut = ;
//====|| Get RSS items for feed||====\\
foreach ($feed->get_items() as $item){
$htmlOut .= ‘<div class="rssItem">’;
$htmlOut .=    ‘<div class="rssTitle"><a href="’.$item->get_permalink().‘">’.$item->get_title().‘</a></div>’;
$htmlOut .=    ‘<span class="rssDescription">’.$item->get_description().‘<br></span>’;
$htmlOut .=    ‘<span class="rssDate">’.$item->get_date(‘j F Y | g:i a’).‘</span>’;
$htmlOut .= ‘</div>’;
}
print $htmlOut;

?>
 

This was a fun little solution I thought was worthy of sharing.

The Problem: You have a  very large grid. you need to display it on one page, but when you scroll you loose sight of the headers.

The solutions: Create separate tables for the headers, put them in containers that hide the overflow, and use slidersto control the scrolling.

Demo: http://anthong.com/examples/scrollxy/

References: http://demos.flesler.com/jquery/scrollTo/  http://docs.jquery.com/UI/Slider

Enjoy,

-Anthony

28
Jun

Build, Buy or Retrofit

   Posted by: admin

Build vs. Buy is an old dilemma. I find the answer to be rather simple. If you find a product that:

  1. Meets all of your criteria
  2. Will not require modification to meet the criteria
  3. Is within your budget

Then the choice is clear. Get that product and feel good about it.

The obvious time to build is even more simple. If you can’t find anything that comes close to what you need, put your construction hat on.

The 3rd option is more complicated.  You will often not find any exact matches for your requirements, but you’ll find a few that are close. Then it will take some evaluation into how deep the changes will go into the original product. In general I advise AGAINST getting a product that you cannot use right away as intended. For many commercial products, you have much more leverage as a potential customer than a supported one. Present your issues to a sales engineer and make them come up with how their product will support your requirements, in writing.

If you decide to get a product and retrofit it to your needs, you are running some pretty high risks. The risks are:

  1. You will no longer be supported by the owners of that product.
  2. You won’t be able to take advantage of future releases without retrofitting them as well.
  3. Any published user extensions will not be compatible with your version with some added effort.
  4. Over time you version is cannot be recognized or reconciled with the original.

This can be an option if you want to jump start your development and have an appropriately sized full time development staff to maintain it for the rest of the products lifespan.

If you have a development team and a little more lead time to get your product ready, I suggest another approach.  Build your product from components that you don’t need to change. Evaluate several lower level applications and development frameworks to assemble exactly what you nee. This will allow you to still take advantage of upgrades and extensions for the various components while still meeting your very specific needs.

This advice comes from analyzing a lot of past experiences, I hope that it can be beneficail to someone else.

8
Nov

Open source development = better developers

   Posted by: admin

I have contributed to open source projects ever since I knew what the term meant. I didn’t see the real benefits until I created my own projects, or completed modules for existing projects. Now that I have been doing that for over a year I want other developers and their employers to know the joys and benefits of developing open source projects.

There is an obvious intrinsic benefit of creating something and sharing it with others, and community collaboration, but that’s so surface level I won’t mention it anymore.

When you work in any job that has more than one technical resource, you divide responsibilities. You are often given the responsibility that you are strongest in so that the company can benefit. You might not be in the project from the start or carry it until its finished. You block out your own ideas on the project in order to knock out the next ticket, task or requirement. Over a long period of time, you have done a lot of work but all of the identifiable accomplishments are that of the team. This is all very necessary in order to accomplish an end goal, but what I’ve found is that some smart side tracking can be good overall.

In contrast to the typical workplace; When you create your own project from start to finish every idea is yours. The ones you implemented and the ones you did not. You start to get a new perspective for what decisions need to be made to complete a product at the expense of some lost ideas and compromising standards. As you are developing, you might think of one of the forums you were 0n that was criticizing someone else’s application, so you rewrite half your code just to avoid that same person complaining about yours (You could do this endlessly). As you are developing you find out how much of a niche you’ve been in when it comes to the complete process. You have to create the database, the functions, the presentation etc. there’s a lot to learn in each niche and also a lot to learn in how they work together. You start to gain an understanding for why the DBA needs to be involved early, and why the presentation guy needs to know all of the peripheral components that he’s not working on directly. When you have a product that you think is complete, you go to publish it and realize how far you really have to go. You still have to assign a starting release number and think of a reason it is and how you will progress it. You have to write the documentation. When you get to the install portion of the doc you admit that you would not follow such a long and complicated process, so now you need to write some install routines. Now you have an actual complete package, what will you do with it? If you use other apps as a benchmark you need your own webpage, screenshots, forums etc. By the end you are a bit sick of how much effort there is beyond creating the core product, but hopefully you push through to be proud of something you can release.

By the time you release your project you have learned a lot of things you otherwise would not have had the opportunity to. You put some extra thought and effort into it knowing you’d have your name next to it and people can be rather heartless when review the free software they download. Most importantly, you gain a new perspective for how everything fits together, and logical explanations for why things turn into the messes you work with normally.

After you release it you can get some very good feedback in how to improve you development skills, presentation, etc. You have to be stoic about the process and expect for a few people to just flame you in a non-helpful manner, which in itself is a very useful skill.

From an employers perspective, they have an employee with enough thought to create their own product, drive to implement it and mental agility to learn all the areas necessary to complete it. It’s far better than a training seminar, or college course and there’s something tangible to show for it at the end. If the employer likes the project they can claim it as their own to drive traffic and credibility. If they become a user of the application they will immediately appreciate the completeness of the package including documentation and install routines that are so often missed in internally developed apps. This is something that Google does as a practice and many of their very popular applications were born out of encouraging employees to work on personal projects that were approved. I believe that this is of such an obvious benefit to employers that they will eventually look for employees who have done these projects in the past, because it can tell you so much more about them as a developer than any resume.

Good luck in your Open Source apps and getting them approved!

27
Oct

eyeOS: More than another framework.

   Posted by: admin

I have been working with eyeOS lately. I hacked around with some of the existing components, and developed one of my own. I can tell you that after researching every “Web Desktop” and “Virtual Desktop” I could find reference to, this is the best one that you can download and manipulate under an open source license. This project is unique in its approach. They have merged the use of PHP with javascript to seamlessly complement each other and create an actual OS on a web platform. One thing I noticed right away as I started developing with it, is that this team concentrated on creating systems that would be beneficial to having a web environment rather than a bunch of web apps throw together. For example, each running module or “eyeApp” is assigned a serialized process ID which can be used to kill the process, or Identify it. There is a lot of work that needs to be done on it depending on your application for it. The windowing system is done very well but all of the eye candy is not as rich as something like extjs. The PHP is clean and very innovative, but there are many core features that it lacks (Like native DB support). Since I started working with it, I have run across many more projects that are in the same area and I’m still very happy that I chose to go through the learning curve on this one. Here is a summary review to compare with other products: 0-10 style

  1. Cost: Free (Under an Open Source License)
  2. Features: 7
  3. Presentation: 8
  4. Code maturity: 8
  5. Documentation: 4
  6. Community:8
  7. User learning curve: 30 minutes
  8. Developer Learning curve: 3 days

If you have huge $$ to blow and want a commercial product to invest in instead of spending development time customizing eyeOS I recommend checking out the Laszlo webtop.

8
Oct

PHP tech mashup: CURL + Tidy + XMLParser

   Posted by: admin

I have been working on some very challenging projects recently that require retrieving remote pages, manipulating them and then sending them back to the user. There are several problems with this scenario:

  1. Due to a bunch of greedy and malicious hacks, many of the obvious and easier techniques are blocked by necessary security measures.
  2. Whenever you deal with outside code you don’t know what you’re going to get. It could be half formed, have all kinds of javascript workarounds, rely on frames in frames etc.
  3. Relative URLs. If you retrieve a remote page and display it locally, any relative URLs will be off, so links will be broken and image will not show. This also applies to stylesheet and script references.

Here’s the solution in a nutshell:

  1. Use CURL http://us3.php.net/manual/en/ref.curl.php to retrieve the remote page.
  2. Use Tidy http://us3.php.net/manual/en/ref.tidy.php to put the code into a proper XHTML format. This will close tags, give you predictable formatting, make it very easy to read the source and most importantly put it in a format that can be parsed as XML.
  3. Parse the XML using xml_parse http://us3.php.net/manual/en/function.xml-parse.php This is a wonderful function! I have spent hours coding XML parsing functions that recurse, save parents etc. This is a much more efficient method (Depending on your task) it creates 2 arrays, 1 for an index, and one for values. You can use the index to find what position you need in the values array and get any info you need.
  4. Manipulate the cleaned up HTML. Since you have XHTML to work with now you can manipulate it as XML or you can use string replacement. This would be the time to replace all of the relative URLs with Absolute ones.
  5. Display it back to the user! I don’t want to know how you apply this method, I just like coming up with the solutions ;)
Page 1 of 3123»