Post your trick

initializing jcarousel without page refresh

Hi, I needed to change the contents (li) of the my carousel whenever a tab is click
so i used the example of dynamic content loading through js over here
http://sorgalla.com/projects/jcarousel/examples/dynamic_javascript.html
But i need to reset the carousel on tab change so for that the code is
var carousel = $(’#mycarousel’).data(’jcarousel’);
carousel.reset();
This will reset the caraousel and then  again I can load different content in it.


handling custom form in wordpress

I had a custom login form in WP(3.1.2) and the problem i was facing is whenever the form was submitted i used to get empty $_POST array.This was my code

<form method=”post” action=”<?php echo bloginfo(’url’).’/?page_id=221′; ?>”>

After some google search I found the solution which was :

<form method=”post” action=”<?php echo $_SERVER['PHP_SELF'].’/?page_id=221′; ?>”>

which means if you are submitting the form on the same page use $_SERVER else you wont get the data in post


Passing parameters to a function called with setTimeout

setTimeOut(myfunction(parameter),myTimeout);

The above line will not work. To achieve this you will have to do it with closure

setTimeOut(function(){myFunction(parameter)},myTimeout);




a simple but useful hack regarding annotated spring form and binding result

I was adding an form to an annotated spring controller with the use of binding result parameter.Code for the same is :

@RequestMapping(value = “/save.do”)
public String save(@ModelAttribute(”advertisement”) Advertisement advertisement,HttpServletRequest request,BindingResult result){

//your code

}

While executing I got the error :

Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature!

After some google search I figured out that  :

“The BindingResult parameter must be positioned directly after the corresponding model argument that is being validated.”

Which means :

@RequestMapping(value = “/save.do”)
public String save(@ModelAttribute(”advertisement”) Advertisement advertisement,BindingResult result,HttpServletRequest request){

//your code

}



send an array in ajax and get it in java

Here’s a simple ajax request :
$.ajax({
url : “xyz/functionName.htm”,
dataType : “html”,
type : “post”,
data : { ‘countryName’ : countryName, },
success : function (resp) {

}
});
Now on the server side,to get the value of post variable we normally write :
String name = request.getParameter(”countryName”);

But suppose the data I am sending is array
data : { ‘countryName[]‘ : ['india','australia','africa'] }
Then to access the array on the server side we have to write :
String [] name = request.getParameterValues(”countryName”);


accessing outer class variable within inner class

the syntax to access outer class variable within inner class is OuterClassName.this.varName.

Ex:

Class Outer {

int a = 100;

public Outer{

Inner inner = new Inner();

inner.print();

}

Class Inner {

public print(){

System.out.println(”Value of a is ” + Outer.this.a);

}

}

public static void main(String[] args)
{
new Outer();

}

}


how to create an object of the topmost class in php

$data = array(
‘currency’ => $order->getOrderCurrencyCode(),
‘date_added’ => date(’Y-m-d’)
);
$order_query = new stdClass();
$order_query->row = $data;


php array to sql query

I came across a case where i was getting a post in which i get column names of my table in keys, so i used the following code

$data = array(’column_1′ => 1, ’column_2′ => 45, ’column_3′ => 25 );

$sql = http_build_query($data, ”, ‘, ‘);

$query = “UPDATE table_name SET ” . $sql . ” WHERE id = ‘ 24 ‘ ” ;

Note: This cant be used if you values are coming in string


Passing url in GET parameter of API

Its become very difficult to pass url as a parameter to the api. And it becomes more pain when we have to pass it in GET of that of the api url.

e.g. http://apiurl.com?&name=jimit&url=http://myurl.com&id=241

Now in this the id parameter of myurl goes as GET parameter of apiurl. To overcome such situation we can use HTML URL Encoding

http://apiurl.com?&name=jimit&url=http://myurl.com%26id=241

you can find the complete list of HTML URL Encoding here.


PHP converting all Errors to Exception

<?php

function exceptions_error_handler($severity, $message,$filename, $lineno) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
set_error_handler(’exceptions_error_handler’);
?>


Post A Trick !

How does "Kodetricks" work?

We at kodeplay like to share knowledge. With Kodetricks, even you can join us. All you need to do is post a programming related trick if you have one or rate a trick if you like someone else's.



Technology

Contact Us