Rail Pass – Indian Railways / IRCTC App

Rail Pass – app for Indian Railways and IRCTC

Rail Pass is a companion app for Indian Railways and IRCTC

more

Posted in blog | Leave a comment
• • •

Talk: Bingo – The numbers game!

My talk at Causal Connect Asia 2013, Singapore on the importance of Anlaytics in Game Design.

Tags: , , ,
Posted in blog | Leave a comment
• • •

Talk: Bingo! – Design to Win

My Talk at The Startup Festival 2013, Bangalore.

Tags: , , , ,
Posted in blog | Leave a comment
• • •

Free Enterprise and Community

“In a free enterprise, the community is not just another stakeholder, but is, in fact, the very purpose of its existence.”

Jamsetji Tata

Today is Jamsetji Tata’s 174th Birth Anniversary. He showed that it is so much important that you sow the right seed of vision for your enterprise, so that it bears the fruits that benefit all.

Tags: ,
Posted in inspiration | Leave a comment
• • •

The Nature of Ambition

Ambition Part-1
Ambition Part-2
— Incidental Comics

Tags: ,
Posted in inspiration | Leave a comment
• • •

Talk: Making Hit Cross Platform, Cross Device, Multiplayer Games

My talk about the success story of our hit title Bingo Bash at NASSCOM GDC 2012 at Pune.

2012-11-02 - NASSCOM GDC 2012 Talk

 

Tags: , , , , ,
Posted in blog | Leave a comment
• • •

MySQL Union is column name and data-type agnostic

I stumbled upon a rare care while working on one of my projects. The data in one of my variables in PHP was being jumbled up. It took an hour of debugging my PHP code till I realized the the issue was in the MySQL query and that MySQL was mixing up the data in the columns for a particular query.

The issue was in the way that MySQL parses executes the queries with UNION. When you do a UNION of two SELECT queries, MySQL just appends the second data-set to the first data set without matching the column names or warning if the data types of corresponding columns are not same. If you mix up the order of the column names, the resulting data you get will be incorrect or at least not what you would be expecting in your code.

Although the official documentation does talk about being data-type agnostic, it doesn’t mention about column name being in same order in all the queries.

Example

Lets take this example of a table.

CREATE TABLE users (
	id SMALLINT NULL,
	first_name VARCHAR(50) NULL,
	last_name VARCHAR(50) NULL,
	points SMALLINT NULL,
	level SMALLINT NULL
);
+------+------------+-----------+--------+-------+
| id   | first_name | last_name | points | level |
+------+------------+-----------+--------+-------+
|    1 | Lalit      | Patel     |      5 |    50 |
|    2 | Chuck      | Norris    |     10 |   100 |
+------+------------+-----------+--------+-------+
2 rows in set (0.00 sec)

The following query runs without any error and it gives the result set given below. Note that it takes the column names in the order they were specified in the first query, but mixes the values up.

(SELECT id, first_name, last_name, level, points FROM users WHERE id = 1)
UNION
(SELECT id, last_name, first_name, points, level FROM users WHERE id = 2);
+------+------------+-----------+-------+--------+
| id   | first_name | last_name | level | points |
+------+------------+-----------+-------+--------+
|    1 | Lalit      | Patel     |    50 |      5 |
|    2 | Norris     | Chuck     |    10 |    100 |
+------+------------+-----------+-------+--------+
2 rows in set (0.00 sec)

It doesn’t even throw an error if the data type of corresponding columns dont match. See the query and the result set below. MySQL doesn’t even check the column data types of the columns. It just appends the result of 2nd query below the result of the first.

(SELECT id, first_name, last_name, level, points FROM users WHERE id = 1)
UNION
(SELECT id, points, level, first_name, last_name FROM users WHERE id = 2);
+------+------------+-----------+-------+--------+
| id   | first_name | last_name | level | points |
+------+------------+-----------+-------+--------+
|    1 | Lalit      | Patel     | 50    | 5      |
|    2 | 10         | 100       | Chuck | Norris |
+------+------------+-----------+-------+--------+
2 rows in set (0.00 sec)

MySQL will return the result as long as the number of columns are same in both the queries. A bit too much forgiving.

Tags: , ,
Posted in lab | 4 Comments
• • •

Greedy Regular Expressions

By default the pattern matching in regular expressions is greedy in nature. The regex engine will try to match the largest string possible for the given expression rather than the first smallest match, which may not be desired behavior every time. Consider the example below where we are parsing an XML string using regular expressions.
more

Tags: , ,
Posted in lab | Leave a comment
• • •

Base62: Convert a number to base 62 for short URLs in PHP

Base62 is a PHP class to convert a number to and from any base between 2-62.

There is no native function in PHP to convert upto base 62. The native function base_convert converts numbers upto base 36, ie. using 0-9 and a-z. Since URLs are case sensitive, we can use additional character set of A-Z to make a number even shorter.

Most of the examples I found on the internet were converting a decimal to 62 and most of them were using BC Math functions to achieve that functionality. This script removes those restrictions and you can use it to convert to and from arbitrary bases between 2-64.

Features and Optimizations

  • It doesn’t use BC Math functions so works without the use of BC Math library.
  • It uses the native base_convert functions when the base is below 36 for faster execution.
  • The output number is backward compatible with the native base_convert function.
  • Can be used to convert to and from arbitrary bases between 2-64.

Usage

The usage is pretty simple. You have to include the class file in your code and call the following function.

$converted_num= Base62::convert($number, $from_base, $to_base);

Example

echo Base62::convert(696952941);
// 'Lalit'
echo Base62::convert('Lalit', 62, 10);
// 696952941

Download (v0.1, 08 Dec, 2011)

The code is released under Apache License 2.0

Plaint text PHP source (v0.1) | Formatted PHP source (v0.1)

Tags: , , , ,
Posted in lab | 3 Comments
• • •

XML2Array: Convert XML to Array in PHP

XML2Array is a class to convert XML to an array in PHP. It returns an array which can be converted back to XML using the Array2XML class.

It can take a string XML as input or an object of type DOMDocument.

Conventions

  • attributes stored as key value pairs under ['tag_name']['@attributes']
  • CDATA nodes are stored under ['tag_name']['@cdata']
  • In case a node has attributes, the value will be stored in ['tag_name']['@value']

Usage

The usage is pretty simple. You have to include the class file in your code and call the following function.

$array = XML2Array::createArray($xml);
print_r($array);

Important thing to note is that the $array returned can be converted back to XML using the Array2XML class.

Example

The Following XML:

<?xml version="1.0" encoding="UTF-8"?>
<movies type="documentary">
  <movie>
    <title>PHP: Behind the Parser</title>
    <characters>
      <character>
        <name>Ms. Coder</name>
        <actor>Onlivia Actora</actor>
      </character>
      <character>
        <name>Mr. Coder</name>
        <actor>El ActÓr</actor>
      </character>
    </characters>
    <plot><![CDATA[So, this language. It's like, a programming language. Or is it a scripting language? 
All is revealed in this thrilling horror spoof of a documentary.]]></plot>
    <great-lines>
      <line>PHP solves all my web problems</line>
    </great-lines>
    <rating type="thumbs">7</rating>
    <rating type="stars">5</rating>
  </movie>
</movies>

will generate the following output:

array (
    'movies' => array (
        'movie' => array (
            'title' => 'PHP: Behind the Parser',
            'characters' => array (
                'character' => array (
                    0 => array (
                        'name' => 'Ms. Coder',
                        'actor' => 'Onlivia Actora',
                    ),
                    1 => array (
                        'name' => 'Mr. Coder',
                        'actor' => 'El ActÓr',
                    ),
                ),
            ),
            'plot' => array (
                '@cdata' => 'So, this language. It\'s like, a programming language. Or is it a scripting language? 
All is revealed in this thrilling horror spoof of a documentary.',
            ),
            'great-lines' => array (
                'line' => 'PHP solves all my web problems',
            ),
            'rating' => array (
                0 => array (
                    '@value' => '7',
                    '@attributes' => array (
                        'type' => 'thumbs',
                    ),
                ),
                1 => array (
                    '@value' => '5',
                    '@attributes' => array (
                        'type' => 'stars',
                    ),
                ),
            ),
        ),
        '@attributes' => array (
            'type' => 'documentary',
        ),
    ),
)

Download (v0.2, 04 Mar, 2012)

The code is released under Apache License 2.0

Plain text PHP source | Formatted PHP source

Tags: , , , ,
Posted in lab | 10 Comments
• • •