Simple Pagination

I use alot of Zend Framework for my freelance projects and prior to version 1.6 they have no pagination class to do the dirty work. I know what you’re thinking right now, why didn’t I just reused some of the many pagination scripts in the internet and/or in PEAR. I just really needed something simple that I can quickly reuse all over my web-app and here it is

To get the offset for your sql query just do this:

$offset = $numOfItemsPerPage * ($page - 1);

Here’s the code for the pagination, take note that the logic can be use regardless of the programming language, i’ve also use this in some of my java webapps

<?php
class Paginator {
	const NUM_TRAIL_LEAD_LINKS = 5;

	public static function generate($url, $total, $current = 1, $numPerPage = 5, $var = 'page', $separator = ' ') {
		$totalNumberOfPages = ceil($total / $numPerPage);

		$linksArray = array();
		$isFirstLinkDone = false;
		$i = 1;
		if(($current - self::NUM_TRAIL_LEAD_LINKS) > $i) {
			$i = $current - self::NUM_TRAIL_LEAD_LINKS;
		}

		for(;$i<=$totalNumberOfPages;$i++) {
			if(!$isFirstLinkDone && $current > 1) {
				$linksArray[] = '<a href="'.$url.'/'.$var.'/1">First</a>';
				$linksArray[] = '<a href="'.$url.'/'.$var.'/'.($current-1).'">Prev</a>';
				$isFirstLinkDone = true;
			}

			if($i == $current) {
				$linksArray[] = '<span style="font-weight:bold">'.$current.'</span>';
			}
			else {
				if(($current + self::NUM_TRAIL_LEAD_LINKS) < $i) {
					break;
				}

				$linksArray[] = '<a href="'.$url.'/'.$var.'/'.$i.'">'.$i.'</a>';
			}
		}

		if($current < $totalNumberOfPages) {
			$linksArray[] = '<a href="'.$url.'/'.$var.'/'.($current+1).'">Next</a>';
			$linksArray[] = '<a href="'.$url.'/'.$var.'/'.$totalNumberOfPages.'">Last</a>';
		}
		return implode($separator, $linksArray);
	}
}

  • Share/Bookmark
3 Comments

New Toy: Razer DeathAdder

Admittedly I suck at first person shooters, so regardless of what I’m using I still suck, the only question is which one would allow me to suck a little less. I was using Microsoft’s Wireless Laser Mouse 8000 which is a bluetooth mouse that has terrible response time when it comes to gaming and is doubly terrible when used with OSX. I don’t know if the issue lies with OSX’s bluetooth drivers or the mouse itself, but it lacks any kind of smoothness and lags all the time which makes using Photoshop or any graphics software an exercise in frustration.

Enter the Razer DeathAdder, a 1800dpi with 1000hz ultrapolling mouse. This baby has so far put me up the top 5 of total kills per game in Call of Duty 4. The difference in performance is tremendous, maybe because of it’s wired nature, but it has absolutely no lag at all between movements. It also has first class support in OSX by way of drivers/software to customize any and all aspects of the mouse (dpi, polling, button mapping).

  • Share/Bookmark
1 Comment

Enterprise and their sucky frameworks

No i’m not talking about frameworks that addresses their own unique business needs. I’m talking about your ORMs, MVC web frameworks, javascript (including ajax) frameworks, but for now i’m talking about your everyday java enterprise application. There is at least one of this monstrocities that is being build in every big company under the pretense of “We don’t need the other stuff that framework X offers”, or maybe you’ve heard of the priceless line “Framework X contains so much features that it can affect the scalability of our enterprise application”. So with those ammunition, management will give them a go signal to build their very own abomination; maybe 2 years down the line some moron will pick up a business magazine seeing those new fangled application servers and will decide that they have to migrate to it to keep up with the trend, and yes, you’ve guess it… their framework is not compatible with the new application server and they will have to spend several thousand dollars to port it. Standards and open source frameworks exists for a reason.

  • Share/Bookmark
3 Comments

5 things that suck about weak typing

We all love our scripting languages with its simplicity, elegant and beautiful syntax, and the ability to quickly hack together a solution. One of the common features of most scripting languages is being weakly typed. Meaning that we don’t have to declare the variable’s data type before we use it; to some developers this is a time saver and is one of the appeal of using these types of languages. But like with most things it has its pros and cons, and these are 5 things that you have to consider before using a weakly typed language.

  1. Using them in a team is risky. Unless you have some kind of coding standards to determine what kind of type this variable is being used for, there is always a risk of somebody trampling on each others variables thereby causing side-effects all over the system.
  2. Maintenance issues. Going back on an already completed system after some time to do maintenance work on bugs is a nightmare in any language. Weak typing exacerbates this situation because of the danger of lacking checks and balances on what a variable can handle.
  3. Tool support. While the current state of IDE’s for scripting languages are improving. There is still work to be done especially on code assists, because of the nature of weak typing, this only partially works on some IDE’s.
  4. Error catching. Scripting languages are almost always are interpreted at runtime, therefore most conversion errors are not caught since there is no compiler to catch this errors. Even if there is a compiler, type information are dynamically determined by the interpreter.
  5. Performance/Optimizations. Statically typed languages have the advantage of having been optimized by the compiler. While interpreted languages are always a little bit slower than their compiled counterparts, dynamic typing and dynamic type conversions will always cause a little overhead.

  • Share/Bookmark
10 Comments

Oh no its PHP!!!

A few months ago I was doing some job hunting and i’ve come across some interesting job interviews in the process. Now I have a good mixture of full-time work and freelance work, and most of my freelance work consists of enterprise PHP web applications; while my full-time work primarily consists of J2EE and Oracle.

During the course of the interview when they are finished asking me the standard questions regarding my J2EE and Oracle experience, they focused their attention on my freelance works. I must say there is a really big ignorance with the LAMP stack in the enterprise world, opinions and questions that PHP is only a toy language and it cannot “scale” like their “100 concurrent users or the app server will choke” J2EE apps.  Now this opinions are nothing new, but it’s hilarious when you actually meet people with that opinion. If only they knew that sites like Digg, Facebook, Friendster were constructed with PHP albeit the web-part only, but like they say, right tools for the right job. I cringe everytime people construct simple intranet sites with full blown J2EE architecture thinking it would automatically scale because it’s Java/Oracle; apps that could have been simply done with a LAMP stack and 1 week.

  • Share/Bookmark
7 Comments

Injecting technology to conservative management

During the course of my freelance work, i’ve encountered my fair share of conservative management. The fear, sometimes warranted, of migrating processes to use technology is staggering to the point of being absurd and sometimes humorous. Granted most of this businesses have experience using some form of technology, and they have been burned numerous times by past experiences which leads them to revert to the old style of pen and paper (LOTS OF THEM!). Future companies that would be brave enough to sell them their technology would be hard press to convinced them otherwise. Citing different reasons such as cost, maintenance and the concern for downtimes that will inevitably result to slower operations, and if your offering them an online solution, the concern of intermittent or unavailable online access.

How do you deal with this kinds of clients? Should we avoid them? Or try to enlighten them?

  • Share/Bookmark
0 Comments

New home!

Hi! Welcome to my new home, a place to share technology related ideas and random rants. Also a place to post new development updates for a still to be announced venture.

  • Share/Bookmark
0 Comments