Wednesday, May 16, 2007

Squash That Bug!

Sometimes programming goes awry for the slightest of errors. In our lingo, we call those nasty things bugs.

I just spent maybe half an hour debugging a code that went wrong just because I forgot the "$" in front of a couple of my variable names.

Aargh. That's programming for you...

Anyways, I've learnt quite a bit of stuff on PHP by now. One of which is file operations such as reading & writing to the file.

Here, I'm gonna be awful nice & share...

Reading from a file:

$c_file = "counter.txt";
$check_counter = fopen($c_file, "r");
$curr_counter = fread($check_counter, filesize($c_file));
fclose($check_counter);

The code reads a counter value stored in the file counter.txt. $check_counter is a file handler which opens the file for reading (specified by the parameter "r"). $curr_counter is the variable which is assigned the value of the counter read from the file. The last line quite obviously closes the file after reading.

Writing to a file:

//read counter value from file counter.txt
$counter_file = "counter.txt";
$get_counter = fopen($counter_file, "r");
$counter = fread($get_counter, filesize($counter_file));

//close the counter.txt file
fclose($get_counter);

//open counter.txt file for writing
$alter_counter = fopen($counter_file, "w");

//decrement counter value by 1
$new_counter = $counter-1;

//write new decremented counter value to counter.txt then close the file
fwrite($alter_counter, $new_counter);
fclose($alter_counter);

This code writes a new counter value to the file counter.txt in 2 steps. First the value is read from the file then the file closed, then in the 2nd part minus 1 from $counter & store the new value in the file counter.txt.

I'm sure there are more efficient ways of doing this. If any veteran out there sees this & doesn't mind sparing me a lesson on how, please drop a comment.

& in the meantime, I spent more than half my day trying to figure out how to either call javascript functions from within PHP script or using javascript code to read my counter value from the file. All just to find a way of disabling the form elements when the registration limit is reached (determined by the counter value of zero). Only to talk to my boss later in the evening & realise that he doesn't mind me just echoing a statement in the window to the effect that the user is informed that their registration is rejected because the registration limit has been reached.

Hmmph.

Oh well, at least I am learning new things everyday. I did learn that there is such a thing as server side Javascript. & I did learn those small bits about event handling for stuff that should happen when HTML code loads onto a page. It's done like this:

< onload="check_limit()">

check_limit() being the javascript function, of course. The comment tags are just so that this post gets by the blogger specifications since HTML tags aren't technically allowed on the post :P

Okies, enough rambling. I aim to be home soon.

1 comment:

Unknown said...

U did this in the Office??? i didnt know u hav tat much time