Friday, January 21, 2011

Jquery: check if checkbox is checked

In Jquery it is very easy to find if a checkbox is checked or not.

Example:

if($('#mycheckbox').is(':checked')){
//do something
}else{
//do otherwise
}

Jquey: count all checked checkbox

In Jquery it's very easy to find all checkboxes that are checked. We can use filter() method of Jquery.

eq.

var $allcheckbox = $('input[type=checkbox]');
alert($allcheckbox.filter(':checked').length);

Tuesday, April 27, 2010

Profile php code with xdebug for fast performance

There are times when we need to look into the execution path of php. We need to find out what is going under the hood of php, What and how many objects are created, What function take most time to execute.

This is where xdebug extension for php come handy. Xdebug 2 is a most important extension for a php programmer as it provides various ways of debugging and analysing your code on your development server. The installation of xdebug is very easy. I am going to show how it can be installed and enabled it in xampp for windows, but installation on other OS is same.

xampp already comes with xdebug, but that not work for me, so I download a new dll for xdebug.org. Once you have the dll, follow the given steps to install it :

1. copy the xdebug dll in php ext folder.

2. edit the php.ini (one that resides in apache bin folder)

a. comment out any other zend_extension entries
b add following configuration

[XDebug]
zend_extension_ts = "C:\xampp\php\ext\php_xdebug-2.0.0-5.2.2.dll"
xdebug.remote_enable=On
xdebug.remote_host="localhost"
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.profiler_enable=1
xdebug.profiler_output_name=cachegrind.out.%t
xdebug.profiler_output_dir="C:\xampp\tmp"

3. restart apache

4. confirm that xdebug is enabled using phpinfo();

The above configuration will create a profile dump file for each request. If you don't want to create this file for each request, You can also selectively enable the profiler with the xdebug.profiler_enable_trigger setting set to 1. If it is set to 1, then you can enable the profiler by using a GET/POST or COOKIE variable of the name XDEBUG_PROFILE. The FireFox 2 extension that can be used to enable the debugger (see HTTP Debug Sessions) can also be used with this setting. In order for the trigger to work properly, xdebug.profiler_enable needs to be set to 0.

View the Cachegrind files:

WinCacheGrind is the best way to get useful information out of your cachegrind output file on windows (Linux users can use KCachegrind) . It provides a simple tree view of the PHP execution with references to class, function and filenames. Most importantly, it tells you how long each function call took.

For more information on configuration of xdebug, visit http://xdebug.org/docs/profiler

Thursday, December 25, 2008

Disable browser caching in asp.net

To improve speed and response to user, some browsers cache the whole webpage. when user request for same page, instead of going to server, browsers server this page from the cache.
This approach can create problem, browser caches a secured page, which user should see only if he is login. There is a way to force the browser not to cache an ASP.NET web page and you need to add only 1 line of code to your ASP.NET web pages in order to disable browser caching. 

Add the following line on top of the Page_Load event handler and your ASP.NET page will not be cached in the users browsers: 

Response.Cache.SetCacheability(HttpCacheability.NoCache)

Caching in asp.net

One of the techniques developers commonly relied on to speed up processing of request is the use of caching. When information is cached, it stays cached either indefinitely, until some relative time, or until some absolute time. Most commonly, information is cached for a relative time frame. That is, our database information may be fairly static, updated just a few times a week. Therefore, we might want to invalidate the cache every other day, meaning every other day the cached content is rebuilt from the database.

There are a number of classes in the .NET Framework designed to aid with caching information. ASP.NET supports three types of caching for Web-based applications:
  • Page Level Caching (called Output Caching)
  • Page Fragment Caching (often called Partial-Page Output Caching)
Output Caching : 
Page level, or output caching, caches the HTML output of dynamic requests to ASP.NET Web pages. Output caching is easy to implement. By simply using the @OuputCache page directive, ASP.NET Web pages can take advantage of this powerful technique. The syntax looks like this:

<%@OutputCache Duration="60" VaryByParam="none" %>

The Duration parameter specifies how long, in seconds, the HTML output of the Web page should be held in the cache. 
The VaryByParam is a useful setting that can be used to cache different "views" of a dynamic page whose content is generated by GET or POST values. For example, you may have an ASP.NET Web page that reads in a Part number from the QueryString and displays information about a particular widget whose part number matches the QueryString Part number. Imagine for a moment that Output Caching ignored the QueryString parameters altogether (which you can do by setting VaryByParam="none"). If the first user visited the page with QueryString /ProductInfo.aspx?PartNo=4, she would see information out widget #4. The HTML for this page would be cached. The next user now visits and wished to see information on widget #8, a la /ProductInfo.aspx?PartNo=8. If VaryByParam is set to VaryByParam="none", the Output Caching engine will assume that the requests to the two pages are synonymous, and return the cached HTML for widget #4 to the person wishing to see widget #8! To solve for this problem, you can specify that the Output Caching engine should vary its caches based on the PartNo parameter by either specifying it explicitly, like VaryByParam="PartNo", or by saying to vary on all GET/POST parameters, like: VaryByParam="*".

Partial-Page Output Caching:
Partial-Page Output Caching, or page fragment caching, allows specific regions of pages to be cached. this can be done by caching the usercontrols.  This caching can be done by adding an OutputCache directive at the top of the User Control.


Tuesday, December 9, 2008

Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true

If you get this error when calling web service asynchronously, add the Async=”true” attribute in the page directive at the top of page.




Wednesday, November 12, 2008

Create Thunbnail images in php

Here is the code that you can use to create thumbnails in php.

This method only support jpeg, jpg and png files.

function createthumb($originalfilename,$thumbfilename,$new_w,$new_h)
{
$system=explode(".",$name);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y)
{
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y)
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}