Pagination with search results in Codeigniter - php

With simple queries I'm able to produce pages with nice pagination links. However, if the query is generated through some sort of search filter, I'm not sure how to pass the $_POST data from page to page.
Usually I can do this ... www.domain.com/search.php?id=200&type=host&rack=3&os=redhat%5
However, with CI's URI library, I'm using "pretty urls," hence my URL is more like www.domain.com/search/page/1 Appending the rest of the variables doesn't make sense nor will I think it will work. Any ideas on how to tackle this issue?

Why don't you just store the variables you need in a user session? Here is some information about how to use a session to store information about a user with codeigniter:
http://codeigniter.com/user_guide/libraries/sessions.html

Format your URI like so:
index.php/controller/method/id/200/type/host/rack/3/os=redhat%5/page/2
You can then use $this->uri->uri_to_assoc(n) to turn that URI into key-value pairs. See:
http://codeigniter.com/user_guide/libraries/uri.html

Please have a look the link below. This article show you have to pass parameters on pagination without turn on enable query string settings.
http://pujanpiya.com.np/?q=node/37
Hope this help!

If your using the built in pagination class, just make a separate search method on your controller and provide it with the pagination info...
FYI, there is a great jQuery Plugin that takes care of alot of table related work for you. Datatables.net
I just wrote a library that handles search, pagination, sorting and limiting.. Check it out here.
http://datatables.dyndns-web.com/

Related

Defining Laravel routes when certain query strings are present

Say I want to be able to submit a search form on any page that will append a ?s= to the current url but return a SERP: https://example.com/my-page?s=foobar. (I've seen a few sites do this instead of pointing to /search?s=.* - not the way I'd do it, but it illustrates my question.)
In my Laravel web.php routes, is there currently a way to register this other than maybe using a wonky regex?
<?php
Route::get('.+?\?.+?\bs={search}', 'SearchController#search');
// This regex is probably wrong, but you get what I was going for,
// and that kinda highlights why this is not an ideal way to do it
?>
This gets uglier when, say, you want to capture multiple $_GET params:
https://example.com/my-page?s=foobar&dept=clothing
I haven't found anything in Laravel docs that let you define query string options on the Route $uri parameter. The recommended option is to just use pretty URLs (https://example.com/search/foobar), but there are definitely valid use cases for keeping query strings.
1: Create a middleware (https://laravel.com/docs/5.7/middleware)
2: Validate incoming request using $request->query('s')
3: If validation successfull return to next else redirect to wherever or display an error response
That is very simple that depends on the parts of a url. The Route class uses the "path string" of the url and you try to use the "parameters" of the url.
https://doepud.co.uk/blog/anatomy-of-a-url
In modern websites you should work with that structure because you get a better URL structure and it's much better for SEO and search engines.
You should use the parameters only in the function you call for small things which you can call over the $request->get('s'); method but then you have to redirect or you have to work in that function.
So don't fight the framework and work in that structure what is defined from the framework that all people who know the framework know how to work with it.

How to pass variables to Joomla modules?

I am trying to make a Joomla website, where entries from the SQL database would be shown depending of different queries. I would like to write a universal module, which could read in some parameters (as a normal function would do), then do a database search and display the results. I cannot find a way to do this. I can only hardcode some rules inside the module but there is no access to the module from the Joomla administrator panel. Thanks in advance.
I'm guessing here that you're tyring to build something similar to an AJAX search module, given that there are lots of these on the Joomla Extensions Directory, I would recommend looking at several of these modules to see how they did it.
Depending on your specific version of Joomla and the way you implement it you may be able to use the AJAX Interface in 3.2+ versions of Joomla
Are the URL parameters you need available on initial page load? If so, you can simply use the JApplication input object to retrieve cleansed GET/POST parameters.
$jinput = JFactory::getApplication()->input;
$myVar = $jinput->get('myVar', 'default my var value if not avail');
You could then capture the needed parameters to build out your DB query.
If the parameters are not available and the data needs to be acquired through an AJAX call, you could use cppl's solution or roll your own using this tutorial.
http://docs.joomla.org/JSON_Responses_with_JResponseJson

Codeigniter Always append something for all send request

I am finding for a good solution in codeigniter in where for every post request are made I would append something in it.
My temporary solution is to create a library and if post is found I would append something like this in my URI ?sTime=20140623101010 but this is dynamic which is basically bypassing the cache gateway that is setted in my server.
Any bright ideas that can lighten me up on this.
Thanks in advance
If your version of Codeigniter support it you can use CSRF protection to stop users reposting the same form more than once.. CI documentation Security ( if this is your problem.. )
If you want it for other reason there are different ways to create such "time" value.
Best way is to change the CI form Helper function form_open. Inside you can put what ever field you want and after that you have only to use this function when starting new form. CI documentation extending CI Helpers
Use before any function in your controller
if(!$this->input->get('time') && empty($this->input->post()))
{
redirect($url.'?time='.time());
}

Should I use array or string in link param in CakePHP HtmlHelper?

CakePHP HtmlHelper link() method accepts 2 types of variable as second parameter (the link URL param).
Now I wonder if using array for the param, like
array('controller'=>'users','action'=>'login')
is slower than using string, like '/users/login'. Because Helper won't have to parse the array, just display the link.
If it is so, then what is the purpose of link() method? For now, I am using HtmlHelper::url() method with a regular <a> to display all of my links, to keep my template clean!
Please correct me :)
One reason is reverse routing: For examples, if you route '/blah' to array('controller'=>'articles','action'=>'index'). When you create the link with array('controller'=>'articles','action'=>'index'), cake can automatically output '/blah'. It may not sound very interesting; but if later on you change the route to '/foo', then the link() method can automatically change the output to '/foo'.
Another reason is: using array you can build the url in a programmatic fashion. It's not just controller and action, you also have prefix, named parameters, your own custom parameters if you create in routes, etc.
For now, I am using HtmlHelper::url() method with a regular <a> to display all of my links, to keep my template clean! Well, you are making it harder on yourself then :)
This provides a consistent and flexible method for creating hyperlinks, referencing controllers / actions and specifying dynamic options via associative arrays. Performance shouldn't be an issue unless you are iteratively creating links. Even then, array management and implosion is usually much more efficient than string concatenation.

How do I do table sorting using CodeIgniter?

I've been developing a site over the past few weeks using CodeIgniter as the framework. I've been thinking of the best way to accomplish something, which in a lot of other frameworks in other languages is relatively simple: sortable tables. CodeIgniter switches off query strings by default, because your URLs contain method parameters. So a URL might look like:
/controller/method/param1/param2
You might think that you could just add in sortBy and sortOrder as two additional parameters to the controller method. I don't particularly want to do that, mainly because I want to have a re-usable controller. When you use query string parameters, PHP can easily tell you whether there is a parameter called sortBy. However, when you're using URL based parameters, it will vary with each controller.
I was wondering what my options were. As far as I can see they are something like:
Pass in my sortBy and sortOrder parameters, just suck it up, and develop some less-than-reusable component for it.
Have an additional controller, which will store the sortBy and sortOrder in the session (although it would have to know where you came from, and send you back to the original page).
Have some kind of AJAX function, which would call the controller above; then reload the page.
Hack CodeIgniter to turn query strings back on. Actually, if this is the only option, any links to how to do this would be appreciated.
I just can't quite believe such a simple task would present such a problem! Am I missing something? Does anyone have any recommendations?
While I love jQuery, and I'm already using it on the site, so TableSorter is a good option. However, I would like to do server-side sorting as there are some pages with potentially large numbers of results, including pagination.
If you're OK with sorting on the client side, the Tablesorter plugin for jQuery is pretty nice.
I ran into this with a fairly complex table. The hard part was that the table could grow/shrink depending on certain variables!! Big pain :(
Here's how I handled it..
Adjusted system/application/config/config.php to allow the comma character in the URI:
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-,';
Adjust my controller with a sorting function:
function sorter() {
//get the sort params
$sort = explode(",",$this->uri->segment(3)); //the 3rd segment is the column/order
//pass the params to the model
$data = $this->model_name->get_the_data($sort[0],$sort[1]);
$this->_show($data);
}
function _show($data) {
//all the code for displaying your table
}
I've oversimplified, but you get the idea. The purpose is to have a url like this:
/controller/sorter/columnname,sortorder
The sorter function calls another internal function to deal with the display/template/view logic - it's job is to deal with the sorting call and get the appropriate data from the model.
Of course, this could be reduced to just your current function:
function showGrid() {
$sort = $this->uri->segment(3);
if ($sort) {
//get the data sorted
} else {
//get the data the default way
}
//rest of your view logic
}
That way, you don't even need a separate function - and can use the third segment to define your sorting.
I recently added this Table sorter (which uses Prototype) to a bunch of my pages. It's fast and pretty easy to implement.

Categories