I´m working on caching in Codeigniter and I find out one problem which I don´t know how to solve:
I have method view_produdcts() where I get products according the url and render the view. I turned on the cache there but if I want to render product´s with some parameters for the first time, it´s ok, but if I try to render another products with other parameters, it still returns me the products which I loaded for the first time.
Do you know, how to solve it?
Thank you very much for your ideas.
Here is my method:
/**
* view products by parameters
*/
public function view_products()
{
$limit = 12; // limit of products per page
$attrs = parse_attributes_from_url($_GET); // load attributes from url
$config['per_page'] = $limit; // set some config files for paging
$config['base_url'] = prepare_url_from_attributes_for_paging(generate_url_from_attributes($attrs));
$config['cur_page'] = $attrs['page'];
$attrs['limit'] = $limit;
$data['products'] = $this->products_model->get_products_specified($attrs); products
$data['popular_categories'] = $this->categories_model->get_popular_categories();
$data['all_categories'] = $this->categories_model->get_categories_list_eshop();
if(isset($attrs['cat']) && $attrs['cat'] != null){
$data['category_title'] = $this->categories_model->get_category_title($attrs['cat']);
}
else {
$data['category_title'] = $this->categories_model->get_category_title(3);
}
main_view_front('products/category', $data);
}
Related
I am working on Code igniter (new at codeigniter) and I want to do pagination on $.get.
Contoller Code is here:
public function get_todo($id=null)
{
$this->_required_login();
if($id!=null)
{
$result=$this->todo_model->get([
'todo_id'=>$id,
'user_id'=>$this->session->userdata('user_id')
]);
}
else
{
$config = array();
$config["base_url"] = base_url() . "dashboard/load_todo"; // I want here is $.get instead of a link
//I have js files in which $.get is.
$total= $this->todo_model->get_rows($this->session->userdata('user_id')); //Total rows
$config["total_rows"] = $total;
$config["per_page"] = 3; // Per Page required
// I have no idea what it is.
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
// no idea thing ends here
//getting results and working fine
$data["results"] = $this->todo_model->
fetch_data($config["per_page"], $page);
// Real pain in the neck
$data["links"] = $this->pagination->create_links();
}
$this->output->set_output(json_encode($data));
}
Now about js file which is creating html response
var load_todo = function() {
$.get('api/get_todo',function(o){
//api/get_todo is controller
var output='';
for (var i=0;i<o.results.length;i++)
{
output+=Template.todo(o.results[i]); // Pagination result
}
output+='<p>'+o.links+'</p>'; // Pagination links
//console.log(output);
$("#list_todo").html(output);
},'json');
};
What I want it to have o.links to have $.get.
you forgot to return your output. In your controller method get_todo() edit the last line to this:
return $this->output
->set_content_type('application/json')
->set_output(json_encode($data));
After 3 days of struggle and headache I finally got my answer own my own. For those who might come with same thinking about pagination as I did, if you want to do pagination for ajax call get or post, best way will be to change ajax_pagination library.
ajax_pagination -> create_links() -> getAJAXlink()
and edit its code as u want to.
we have an application which has page's result on the bases of certain parameters like domain.com/index.php/welcome/student/male here welcome is controller student is method name male is parameter, now we want pagination for this, but the problem is a person can also use sub category like this domain.com/index.php/welcome/student/male/steven where steven is another parameter, how to create pagination for this type of urls. Thank you in advance :)
Some problems when having optional parameters along with pagination:
The placement of pagination offset changes in the URL based on the
number of provided parameters.
If optional parameters are not set,
the pagination offset will act as a parameter.
Method 1:
Use query strings for pagination:
function student($gender, $category = NULL) {
$this->load->library('pagination');
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'offset';
$config['base_url'] = base_url().'test/student/'.$gender;
// add the category if it's set
if (!is_null($category))
$config['base_url'] = $config['base_url'].'/'.$category;
// make segment based URL ready to add query strings
// pagination library does not care if a ? is available
$config['base_url'] = $config['base_url'].'/?';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
// requested page:
$offset = $this->input->get('offset');
//...
}
Method 2:
Assuming the category would never be a number and if the last segment is a numeric value then it's the pagination offset not a function's parameter:
function student($gender, $category = NULL) {
// if the 4th segment is a number assume it as pagination rather than a category
if (is_numeric($this->uri->segment(4)))
$category = NULL;
$this->load->library('pagination');
$config['base_url'] = base_url().'test/student/'.$gender;
$config['uri_segment'] = 4;
// add the category if it's set
if (!is_null($category)) {
$config['uri_segment'] = $config['uri_segment'] + 1;
$config['base_url'] = $config['base_url'].'/'.$category;
}
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
// requested page:
$offset = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 1;
//...
}
I prefer the first method because it does not interfere with function parameters and it would be easier to implement the pagination support at an abstract level.
I worked around with codeigniter but i think that maybe you have to use config/routes.php. For example with:
$route['^(bar1|bar2)/routes/(:any)/(:any)'] = "routes/get_bar/$2/$3";
you could write a function in controller (in this case routes) like this:
public function get_bar($bar1 = null, $bar2 = null)
I hope this help you
Regards!
I have a blog page of posts that I am trying to paginate using CodeIgniter. The numbering and limiting seem to be working fine, except I keep getting a 404 when I try to travel to another page.
The strange thing is the normal culprits that cause this issue are correct. The baseUrl and the uri_segment.
My controller looks like this:
$config = array();
$config["base_url"] = $this->config->site_url("/blog");
$config["total_rows"] = $this->blog_model->count();
$config["per_page"] = 2;
$config["uri_segment"] = 2;
$config["num_links"] = round($config["total_rows"] / $config["per_page"]);
$config['use_page_numbers'] = TRUE;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$this->load->view('blog', array(
'user' => $this->user,
'blog' => $this->blog_model->loadPosts($config['per_page'], $page),
'links' => $this->pagination->create_links(),
'footer' => $this->blog_model->loadFooter()
));
And then in my model I am grabbing the posts
public function loadPosts($limit, $start)
{
$this->db->limit($limit, $start);
$this->db->order_by("date", "desc");
//this loads the contact info
$query = $this->db->get('entries');
return $query->result();
}
My full URL is www.mysite.com/blog and then with the pagination it appears as www.mysite.com/blog/2.
For the base_Url I have also tried base_url() . "/blog";.
And I have tried setting the uri_segment to 1 and 3, but nothing seems to work.
As well I have tried playing around with the routing and have added just to see if it would do anything:
$route['blog/(:num)'] = 'blog/$1';
You can use this line of code if your code is inside the index method:
$route['blog/:any'] = "blog/index/$1";
Because you have used the segment(2), and you should change the blog/index/$1 to blog/:any.
Assuming the function name that contain your pagination code is index(), you should change the route to:
$route['blog/(:num)'] = 'blog/index/$1';
And in your index() function, add the $page parameter:
public function index($page = 1){
...
With your routes, try if you can to add as many :anys or :nums after as you like:
$route['blog'] = 'blog/index'; // For the first level
$route['blog/(:any)/(:any)'] = 'blog/index/$1/$2'; // For extra "uri" segments.
// base_url pagination
$config["base_url"] = base_url("blog"); // Is preferred
You can keep your other code as it is. Just add this at your routes.php file:
$route['blog/:num'] = "blog/index";
//Assumes your code is inside the index method.
//If not, use this way:
//$route['blog/:num'] = "blog/YOUR_METHOD_NAME";
You can't pass a parameter to the index() function of a controller like it is a random function.
If you try to do controller/var instead of controller/function/var, CodeIgniter will search for a function var() inside the controller which does not exists. That's what happen when you try to access blog/2: 2() isn't a function in your controller.
You can create a new function in your controller, let's say page(), and move your code inside. That way, you will call blog/page/2. Function page() will exists and will not get a 404. Also don't fordet to redefine your base_url for pagination.
$config["base_url"] = site_url("blog/page");
Another solution if you absolutely need the URL like /blog/2, routes:
$route['blog/(:any)'] = 'blog/index/$1';
Remap may also be a solution: http://www.codeigniter.com/user_guide/general/controllers.html#remapping
I have a codeigniter search form which includes a dropdown list (Car) and a checkbox array (Car types). I am using POST method to get values from database but post method conflicts with pagination. Could you please check my code and help me to find the mistake.
Here is my controller
public function search($offset = 0) {
$limit = 3;
$this->load->library('form_validation');
$this->load->model('model_x');
$this->form_validation->set_rules('car', 'Car','required');
$this->form_validation->set_rules('types', 'Car Type','required');
if($this->form_validation->run()) {
$car= $this->input->post('car');
$types = $this->input->post('types');
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/abc/cont/search/';
// 'http://localhost/abc' is my base url
$config['total_rows'] = 14;
$config['per_page'] = 3;
$data['pagination'] = $this->pagination->initialize($config);
if ($this->model_x->did_search($car, $types, $limit, $offset)){
$data["results"] = $this->model_x->did_search($car, $types, $limit, $offset);
$this->load->view("search_ok",$data);
}
}
else
{
$data['message'] = 'Please select your options.';
$this->load->view("search_nok",$data);
}
}
Using get parameters for search would be better since besides making pagination easier, it will allow users to access that link directly through Bookmarks or other hyperlinks without needing to go through posting your form every time they need to repeat a "saved" search.
You can use pagination with post method search data php ci
store post data in session and on second page check session with 2?page_rows=5
get from URL.
This will work
if (session_id() && !empty($this->input->get('page_rows', 0))) {
$pData= $this->session->userdata('custumer_data');
....
then query
}
I'm trying to load a blog type page, I load the entries from my database with this controller
public function blog($page){
$this->load->model("model_get");
$this->load->view("site_header");
$this->load->view("site_nav");
$counter = $this->model_get->getBlogcount();
for($counter; $counter > 0; $counter --){
$data["results"] = $this->model_get->getBlog($counter);
$this->load->view("content_blog", $data);
}
}
$this->load->view("site_footer");
}
and this model
function getBlogcount(){
$result = $this->db->count_all("blog");
return $result;
}
I count the entries in the database where I call them out by their ID. But now I'm trying to create multiple pages that expand automatically everytime I enter a new entry. So lets say I have 27 entries, and want to have no more than 5 entries on a single page, how do I make it so that it creates the necessary 6 pages to show them without loading the other 3 empty entries and stuff.
I'm new to codeigniter and have always worked with ASP .NET, any help would be helpfull.
Thanks in advance!
p.s. english isn't my first language
CodeIgniter have his own pagination Class. Take a look here : http://ellislab.com/codeigniter/user-guide/libraries/pagination.html
You can try this firstly and adapt to your project :
public function blog($page = 0)
{
$this->load->library('pagination');
$this->load->helper('url');
$config['base_url'] = base_url('blog/'. $page);
$config['total_rows'] = $this->model_get->getBlogcount();
$config['per_page'] = 5;
$this->pagination->initialize($config);
$data['results'] = $this->model_get->getBlog($config['per_page'], $page);
$this->load->view("content_blog", $data);
}
Edit your "getBlog" function model to get results with limit clause, like this :
function getBlog($limit, $start)
{
$results = $this->db->limit($limit, $start)->get('your_blog_table');
if ($results)
{
return $results->result();
}
return FALSE;
}
And use in your view this code to create your pagination links :
echo $this->pagination->create_links();