Sunday, 24 August 2014

MySql return rows matching year month 0

If we want to get rows by filtering given month and year, We can use 2 popular ways.

  1. EXTRACT()
  2. BETWEEN
1. EXTRACT( ):
The EXTRACT() function is used to return a single part of a date/time, such as year, month, day, hour, minute, etc.

SYNTAX:
EXTRACT(unit FROM date)

This function have a following units to use.
MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR, SECOND_MICROSECOND, MINUTE_MICROSECOND, MINUTE_SECOND, HOUR_MICROSECOND, HOUR_SECOND, HOUR_MINUTE, DAY_MICROSECOND, DAY_SECOND, DAY_MINUTE, DAY_HOUR, YEAR_MONTH.

This function is really useful when working with dates in mysql. Ok, Let we see how to use EXTRACT() in this case,

SOLUTION 1 :
SELECT * FROM table
WHERE EXTRACT(YEAR_MONTH FROM timestamp_field) = EXTRACT(YEAR_MONTH FROM NOW())

We can replace NOW() with our date like this below code.

SELECT * FROM table
WHERE EXTRACT(YEAR_MONTH FROM timestamp_field) = EXTRACT(YEAR_MONTH FROM '2014-08-25')

2. BETWEEN :
We can use BETWEEN clause to replace a combination of "greater than equal AND less than equal" conditions.

SYNTAX:
BETWEEN A AND B

Ok, Let we see how to use BETWEEN in this case,

SOLUTION 2:
SELECT *
  FROM myTable
 WHERE date_column BETWEEN '2014-08-01' AND '2014-08-31
We should make sure our date_column have a date format like 'Y-m-d' when using this method. Or, simply change your timestamp format to DATE_FORMAT().

Note:
You could extract the year and month using a function, but that will not be able to use an index. 
If you want scalable performance, you need to use BETWEEN.

 Still have any doubt? Feel free to comment here...


Monday, 18 August 2014

How to resize image diagonally using javascript 0

In general we don't like to see our images stretched in view. In this case, If we want to reduce size diagonally, what should we do?

We can do it by changing width property of image.

  • First We need to get original width of image using javascript's naturalWidth property. 
  • Then Multiply it with 0.5 to get 50% reduced image.
  • If you want 2x larger image, then multiply it with 2. like this below code,
domElement.width = doemElement.naturalWidth * 0.5;
  • Then assign resultant number to image width.

Still you have doubt??? see this below DEMO. It will make you understand what I'm saying.

Related Post : HTML DOM Image naturalWidth, naturalHeight Property

HTML DOM Image naturalWidth, naturalHeight Property 0

The Javascript's naturalWidth and naturalHeight properties will return original width and height of an image.
For an example, If we are using an image with 350px original width and 225px original height, It will return the same height and width even we made changes in DOM's height and width property.

The following DEMO will make you understand more what I'm saying. 


In this DEMO the original image size of image is 350 x 225 px. We are also changed image width and height property. But now also it's returning the actual size.



Saturday, 9 August 2014

$_GET vs. $_POST - PHP 2

$_GET and $_POST
  • Both GET and POST create an array.
  • e.g. array(key=>value, key2=>value2, key3=>value3, ...).
  • This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.
  • Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.
  • $_GET is an array of variables passed to the current script via the URL parameters.
  • $_POST is an array of variables passed to the current script via the HTTP POST method.

When to use GET?
Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.
GET may be used for sending non-sensitive data.
Note: GET should NEVER be used for sending passwords or other sensitive information!
When to use POST?
Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send.
Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server.
However, because the variables are not displayed in the URL, it is not possible to bookmark the page.


Thursday, 7 August 2014

How reloadAfterSubmit working in jqGrid 1

This reloadAfterSubmit attribute always confusing with data type. I have seen so many post related to reloadAfterSubmit not working like that in jqGrid forum and stackoverflow posts. Here I gave explanation about reload process in jqGrid.


  • jqGrid using AJAX to post data to Server Side (add, edit, delete). So, our page will not get refresh.
  • jqGrid reloadAfterSubmit : true will again load our data source to jqGrid.
  • So, If we set reloadAfterSubmit : true for JSON or XML dynamic data source, it will work. We can see updated data in jqGrid. Because, jqGrid again will get the updated data source from dynamic JSON or XML data type (data source).
  • If we set reloadAfterSubmit : true for local data, It will also work. But, We can't see any updated data in jqGrid. Because, our local data is static till we manually refresh our complete page. So, this reloadAfterSubmit : true will again load our declared local data.
So,
  • For local data data type, we should set reloadAfterSubmit : false.
  • For, JSON or XML data type, we should set reloadAfterSubmit : true.