Wednesday, April 7, 2010

GCE O/L, A/L and Grade V Examination Results

Do you want to view O/L, A/L and Grade V results a bit earlier than http://www.doenets.lk lets us. I have tried it when it says, "New Await Results". You can do this using firebug addon on firefox.

www.doenets.lk lets us to check previous result too. These are displayed by the "New Await Results" table. So what you need to do is first select one of those forms and change action url to exam_OL.jsp, exam_AL.jsp or exam_GV.jsp as appropriate. You would also need to change the size/maxLength of the inputput field as well. Otherwise it won't let us to insert our full index number. That's all, fill it with your index number, submit and get your results. You can also do a http POST using your own form as well.

Attention : I am not responsible for the accuracy of results as www.doenets.lk might not have officialy released the results when you use this.

Sunday, March 21, 2010

How to enable mod rewrite on Apache

Tired up trying to enable mode rewrite on apache? Try this link which explains it quite well.
http://drupal.org/node/134439

Sunday, December 6, 2009

Serialize Javascript to PHP

When interacting Javascript with PHP, you might need to serialise any Javascript data type into the PHP serialized format, where it can be unserialized using PHP. The following function will serialize any Javascript data.
function serialize(mixed_value) {
    var _getType = function(inp) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            match = cons.match(/(\w+)\(/);
            if (match) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';

    switch (type) {
        case "function":
            val = "";
            break;
        case "null":
        case "undefined":
            val = "N";
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
            val = "s:" + mixed_value.length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") {
                    continue;
                }

                okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
                vals += serialize(okey) +
                        serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
    }
    if (type != "object" && type != "array") {
        val += ";";
    }
    return val;
}
The code fragment below will do the serializing in Javascript and the later code will unserialize it in PHP.
var array = ["ruchira", "wageesha"];
//serialize value
var serialize_array = serialize(array);
$array = unserialize($_POST['array']);

Friday, September 11, 2009

Javascript Popup - close after redirect

If you want to redirect the popup window to another location (a page or a file download) and then close the window, you can simply use the following javascript.
<script type="text/javascript">
    window.opener.location = "new_page.htm";
    window.close();
</script>

Sunday, August 30, 2009

Find and Replace using grep and sed

Following command finds all the occurences of <br> in all .xml files in the current directory, and replace those <br> with <br/> . Please note that < and > have been surrounded with [ and ] in the sed command. / in the <br/> tag has been escaped using \. If you want to replace foo with bar, then you can just use those words.
find . -iname "*.xml" | xargs grep -l "<br>" | xargs sed -i -e 's/[<]br[>]/<br\/>/g'
find . -iname "*.xml" | xargs grep -l "<br>" | xargs sed -i -e 's/foo/bar/g'
A more general form of the expression would be
find . \( -name "*.php" -or -name "*.html" \) | xargs grep -l 'TEXT TO SEARCH' | xargs sed -i -e 's/TEXT TO REPLACE/REPLACEMENT STRING/g'

Thursday, August 27, 2009

Rename Files with find command in linux

If you want to rename files recusivelly using linux find command, here are two example usage of it with exec and xargs. This command will convert all the *.xml files into *.html files.

Using xags
find . | xargs rename 's/\.xml$/\.html/i'
Using exec
find . -exec rename 's/\.xml$/\.html/i' {} +
Above commands recurrsively replace files within inner directories too. If you want to do it for current directory only you can use -prune.
i.e
find . -prune | xargs rename 's/\.xml$/\.html/i'
find . -prune -exec rename 's/\.xml$/\.html/i' {} +

Monday, August 10, 2009

Multiple Joins on the same table

Here are few example queries which shows how to use multiple inner joins with sql.

SELECT Fruit1, F1FruitName = F1.FruitName, F1FruitCost = F1.FruitCost,
Fruit2, F2FruitName = F2.FruitName, F2FruitCost = F2.FruitCost
FROM T1
JOIN T2 F1 ON Fruit1 = F1.Fruit_ID
JOIN T2 F2 ON Fruit2 = F2.Fruit_ID
If you want each fruit on it's on line(Record set), then it looks like
SELECT UserID, Fruit1 AS FruitID, FruitName, FruitCost
FROM T1
JOIN T2 ON Fruit1 = Fruit_ID
UNION
SELECT UserID, Fruit2 AS FruitID, FruitName, FruitCost
FROM T1
JOIN T2 ON Fruit2 = Fruit_ID
Here is two equivalent queries where one of them uses join and other doesn't.
SELECT codice, 
other1.value AS value_one, 
other2.value AS value_two
FROM main
LEFT OUTER JOIN other as other1 ON (main.id = other1.id_main AND type = 'type_one')
LEFT OUTER JOIN other as other2 ON (main.id = other2.id_main AND type = 'type_two)
is equivalent to
SELECT codice,
(SELECT value FROM other WHERE id_main = id AND type = 'type_one') AS value_one,
(SELECT value FROM other WHERE id_main = id AND type = 'type_two') AS value_two
FROM codice
Query below will give you a clear idea about how to use mutliple joins in the same query. Hope this will help you to understand the usage.
SELECT u1.displayname AS Player1
, u2.displayname AS Player2
, u3.displayname AS Player3
, u4.displayname AS Player4
, u5.displayname AS Player5
, u6.displayname AS Player6
FROM (
(
(
(
(
DCMLeagues AS L
INNER 
JOIN Users AS u1 
ON u1.userid = L.player1
)
INNER 
JOIN Users AS u2
ON u2.userid = L.player2
)
INNER 
JOIN Users AS u3 
ON u3.userid = L.player3
)
INNER 
JOIN Users AS u4 
ON u4.userid = L.player4
)
INNER 
JOIN Users AS u5 
ON u5.userid = L.player5
)
INNER 
JOIN Users as u6 
ON u6.userid = L.player6