Phine Solutions web work notes

A PHP boolean variable note

Filed under: PHP development by 1.618 — June 22, 2008 10:40 am

I am by no means a PHP know-all so this might have been documented.

If I have a boolean variable, say

boolean x = true;

and if I use it in a if statement like:

if ( x == ‘abc’)

The result will always return “true”.

I believe the PHP version I am using is 5.2.

Is Yahoo making the wrong move?

Filed under: my 2 cents by 1.618 — June 13, 2008 10:57 am

There are a lot of talks around Yahoo’s move recently. Y’s decisions to reject the Microsoft deal and partner with Google on search advertising market don’t sit well with a lot of people. Some think Yahoo is making stupid decisions.

I beg to differ.

Being bought by Microsoft will make a lot of shareholders happy today. But if bought by Microsoft, 3 years from now, will Yahoo still be relevant at all? Choosing to remain independent will give Yahoo a chance. But what about the shareholders? Well shareholders don’t RUN the company. They can make their influence by choosing the board, or cast their vote by selling the stock. I don’t own YHOO so this might sound insensitive. But company shareholders having disagreement with management team is not new and opinions from shareholders who want to sell the stock and cash in should not be used to judge the company’s direction.

Will Yahoo+Google deal hurt Yahoo in the long run? It might hurt Yahoo’s advertising network. But Google is already so dominant in search market and Yahoo may never be able to catch on with its technology today. The key is for Yahoo to innovate and find new ways to establish a stronger brand, not necessary all in the search market though.

Some also have concerns that Yahoo is helping to create a monopolistic Google which will turn evil in no time. Google has 60% search market share because so far its search technology is the best. And it is not so dominant in other areas and Yahoo’s deal will not make it be. I think the key is to believe that technology is alway moving forward, today’s big players will be replaced if they stay still, and there will always be newer and better things coming out. Technology is simply not a business that can be established upon monopoly.

Having said that, the challenges ahead of Yahoo are obvious and I wish them the best of the luck.

Use JQuery to adjust the iframe height

Filed under: javascript by 1.618 — June 8, 2008 2:21 pm

Although frame is generally not recommended on most of the web pages, iFrame can still be useful in some occasions, especially as an Ajax alternative. One strength of Ajax is that it greatly reduces (or at least it appears so) the page response time by only changing a small part of the page. Using Ajax to submit a form is a great example. This usability improvement doesn’t come free though. In order to use Ajax to send the form data back to the server, the JavaScript code has to be written to collect the form data and append them to the request URL as GET or POST parameters. Frameworks like JQuery or Prototype make this process a lot easier.

However there is some limitation to use Ajax to post a multi-part form. Although it might be possible, it is definitely not a clean implementation, and it is not supported by most of the browsers. In this case, iFrame may be a good alternative. The inner frame page will handle the multi-part form and the parent page will have the similar Ajax effect. That being said, iFrame’s biggest problem is that its width and height are set right from the start and it won’t adjust based on the source content. This will leave the ugly scrolling bars around the iFrame, or some content will be hidden if the scroll bar is disabled. For most of the pages, the width is somewhat less of a problem but the height is harder to control and set correctly.

The good news is there are ways to dynamically adjust the height of the iFrame based on the inner content. There are different ways to achieve this using JavaScript. I found this approach is the best:

Using jQuery, we can add the following code in the iframe source content:

<script type=”text/javascript”>
$(document).ready(function() {
var theFrame = $(“#iFrameToAdjust”, parent.document.body);
theFrame.height($(document.body).height() + 30);
});
</script>

The JavaScript will get the iframe object from the parent DOM and change its height according to the size of the current document after the document is loaded (very important to get the true size). I like it because it is less intrusive to the page where the iframe is on, and the iframe source kind of “take care of its own size” when being displayed. If the source page is displayed as regular page, there simply will not be any adjustment. With the help of jQuery the code is quite clean and simple. Of course the iframe source has to be an internal page and the developer has the permission to add the code.

From my testing result, this works in FF2, IE7 and Opera 9.25, not in Safari 3.1 for Windows though.

Some updates:

I found it work better to put the JavaScript which adjusts the frame height in the body onLoad attribute. Basically “ready” function will be kicked off when the DOM is loaded, at which point the page may or may not be completely loaded. The onLoad event will be a better bet in this case since we need the actual size of the page including all the images.

Amazon AWS web service

Filed under: programming in general by 1.618 — June 2, 2008 1:08 pm

Amazon’s AWS webservice has been around for a while now and recently I implemented one of my web site to use the Simple Storage Service (A3). The idea is to utilize the storage space in Amazon’s computing cloud to ease the actual loads on my own web server. In this case, I store the user uploaded images into Amazon A3 storage as backup; and when the images are loaded on a page, they are pulled directly from Amazon A3. By using A3 the web site uses the distributed computing/storage resources and save the bandwidth.

Some of the good reasons to use A3:

  • The service is cheap. We are talking about GBs on the pennies here.
  • The service is quite reliable. I know earlier this year when A3 went down and a bunch of sites that were built using A3 as part of core infrastructure went down with it. But overall the down time is very rare. And A3 load time has also been pretty fast based on my experience.
  • Easy development. The A3 development community is a great place to find resources. For example, this standalone S3 php class pretty much has everything you need to start using the A3 storage.

Although we have a lot of good reasons to use AWS, I still wouldn’t embed too much of it into the infrastructure. The A3 storage in my case is used as a backup source and the actual data can be retrieved from local server by simply flipping a property value in the configuration file. The data update is also initialized by the scheduled cron jobs instead of building into the code. The benefit of this is that A3 is never something that my site has to reply on to stay up. And A3′s performance will not heavily influence the web site’s performance.

I have to say AWS is a brilliant idea and the fact that it came out from an online retail site makes it even more interesting. Here is a brief interview on Jeff Bezos who talked about Amazon’s cloud computing service during the D6 conference in May 2008.

©phinesolutions.com