blog home

CSS transparency tricky IE6 fix

Posted by Jen in coding front-end on June 29th, 2009

semi-transparentIt’s not hard to implement a semi-transparent block by CSS in all modern browsers. The code is something like this:

.thisblock {
filter: alpha(opacity=65);
-moz-opacity: .65;
opacity: .65;
}

And in your HTML, you have something like

<div class=”thisblock”><img src=”someimage.jpg” />some text after the image</div>

Very eassy, right? It basically covers all browsers - IE7, Firefox, Mozilla, Chrome and even Opera. However, if you try IE6, it’s not working.  Here is one little trick for that… add either width or hight to that div class, then you are done. So the fix will be

.thisblock {
filter: alpha(opacity=65);
-moz-opacity: .65;
opacity: .65;
width: 50%;
}

width can be anything, number, percentage, except “auto” or “inherit”.

With the above trick said, if you really don’t want to define the width or height of a block, but have to implement the semi-transparency in IE6, use <p> instead. With <p>, you don’t have to worry about width or height, because not like <div> which span across the entire row,  <p> automatically wrap around the elements inside it, thus it got a specific width by itself.

A peek at Wordpress - permalinks and SEO sitemap

Posted by Jen in coding front-end, random thoughts on June 24th, 2009

blue-lWhen it comes to free CMS, people usually think of Joomla, Drupal, openSourceCMS, etc. Yes, those are good CMS, and we have made a few websites by using those systems and they all turned out to be great in terms of usability. However, what impressed me the most is the open source PHP/MySQL based Wordpress. With a false impression of a blog CMS, it won’t come up to many un-geeky people’s mind when they want to have a CMS for their regular static page based website. However, as a web professional, I would strongly recommend wordpress as the first CMS choice for most of my clients, because it’s simply great and powerful to develop and use, not just as a blog CMS, but also in all. You can certainly use it for a regular blog free website.

In general, we should think of a blog as a regular website, take its categories the same as navigation menus. The only difference is blog category usually has the URL as www.yoursite.com/category/categoryname1. It’s very easy to turn this URL to www.yoursite.com/categoryname1 which looks just like a regular website subpage. And of course it’s good for the search engine to find your page with less sub-directory as we all know. So how to do that? All you need to do is just go to your wordpress admin page, settings -> Permalinks ->Optional, in the field of category base, simply enter “.” (without quotes of course).

Alright, this is just one little trick of wordpress. It’s just so powerful that it does many things for you already and all you need to do is just a few clicks. One more example. For the google SEO sitemap, when you have wordpress installed, you will never need to manually write up or update this tedious XML file. Go install this google XML sitemap plugin, it takes all your dynamic blog posts as well as static pages to the sitemap file and update it whenever you change something on your site.

One more little trick about this sitemap plugin, if you install your wordpress for only the blog session on your server, and all other pages are located one directory above the wordpress blog session, to include all other pages as well as the blog pages in the sitemap, you will need to upload the sitemap.xml file to the root directly of your site, but not wordpress directory. On the sitemap configuration page, in the “additional pages” section, add the URL of the pages that are located above the blog directory, in “Location of your sitemap file” session, choose “custom location” radio button, fill in the sitemap location manually (should be under root directory). In the sitemap content session, make sure to choose “include homepage”, “include static pages”. Last, click on the link of “build the sitemap for the first time”. Then you are done! No need more work when you add more post or do any updates on the site. Isn’t this cool?

BTW, there are many free and beautiful themes on wordpress.org, and also many interesting blog sites on wordpress.com. You can go get your free blog sites by signing up there.

be consistent with php function return type

Posted by William in backend on April 20th, 2009

PHP is a type-less language, which gives you the flexibility of, for example, return anything from a function. This, however, can trigger hard-to-find bugs if you abuse it. A better choice would be consistent with return types, where I mean if the function is supposed to return boolean, only return boolean; if it’s supposed to return a string, always return a string; if it’s supposed to return an array, always return an array.

Here are some examples that shows how it can break your code if you do not pay attention to return types.

Example 1. Disaster: return boolean true and string:

function foo ()

{

if ( some_condition() )

return “ERROR_1″;

else if (some_other_condition() )

return “ERROR_2″;

else

return true;

}

So the author of this function is trying to return some error code (in string, bad idea!), or true if everything is good. But the problem is that any string will be evaluated to be true. So if you have a caller like this:

$rtn = foo();

if ($rtn == true)

// foo is good.

else if ( $rtn == “ERROR_1″ )

// handle ERROR_1

You will have a bug here. The “else” section will never be executed, that’s because if you compare any string to boolean true with “==” operator, it is always true. You could use === to force a type check but your callers may not be aware of that. Some solution to this would be:

  • make the function return true or false only, on throw exceptions instead of returning error code
  • if you so intend to return error code, either through string, or use defined constant (a better idea), never return a boolean. For example, rather than return boolean true, return a constant RTN_OK

in other words, when  you design your function, if you decide what type it should return, always return that type.

Example 2: a function that returns an array.

For example, you have a function that tries to get some data from memcache. If no data from memcache found, get it from database, and cache it in memcache.

function get_customers( $bookid )

{

$mc_key = “bk-cust-$bookid”;

if  ( !$customers = $memcache->get( $mc_key ) )

{

// no records in memcache

$customers = array();

$result = mysql_query(”SELECT * FROM book_customers WHERE bookid=$bookid”);

while ($row = mysql_fetch_array($result))

$customers[] = $row;

$memcache->set( $mc_key, $customers ); //cache it.

}

return $customers;

}

The problem of this function is that if there is nothing found in the database initially, you will end up setting an empty array in the memcache, then when you do memcache->get, you will get it back as a boolean true instead.