Wednesday, December 9, 2009

Locale on Ubuntu

Using setlocale isn't easy. You have to know the exact name of the language package you want to use,

On ubuntu, to see the list of available options, we can perform "locale -a". To install a new language, "apt-get install language-pack-**-base" and then restart apache.

Thursday, June 5, 2008

Tuesday, March 4, 2008

A model example

In a few hours I've created a trivia in CodeIgniter allowing send questions and answer some that other users sent.

The code is simple, but the interesting thing I want to show is the work of the models. In application/libraries/MY_Model.php the definition of the models is extended. Then we have the methods "get", "fetchlist", "count" receiving the type of object and an array of filters wanting to apply.

How are translated filters?
In general, the filters are conditions database. But it also allows "limit", "start", "orderby" and "orderbydir" which is to limit the amount of registration, change the start (offset), the column to sort and how (ASC/DESC) , respectively.
And even more, you can define filters themselves in each model. Defining a method "filter" can set own conditions. The method receives three arguments: object type, key and value. The function should return TRUE if they have a management own filter or FALSE if not. In the example, this can be seen in the model trivia.

What other methods have defined for the models?
Methods save, update, insert and delete receive an object and store it in the database. These objects should be objects that extend the Data_object class, be prefixed with "obj_" and must be called as the table it uses. Each must define the attributes that will be stored in columns in the database.

Download example

Tuesday, February 26, 2008

Caching data

If we are looking for ways to improve our website performance, the cache is the key.

There are many ways to do this, the most common cache is full pages, but sometimes this is not possible. In these cases, the main thing is to locate the slowest processes and try to save their outcome. To that end, the best thing is to use memcache that allows us to keep one variable (serialized, as a string) and recover very quickly.

A more generic solution can be implemented in a class that receives data to store, allowing change the way of doing (memcache, database, files), if necessary to migrate the code to a server that does not have the necessary technology.

Sunday, February 24, 2008

Surviving without GET parameters

CodeIgniter does not allow the use of variables GET default, to allow the "friendly URLs" However, parameters can still be passed.

Suppose a forum, and we want to pass the item number to display the comments. In PHP classic the URL would be view_thread.php?thread_id=1234 but in CI, on the other hand, we can make thread/view/1234. Then the controller is thread, the method view and parameter aditional is 1234.

How to recover the value of this parameter?
We have two options. We can define it as a parameter of the method view or through the URI class.
The first alternative would be something

function view($ id = 0)
(
// And it is defined $ id
)

The second option is

function view()
{
$id = $ this->uri->segment(3);
// And it is defined $id
}

The first seems more beautiful, right? Maybe someone will call attention to the parameter = 0 ... Because the user defines the URL the visitor may not send the parameter and this would generate a PHP fatal error, unrecoverable. In this way, we can do that if the id is empty, the user is redirected to an error page or to the list of items ... Basically, fail gracefully.

This case is simple. However becomes more complicated when the parameters are many. For example, to paginate the theme that we are showing, we can add a fourth parameter, leaving

function view($id = 0, $page = 1)
{

}

But things can be even more complicated when there are multiple parameters and not necessarily in order of importance (in the previous case, always require an ID, but not always a page number).
Suppose an advanced search form, within the same forum. In addition to keywords, we can select a range of time and a particular user. If we try to apply the same approach, come to something as confusing as

function results($keywords = '', $from_date = '',$to_date = '', $user = 0)
{
// ...
}

What seems more annoying than friendly. There are several possible solutions.

Sessions
One possible option is to send the form to a page and store the data in the session, and then knowing that sought to navigate through the session data. Against: if a user does not two searches can "surf" (enter items and return, or use the paging) simultaneously. Against II: can not be shared or saved as bookmarks page of search results.

Uri Assoc
While we can not do search_results.php?keywords=...&from_date=...&to_date=... We can make a URI associative and use search/results/keywords/.../desde_fecha/.../to_date/... And then take advantage of the method of the URI class uri_to_assoc to achieve a simple associative array.

Hash
Finally, we may use the URI to send an identifier of the search. For example search/results/bda90d48eda6d07d961b3ec26216fe05 and keep our database criteria mean "bda90d48eda6d07d961b3ec26216fe05".