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".
Sunday, February 24, 2008
Subscribe to:
Post Comments (Atom)

5 comments:
Great post. Thank you
I'm trying to do the following:
function item ($lcoation,$itemname="", $itemid="")
If has $itemid -> load (singleitemview)
If has $itemname -> find like name -> If 1 ->load (singleitemview) else >load (multipleitemview)
If only has $location -> find items in location -> If 1 ->load (singleitemview) else-> load (multipleitemview)
I want to paginate the results if there are multiple items in a location
I want to paginate the results if there are multiple items with that name
I don't want to break up my seo friendly links.
I found this site using [url=http://google.com]google.com[/url] And i want to thank you for your work. You have done really very good site. Great work, great site! Thank you!
Sorry for offtopic
Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!
1日5万円~が手に入るサイドビジネスのご案内です。男狂いのセレブ女性はネットで知り合った男を次々に金の力で食い散らかしています。そんな女性を手玉にとって大金を稼いでみませんか
最近寂しくて困っています。夜一人で寝るのが凄く寂しいです…隣で添い寝してくれる男性いませんか?見た目とか特に気にしません。優しくて一緒にいてくれる方大歓迎☆一緒に布団で温まりましょう♪shart.enamorado.de-me@docomo.ne.jp
Post a Comment