Friday, 3 July 2015

How to use plug-in's component in our application controller - CakePHP 3 0

Here is a small How to about creating and using plugin component in application controller.

Here we are going to create 'TestingComponent' inside of 'Test' plugin, Then get 'Hello world' text from component function and print it from our application's controller.

first create 'TestComponent' class in 'YourCakeDirectory\Plugins\Test\src\Controller\Component\TestComponent.php'

Here is the most important thing we have to do with our plugin load in CakePHP bootstrap. Before calling plug-in component in application controller, we need to auto load it in CakePHP. If our plugin created using bake method, we should autoload this class in 'composer.json'. If our plugin was manually created, then we should use 'autoload' parameter as a value 'true' as a configuration parameter while loading this plugin in application's 'YourCakeDirecctory\Config\bootstrap.php'.
The bellow one is a way to load component in controller on the fly.
Now we are going to load plugin's component. So we need to use our plugin component name followed by plugin name and dot('.'). Here we are going to load 'TestingComponent' from the plugin 'Test'. So, the code will be like the bellow,

Now we will get the output of plugin's component string 'Hello World' which is echoed from our application's controller.

Have any doubt, feel free to comment here!

Monday, 4 May 2015

How to get relative path in javascript 0

Problem

We can't easily get relative paths in javascript as like server side scripts. We have use our own logic to get it. Hard coding server root path in javascript is really bad idea. So, We are going to get our server root path from already loaded resource.


Why I need this?

If you are going to do use redirection or want to load any other resource which is located another directory in your server root directory, you may need server root path. On that time you should need this.

Solution

 Assume, we are having "http://sampledomain.com" and our assets in webroot directory. consider we have an external javascript file called "sample.js", add id attribute to your script tag.

And simply get the location of your javascript file during run time using javascript by parsing the DOM for the 'src' attribute that referred it:

Now we can call getRootPath() function to get the server root path. So, by our example our javascript function getRootPath() will return "http://sampledomain.com"

We can also use this as webroot path by removing "webroot" string from the replace function. If we did this, we will get "http://sampledomain.com/webroot" from the returned string.


Have any doubt, feel free to comment here!

Thursday, 26 February 2015

How to get sql dump using command prompt in WAMP 0

To get a mysql dump of a database in wamp, first we need to set our mysql path using set path command. The example is given below.
  

set path=c:\wamp\bin\mysql\mysql5.1.36\bin

Replace c:\wamp\bin\mysql\mysql5.1.36\bin with your mysql bin directory path.

Then use the below command to export our database.
 
mysqldump -u YourUserName -p YourDatabaseName > sqlDumpFileName.sql

YourUserName will be our mysql username. And YourDatabaseName will be our DataBase name. Then command prompt will ask for the mysql password. Your should enter your mysql password to get your sql dump of your database.

Have any doubt, feel free to comment here!

Tuesday, 4 November 2014

Live Digital Clock - Javascript 0

Here is the code for Live digital clock.

Just we are going write a function to get the current time and display it in span. We are calling that function for each 1000 milli seconds. so, live clock is working.

var int=self.setInterval("clock()",1000);
function clock(){
var now=new Date();
var time=now.toLocaleTimeString();
document.getElementById("clock").textContent=time;
};
 Here is the live Demo

Thanks Christian White

Monday, 6 October 2014

Find how many times a search phrase is found in a string 0

Here is the function to tell how many times a search phrase is found in a string.

Note: It's case sensitive.

function howMany(str, term){
return str.match(new RegExp(term, 'g')).length;
};

var text='Visit WHAK.com for WHAKy things!';
var phrase='HAK';
alert(phrase+' is found in\n'+text+'\n'+howMany(text,phrase)+' times.')

Thanks Christian White

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 0

$_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 0

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.