Showing posts with label html. Show all posts
Showing posts with label html. Show all posts

Tuesday, September 15, 2009

Extract required parameters to variables with a single line of code! (PHP)

[Note: this is for those not using sophisticated PHP frameworks (ZF, CakePHP, etc.), but using plain old PHP (Smarty is still OK and great!)]

How many times have you wanted to extract selected GET or POST or COOKIE variables into local/global variables to be used easily? Something like:

$submit = (int) $_REQUEST['submit'];
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$preference = (int) $_REQUEST['preference'];

if ($submit) {
    if (!$name and !$email) {
        $error = 'Please fill in your name and email';
    } else {
        sql("insert into POST values (?, ?, ?)", $name, $email, $preference);
    }
}

And then at another page, you need to do the same thing over and over again (and you don't actually care whether it's GET or POST or...)

$start = (int) $_GET['start'];
$length = (int) $_GET['length'];
$locale = $_GET['locale'];

Solution: I have always been using this function to extract request variables to the global scope:

// first example
getVars('submit name email preference');
echo "Your name is $name"; // the variable $name is now available
// second example
getVars('start length locale');

Isn't it convenient?

How does getVars() function look like?

function getVars($vars) {
    if (is_string($vars)) {
        $vars = split(' ', $vars);
    }
    foreach ($vars as $var) {
        $GLOBALS[$var] = $_REQUEST[$var];
    }
}

The trick is to move the variables from $_REQUEST to $GLOBALS.
Now let's enhance it so that it converts the variables to int when needed:

function getVars($vars) {
    if (is_string($vars)) {
        $vars = split(' ', $vars);
    }
    foreach ($vars as $var) {
        $modifier = '';
        if (strpos($var, '/') !== false) {
            $tp = split('/', $var);
            $var = $tp[0];
            $modifier = $tp[1];
        }
        $value = $_REQUEST[$var];
        if ($modifier == 'hex') {
            $value = pack("H*", $value);
        } elseif ($modifier == 'int') {
            $value = (int)$value;
        }
        $GLOBALS[$var] = $value;
    }
}

We go back to the examples, we now have:

// first example
getVars('submit/int name email preference/int');
// second example
getVars('start/int length/int locale');

Such a useful function, Let us use it!

Tip: you can also write '/hex' to convert "414243" to "ABC"

Wednesday, August 5, 2009

Setting GWT locale using cookies

GWT represents locale as a client property whose value can be set either using a meta tag embedded in the host page or in the query string of the host page's URL.
I wanted to set the locale of my GWT application stored somewhere, so that I don't need to always append ?locale=ja or &locale=ja to the URL for every page on the site. Putting the meta tag every time the page is requested is also not an option, since you still need to let the server know the preferences of the user (session management).
For example, if I have http://www.ngambek.com/ page in Indonesian, but I want to set it to Japanese, I need to set the address to http://www.ngambek.com/?locale=ja to let the GWT system use the Japanese version of the page. Later when there is a link to http://www.ngambek.com/12345678, I would need to make it http://www.ngambek.com/12345678?locale=ja. What a burden.
I decided to use cookie to store the preferred locale, then let a javascript snippet run to set a meta tag dynamically. When the GWT module is loaded, the locale will be detected because the appropriate meta tag will have been written (in browser's memory).
So, before the GWT module script is loaded, I put these lines onto the HTML file:
var getCookie = function(c_name) {
  if (document.cookie.length > 0) {
    c_start = document.cookie.indexOf(c_name + "=");
    if (c_start != -1) {
      c_start = c_start + c_name.length+1;
      c_end = document.cookie.indexOf(";", c_start);
      if (c_end == -1) c_end = document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}

var locale = getCookie("locale");
if (! locale) {
  locale = "id";
}

document.write("<meta name='gwt:property' content='locale=" + locale + "' />");
Then, in order to set the locale, it's just a matter of setting the locale cookie and reloading the page:
function setLocale(locale) {
  document.cookie = "locale=" + escape(locale);
  document.location.href = document.location.href;
}
By the way, the internationalization feature of GWT is great! Try it if you haven't tried it before.