Monday, April 24, 2006

Find numbers of day in a month

Well, that may be a tricky question but if you are oversmart, well that is no problem at all. take a look at the following code





so do you get the point? when you just set the day=0, that also means the last day of previous month.

Dont use "d" in your dateformat string cause it wont return the expected value for PHP 5.1.1 and laters. Use "t" which works pretty fine for all PHP versions.

Find numbers of day in a month

Well, that may be a tricky question but if you are oversmart, well that is no problem at all. take a look at the following code


//total number of days in march, 2006
echo Date("t",strtotime("04/0/2006"));
?>


so do you get the point? when you just set the day=0, that also means the last day of previous month.

Dont use "d" in your dateformat string cause it wont return the expected value for PHP 5.1.1 and laters. Use "t" which works pretty fine for all PHP versions.

Wednesday, April 19, 2006

Creating Action Queue for AJAX Calls

In ajax, there are several cases where you need to call a bunch of actions synchronously. That means one action must finish before another one called. You can do it by checking the status of ajax request of previous calls and if that was in finished state, you can call next action. This process may be resource extensive if you dont care for checking. So here is a simple actionChain object which will help to put numbers of actions in a chain (or you better say, queue) and call them one of after one smartly. I just demonstrate how it works

<script>
var actionChain = {
curIndex: 0,
actions: new Array(),
fire: function()
{
if (this.curIndex<this.actions.length)
{
alert(this.actions[this.curIndex]);
this.callback();
}
else
{
alert("finished");
}
},
increase: function()
{
this.curIndex++;
},
addAction: function(actionName)
{
this.actions[this.actions.length] = actionName;
},
initialize: function()
{
this.curIndex=0;
this.actions = new Array();
},
callback: function(){}
}

actionChain.callback=checkVar;

function checkVar()
{
actionChain.increase();
actionChain.fire();
}

for(i=1;i<11;i++)
{
actionChain.addAction("hello"+i);
}
actionChain.fire();
actionChain.initialize();
for(i=1;i<4;i++)
{
actionChain.addAction("hello"+i);
}
actionChain.fire(); </script>


so here you see, an actionChain object is created and then we add some actions. Then we call the fire() method. This fire() method simply invoke callback function with current action in queue. And the call back function just increases the counter. So when the counter reaches at final offset, it will terminate.

Here I show a basic demo, just modifying it a bit you can create your full fledged ajax action chain object ( i mean a proper object which stores the action, their url, parameters and response callback etc...)

Thanks

Sunday, April 16, 2006

Generic Smarty Template to make Report from SQL Query

Smarty is an awesome template engine for php developers. It gives you ultimate flexibility over your template. Short learning curve, easy syntax and performance makes it really unbetable. Yesterday night I designed a generic smarty template which can take an array of records and display them in a table.

source of autoreport.tpl

<table class="auto_table">
{if $headers ne ""}
<tr class="auto_header_row">
{section name=header_offset loop=$headers}
<th class="auto_header">{$headers[header_offset]}</th>
{/section}
</tr>
{/if}
{foreach item=row from=$data}
<tr class="auto_data_row">
{section name=column_offset loop=$row}
<td class="auto_data">
{$row[column_offset]}
</td>
{/section}
</tr>
{/foreach}
</table>

So how to use that template? take a look at this code

<?
// connect to your db, say mysql
$records = array();
$result = mysql_query("select name, roll from tablename");
while ($row = mysql_fetch_row($result))
{
$records[] = $row;
}

$smarty->assign("data",$records);
$smarty->assign("headers",array("name","roll"));
$smarty->display("autoreport.tpl");
?>

Thats it!!

Saturday, April 15, 2006

Zephyr Beta 2.0 Released

visit : Zephyr

After a long time since the beta 1.0, today we released the second beta versionof zephyr. It is more optimized than the previous one. This version comes with many new features but major improvements are

1. Prototype is now a part of Zephyr
2. SQLite support
3. Multilevel Filters
4. Multiple pre action processors
5. Supports Multiple database concurrently (From same/different providers)
6. Overall performance tuning
7. Embeded script execution
8. Multibyte strings are supported
9. Package Initializers

We have worked hard to make this release. We hope we can release its final version pretty soon.Download : Here

Saturday, April 08, 2006

Zephyr Beta 2 : Upcoming Features

Yesterday night I added some great functionality in zephyr. Developers can use “Filters” in zephyr packages. In primary stage I just implemented two type of filters, one is “pre-filter” and another is “post-filter”. Pre filters are a simple object which is invoked when visitors input data. Before passing these data to actions, zephyr will call “process()” method of pre filter object. After processing them, these data will be passed to action.

Pre filters are of great use of you want to implement some sort of input filtering. Beside submitted data from users, Prefilters will also receive the action name which is responsible to handle those data. So it will remain flexible to developers.

Second type of filter that I implemented is “postfilter”. After rendering the view, this filter is invoked with the rendered content and action name. So developers can polish the rendered output if they need to.

Day by day I will also add filter in different execution level of zephyr core. I am also planning to implement JSON support in zephyr. Here is a comprehensive list of implemented features for upcoming version of zephyr beta 2.0

  1. Multiple Database Support (For both same provider and different provider)
  2. SQLite support
  3. Prototype is now a part of zephyr. So users can take advantage of this great library.
  4. Automatic display of “loading” image when an action is called.
  5. Multiple PHP file loading capability
  6. Multiple Javascript file loading capability
  7. Multiple pre action processors
  8. Supports exchanging non printable characters as response and request.
  9. Easy loading of PEAR packages.
  10. Package Initializer, a special object that will be invoked while loading the package for first time.
  11. Abstract DBInfo class, to simplify sharing db information.
  12. flexible execute() method in DAO
  13. Better exception management.


Friday, April 07, 2006

Fixing Multiple Database bug in adoDB

popular data access layer for php, adoDB can support multiple databases from different provider at time, but not from same provider. what I mean is if you instantiate two adoDB connection like this

$db1 = &NewAdoConnection (“mysql”);
$db1 = &NewAdoConnection (“oracle”);

then you can run queries simultaneously from these connections. but if you want to access two database from same provider like this

$db1 = &NewAdoConnection (“mysql”);
$db1 = &NewAdoConnection (“mysql”);
$db1->PConnect(logininfo_for_first_database)
$db2->PConnect(logininfo_for_second_database)

then only last action will remain active and if you use $db1->execute(), it will actually works on second database. if you want to access any table from first database, it will generate an error. I think this is an internal bug in adoDB.

How to fix this bug?

if you open adodb-mysql.inc.php and goto function _connect() you will find this line at the end

1. if ($argDatabasename) return $this->SelectDB($argDatabasename);

now goto SelectDB function and see what it contains actually.

// returns true or false
function SelectDB($dbName)
{
$this->databaseName = $dbName;
if ($this->_connectionID) {
return @mysql_select_db($dbName,$this->_connectionID);
}
else return false;
}

so it basically returns a boolean value, mysql connection link is stored in $this->_connectionID variable. Now modify this code like this

1. add this line at the declaration section of adodb-mysql.inc.php

var $__db = array();

2. modify the SelectDB function like this

// returns true or false
function SelectDB($dbName)
{
$this->databaseName = $dbName;
if ($this->_connectionID) {
@mysql_select_db($dbName,$this->_connectionID);
$this->__db[$this->_connectionID] = $dbName;
return $this->_connectionID;
}
else return false;
}

3. Now open the _query function and modify it like this

// returns queryID or false
function _query($sql,$inputarr)
{
//global $ADODB_COUNTRECS;
//if($ADODB_COUNTRECS)
$this->SelectDB($this->__db[$this->_connectionID]);
return mysql_query($sql, $this->_connectionID);
//else return @mysql_unbuffered_query($sql,$this->_connectionID);
// requires PHP >= 4.0.6
}

That’s it!!, now your adoDB supports multiple databases from same provider.

Smarty Book
Smarty PHP Template Programming and Applications In Detail


Smarty is a templating engine for PHP. Designers who are used to working with HTML files can work with Smarty templates, which are HTML files with simple tags while programmers work with the underlying PHP code. The Smarty engine brings the code and templates together. The result of all this is that designers can concentrate on designing, programmers can concentrate on programming, and they don't need to get in each others way so much.
Even if you are developing a site on your own, Smarty is a powerful way to make your code clearer to you and others, as well as easier to debug and modify later.


What you will learn from this book

This book is a comprehensive guide to all aspects of using Smarty. It will help you to:

* Install and configure Smarty on your Web server
* Understand how Smarty affects your web site architecture, and build site foundations that make the most of what Smarty offers
* Designers will learn to work with templates that contain variables and logic, to modify layouts or content of Smarty web sites
* See how Smarty caching can improve the performance of your sites
* Develop custom Smarty functions and plug-ins to incorporate into your templates

Approach

Using a step-by-step approach based on realistic examples, the expert authors show you how to use Smarty in your own PHP development.

Who this book is written for

This book is written for PHP developers who want to use Smarty templates in their development, and for designers who are working with PHP developers who are using Smarty.

Author(s) : Hasin Hayder, Joao Prado, Lucian Gheorge