Search This Blog

Tuesday, February 15, 2011

cycle odd and even in Smarty

Mostly we use to apply alternate colors for table rows to do that we will use mod operator or if else condition to overcome this above method can be used it will rotate the values and assign to class we can have "n" number of values inside the cycle

Updated: Timezone Helper | The Bakery, Everything CakePHP

Handling TimeZones in CakePHP

The web is a global resource for anything, anywhere, any timezone. But often times we are careless in consideration for people in other areas of the world. The different timezones offset people’s schedules from one area to the next. When handling user profiles (or anything for that matter) where date and time is involved we can do a little magic with our CakePHP app to make things a little more convenient for our users. How do we handle this you ask?
First lets say when a user logs in to their profile they can click to edit the profile, selects his/her timezone, and clicks save. Relative to GMT we can calculate offsets of each timezone, where GMT would = 0. So for instance Eastern Timezone for New York, US would be -5 since its 5 ours BEHIND GMT. So lets start by generating a dropdown list with cake with all the different timezones, then save this list to the users profile. If you are using Auth with CakePHP, you will likely have a users table, and we’ll add a decimal field called “timezone” to that table. Fortunately there is a helper to assist us with this dropdown in the bakery, Timezone Helper. By taking a look at the source you’ll notice all the labels associated with an offset “-5.0″ or “+3.0″. Also look a little closer and you might notice a few have actual decimal (Kabul is +4.5). So first things first, go ahead and install that Helper as those instructions say. Add it to your Edit view for users to edit their profile. Once included you just add the drop down to your form with:
 
echo $timezone->select('timezone'); 
Now each user should have a timezone attached to their profile. Now the trick to timezones is that no matter what the timezone of our server is, we just want to convert it to display to the user in that user’s preferred timezone. On the reverse side, if a user enters a datetime, we convert it back to the timezone of the server.
Lets start our with the user viewing a date from the server, in our view we want to convert it to the user’s preferred timezone set to their profile (which should now be stored in their session, I’ll demonstrate with Auth)
 
$time->format('F j, Y g:i:s a'$mydatenull$session->read('Auth.User.timezone')); 
Using the Time helper in CakePHP, the format function accepts a fourth parameter which indicates the timezone, which is extremely helpful. So this is all you need to do to display a time from the server to a user.
Next lets say the user is about to edit a field from the server in a form. First we need to select the existing data from the database, we’ll convert it to the user’s timezone, and place it in an input field to be edited. Then once the user saves the data we’ll simply convert it back to the server’s timezone. (There a bunch of “one liners” in doing all of these tasks). So if you have your Time helper in your controller when you get the data from the database, go ahead and convert it to the new timezone just like we did before:
 
$data['Model']['mydate'] = $time->format('Y-n-d H:i:sP'$data['Model']['mydate'], null$this->Auth->user('timezone')); 
Now in your view for the form there are a few different scenarios. 1) You use Cake’s default inputs for editing date times, with the 6 dropdowns, or 2) You use a plain input and use a date/time picker of some sort. Lets first handle the first scenario, Cakes default 6 input datetime. The data has been submitted, validated and we’re about to place it back in the database, we need to convert it back to server timezone. But wait! the form field is broken down into an array of six elements: year, month, day, hour, minute, meridian. Well we can create a nice little function that will convert this array into a datetime, and pass it our timezone:
 
function dateTimeArrayToServerTZString($dt$tz=0){ 
    $tz = (floatval($tz) >= 0)? '+'. number_format(floatval($tz),2,':','') : number_format(floatval($tz),2,':',''); 
    $dt['min'] = str_pad($dt['min'], 2"0"STR_PAD_LEFT); 
 
    $loc = $dt['year'] .'-'$dt['month'] .'-'$dt['day'] .' '$dt['hour'] .':'$dt['min'] .' '$dt['meridian'] .' '$tz; 
 
    $datetime = new DateTime($loc); 
    $datetime->setTimezone(new DateTimeZone(date('e', time()))); 
 
    return $datetime->format('Y-n-d H:i:sP'); 
} 
Now right before I insert my data into the database I just need to call this function on that datetime array:
 
$this->data['Model']['mydate'] = dateTimeArrayToServerTZString($this->data['Model']['mydate'], $this->Auth->user('timezone')); 
Now $this->data['Model']['mydate'] will be a string, and cake still knows how to handle that just fine. Now for the second scenerio, a user submitting a string. This isn’t too hard at all, we just use our time helper and convert that puppy back to the server time.
 
$this->data['Model']['mydate'] = $time->format('Y-n-d H:i:sP'$this->data['Model']['mydate'], null, -5); 
Ok, so I’ve hardcoded in the timezone to -5 (Eastern Timezone), what if I want this to be dynamic to where ever my server is? Well the Time helper has a function which isn’t mentioned in the book called serverOffset(), which will return a number of the offset. If you’re not using the helper and just want to get the timezone of the server its a simple one-liner:
 
date('Z', time()) 
Thats all there is to handling timezones in your cakePHP site, or any site for that matter. Just keep in mind as you go to convert any times for the user’s pleasure. You have any tips or alternatives?

Handling TimeZones in CakePHP

Wednesday, January 19, 2011

System Columns in Postgresql

In postgresql ever table has several system columns that are implicitly defined by the system. Therefore, these names cannot be used as names of user-defined columns. 


oid

The object identifier (object ID) of a row. This column is only present if the table was created using WITH OIDS, or if the default_with_oids configuration variable was set at the time. This column is of type oid (same name as the column).

tableoid

The OID of the table containing this row. This column is particularly handy for queries that select from inheritance hierarchies, since without it, it's difficult to tell which individual table a row came from. The tableoid can be joined against the oid column of pg_class to obtain the table name.

xmin

The identity (transaction ID) of the inserting transaction for this row version. (A row version is an individual state of a row; each update of a row creates a new row version for the same logical row.)

cmin

The command identifier (starting at zero) within the inserting transaction.

xmax

The identity (transaction ID) of the deleting transaction, or zero for an undeleted row version. It is possible for this column to be nonzero in a visible row version. That usually indicates that the deleting transaction hasn't committed yet, or that an attempted deletion was rolled back.

cmax

The command identifier within the deleting transaction, or zero.

ctid

The physical location of the row version within its table. Note that although the ctid can be used to locate the row version very quickly, a row's ctid will change each time it is updated or moved by VACUUM FULL. Therefore ctid is useless as a long-term row identifier. The OID, or even better a user-defined serial number, should be used to identify logical rows.

Monday, January 10, 2011

9 rules of innovation from Google

1. Innovation, not instant perfection
"There are two different types of programmers. Some like to code for months or even years, and hope they will have built the perfect product. That's castle building. Companies work this way, too. Apple is great at it. If you get it right and you've built just the perfect thing, you get this worldwide 'Wow!' The problem is, if you get it wrong, you get a thud, a thud in which you've spent, like, five years and 100 people on something the market doesn't want."
"Others prefer to have something working at the end of the day, something to refine and improve the next day. That's what we do: our 'launch early and often' strategy. The hardest part about indoctrinating people into our culture is when engineers show me a prototype and I'm like, 'Great, let's go!' They'll say, 'Oh, no, it's not ready.
It's not up to Google standards. This doesn't look like a Google product yet.' They want to castle-build and do all these other features and make it all perfect."
"I tell them, 'The Googly thing is to launch it early on Google Labs and then iterate, learning what the market wants--and making it great.' The beauty of experimenting in this way is that you never get too far from what the market wants. The market pulls you back."

2. Ideas come from everywhere
"We have this great internal list where people post new ideas and everyone can go on and see them. It's like a voting pool where you can say how good or bad you think an idea is. Those comments lead to new ideas."

3. A license to pursue your dreams
"Since around 2000, we let engineers spend 20% of their time working on whatever they want, and we trust that they'll build interesting things. After September 11, one of our researchers, Krishna Bharat, would go to 10 or 15 news sites each day looking for information about the case. And he thought, Why don't I write a program to do this? So Krishna , who's an expert in artificial intelligence, used a Web crawler to cluster articles."
"He later emailed it around the company. My office mate and I got it, and we were like, 'This isn't just a cool little tool for Krishna . We could add more sources and build this into a great product.' That's how Google News came about. Krishna did not intend to build a product, but he accidentally gave us the idea for one."
"We let engineers spend 20% of their time working on whatever they want, and we trust that they'll build interesting things."

4. Morph projects don't kill them
"Eric [Schmidt, CEO] made this observation to me once, which I think is accurate: Any project that is good enough to make it to Labs probably has a kernel of something interesting in there so

5. Share as much information as you can
"People are blown away by the information you can get on MOMA, our intranet. Because there is so much information shared across the company, employees have insight into what's happening with the business and what's important."
"We also have people do things like Snippets. Every Monday, all the employees write an email that has five to seven bullet points on what you did the previous week. Being a search company, we take all the emails and make a giant Web page and index them."
"If you're wondering, 'Who's working on maps?' you can find out.. It allows us to share what we know across the whole company, and it reduces duplication."
Mew here, even if the market doesn't respond to it. It's our job to take the product and morph it into something that the market needs."

6. Users, users, users
"I used to call this 'Users, Not Money.' We believe that if we focus on the users, the money will come. In a truly virtual business, if you're successful, you'll be working at something that's so necessary people will pay for it in subscription form. Or you'll have so many users that advertisers will pay to sponsor the site."

7. Data is apolitical
"When I meet people who run design at other organizations, they're always like, 'Design is one of the most political areas of the company. This designer likes green and that one likes purple, and whose design gets picked? The one who buddies up to the boss.'
Some companies think of design as an art. We think of design as a science. It doesn't matter who is the favorite or how much you like this aesthetic versus that aesthetic. It all comes down to data. Run a 1% test [on 1% of the audience] and whichever design does best against the user-happiness metrics over a two-week period is the one we launch. We have a very academic environment where we're looking at data all the time.
We probably have somewhere between 50 and 100 experiments running on live traffic, everything from the default number of results to underlined links to how big an arrow should be. We're trying all those different things."

8. Creativity loves constraints
"This is one of my favorites. People think of creativity as this sort of unbridled thing, but engineers thrive on constraints. They love to think their way out of that little box: 'We know you said it was impossible, but we're going to do this, this, and that to get us there.'"

9. You're brilliant? We're hiring
"When I was a grad student at Stanford, I saw that phrase on a flyer for another company in the basement of the computer-science building. It made me stop dead in my tracks and laugh out loud."
"A couple of months later, I'm working at Google, and the engineers were asked to write job ads for engineers. We had a contest. I put, 'You're brilliant? We're hiring. Come work at Google,' and got eight times the click rate that anyone else got.
"Google now has a thousand times as many people as when I started, which is just staggering to me. What's remarkable, though, is what hasn't changed--the types of people who work here and the types of things that they like to work on. It's almost identical to the first 20 or so of us at Google."
"There is this amazing element to the culture of wanting to work on big problems that matter, wanting to do great things for the world, believing that we can build a successful business without compromising our standards and values.."
"If I'm an entrepreneur and I want to start a Web site, I need a billing system. Oh, there's Google Checkout. I need a mapping function. Oh, there's Google Maps. Okay, I need to monetize. There's Google AdSense, right? I need a user name and password-authentication system. There's Google Accounts."
"This is just way easier than going out and trying to create all of that from scratch. That's how we're going to stay innovative. We're going to continue to attract entrepreneurs who say, 'I found an idea, and I can go to Google and have a demo in a month and be launched in six.'"