class Loop
{
	private $index;
	private $elements;
	private $numElements;

	public function __construct()
	{
		$this->index	   = 0;
		$this->elements	= func_get_args();
		$this->numElements = func_num_args();
	}

	public function __tostring()
	{
		return (string) $this->get();
	}

	public function get()
	{
		if($this->numElements == 0) return null;

		$val = $this->elements[$this->index];

		if(++$this->index >= $this->numElements)
			$this->index = 0;

		return $val;
	}

	public function rand()
	{
		return $this->elements[array_rand($this->elements)];
	}
}

$color = new Loop('white', 'black');

echo "<tr class='$color'>";  // white
echo "<tr class='$color'>";  // black
echo "<tr class='$color'>";  // white

{ 0 comments }

Hello

by Yada on January 17, 2011

Wow. Haven’t updated the blog in a while.

{ 0 comments }

Tim Horton’s coffee sleeves

by Yada on April 22, 2010

I find Tim Hortons coffee too hot to hold especially while drinking and driving. Yes, I drink coffee and drive. It’s not against the law. :) When I make my coffee order at Tim Horton’s drive thru in the morning I would ask for ‘Double Cup’. The optimum brewing temperature of coffee is 90′c which is pretty much close to boiling. This is too hot for me to hold.

I often wondered why Tim Horton’s doesn’t have coffee sleeves. Today I think I figured out why.

IT’S NOT COST EFFECTIVE. It’s better to employ double cup. Take a look at this price chart at a restaurant supplies store.

Look at the price of Coffee sleeves. It’s is $37.99 for a box of 1000s. Take a look at the price of 1000s cups. It around the same price! Therefore, if Tim Hortons have coffee sleeves in the store probably about 25%* of the patrons would use it. Currently, probably about 5% of the patrons would ask for Double Cup. Hence, it’s better to not use sleeves and let the customers ask for Double Cup.

*number is educated guess. When I worked at Starbucks, 95% of patrons use coffee sleeves. 25% is a low end guess.

{ 0 comments }

Gmail tricks to have multiple emails

by Yada on March 3, 2010

1.  Gmail doesn’t look at ‘period’ in the email.

johnsmith@gmail.com
john.smith@gmail.com
j.o.h.n.smith@gmail.com

Is all the same emails.  Email sent to any of the address will be forwarded to the same email account.

This is a neat way to sign up for multiple accounts at some other service that requires unique email.  I found that out on my own because I have a period in my email and saw some email coming to me without the period.

2.  The second trick, I read on lifehacker is to use the plus sign trick.

Example: You could use
johnsmith+nytimes@gmail.com for nytimes.com
johnsmith+freestuff@gmail.com for freestuff.com

Gmail will automatically remove everything including the plus sign.

{ 3 comments }

Using aggregate function on subquery

by Yada on February 24, 2010

A developer was trying to compute the average of the first 100 records with LIMIT:

SELECT
  avg(con_hits) as avg_hits
FROM
  content
WHERE
  con_type = 1
  AND con_posttime < $twelve_hrs_ago
  AND con_refresh = 0
ORDER BY
  con_posttime DESC
LIMIT 100

Can you spot the subtle bug in the above query?

LIMIT is applied to the resultset, after AVG is calculated. The correct way is to use a subselect and apply the aggregate function:

SELECT avg(con_hits) as avg_hits
FROM (
  SELECT con_hits
  FROM content
  WHERE
    con_type = 1
    AND con_posttime < $twelve_hrs_ago
    AND con_refresh = 0
  ORDER BY con_posttime DESC
  LIMIT 100
) x;

{ 0 comments }

Javascript

February 19, 2010

It is just me, or does it seem that Javascript is getting a lot of buzz lately.  I remember reading resume tips such as “Don’t write Javascript on your resume because it is not really a language.”  Boy, how have the tide change?  Why is it that the Ninja label is binded to Javascript guru [...]

Great Tutorials

February 16, 2010

NetTuts is a great website to learn about web development.  It would have save me hours reading thought textbooks and online manual if this website existed 5 years ago.  Nevertheless, the website is great to pick up new technology quickly.
http://net.tutsplus.com/
I espececially like their getting started with Code Igniter series.
* Jeffrey Way’s CodeIgniter From Scratch: Day 1, Nettuts.com – [...]

C++

February 12, 2010

I find that I’ve been using C/C++ a lot less these days.  Not by choice, but my the what is needed by companies.  A lot of my projects deal with Internet technologies and on the LAMP stack.  If I need a quick way to parse text  I’ll use PHP which has a great string parsing [...]

Legitimate uses for a switch?

February 2, 2010

I earned my second “Nice Answer” badge on a Stack Overflow question.  I, myself, don’t find using switch all that useful.  The syntax for switches are always harder to remember compared to chaining a bunch of if else statements.  There are just a few times when switches do come in handy such as finding the number of days in [...]

PHP and MySQL database connection the easy way

January 21, 2010

I’ve been using a very light weight extension of mysqli to connect to the MySQL database with PHP.  It is a mysqli wrapper class for using the Singleton database design pattern.
To use the:
1.  Change database configurations with login/pass in db.class.php
2. No need for error checking with query.  Exceptions will be thrown on error.
3. Everything else works like mysqli.
See [...]