I am currently displaying a list of products on pyrocms page. Using plugin i get the products from db tables and display them as a list i want to use pagination but have no idea how to use it and where to start from. Does any one know any tutorial or have some suggstion?
You won't need to load pagination library or initialize it. It is a little different from you do in regular codeigniter, also you can surely use the way you do it in codeigniter pagination class
I usually do this for pagination.
in your controller use this
....
// Create pagination links
$total_rows = $this->products_m->count_all_products();
$pagination = create_pagination('products/list', $total_rows);
//notice that product/list is your controller/method you want to see pagination there
// Using this data, get the relevant results
$params = array(
'limit' => $pagination['limit']
);
$this_page_products = $this->product_m->get_all_products($params);
....
//you got to pass $pagination to the view
$this->template
->set('pagination', $pagination)
->set('products', $this_page_products)
->build('products/view');
obviously, you will have a model named products_m
At your model this would be your get_all_products function, I am sure you can build the count_all_products() function too
private function get_all_products($params){
//your query to get products here
// use $this->db->limit();
// Limit the results based on 1 number or 2 (2nd is offset)
if (isset($params['limit']) && is_array($params['limit']))
$this->db->limit($params['limit'][0], $params['limit'][1]);
elseif (isset($params['limit']))
$this->db->limit($params['limit']);
//rest of your query and return
}
Now at your view you have to foreach in your passed $products variable and to show pagination links use this line in your view
<?php echo $pagination['links'];?>
I use this in my works, hope it help.
Pyrocms used codeigniter framework this code works perfectly in case of module but i have never tried this on a plugin but still you can try this.
Load pagination library in Controller
$this->load->library('pagination');
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
Use this code in view to genrate the links
echo $this->pagination->create_links();
Related
I have the following routes for the news that works like a charm:
$word5 = urlencode('mygreek-keyword-here');
$route[$word5] = "news/displayAllNews";
and this page display all news with the desired keyword in the address bar. But...there is always a but...I have paging on this page, so when i click on next page link...page works but with old non-friendly seo url, that is:
news/displayAllNews/10/10
while I would like to have is: mygreek-keyword-here/10/10
code that i have in my controller for the paging is as follow:
$config['base_url'] = site_url('/news/displayAllNews/'.$limit.'/');
what should i do in routes.php and in my controller to get the desired result?
Regards, John
You could get the string of URI by $this->uri->uri_string() method, and set the pagination config as follows:
$config['base_url'] = site_url($this->uri->uri_string() .'/'. $limit.'/');
Note that the URI library is loaded automatically, it doesn't need to load it manually.
Update:
Change the route rule to:
$route["$word5(.*)"] = "news/displayAllNews$1";
If it doesn't work yet, remove the .'/'. $limit.'/' phrase and let the pagination to add the limit itself by:
$config['per_page'] = 10; // or $limit, default items per page.
$config['use_page_numbers'] = FALSE;
I recently jumped on the CodeIgniter train because I finally want to embrace the MVC architecture for my growing PHP/MySQL projects.
Right now I encounter a probolemin the PAgination configuratino that is apparently not described earlier as far as I can ssee.
Probably the issue is not PAgination only related but more general. In my first attempt, I just filled the confiuration in the controller method and passed it to the initialize() method:
$config['base_url'] = $this->config->base_url() . "/categorie/{$catName}/";
$config['total_rows'] = $this->event_model->get_nrofevents_for_categorie($categorieObject->id, TRUE);
$config['per_page'] = 12;
$config['full_tag_open'] = '<div class="paging">';
$config['full_Tag_close'] = '</div>';
$config['prev_link'] = 'Vorige';
$config['next_link'] = 'Volgende';
$this->pagination->initialize($config);
And this works fine. But I have a buch of other pages that use the same pagination with most of its parameteres identical, only the base_url and the total_rows config properties are different for each page.
I have put the common configuration in the config/pagination.php configuration file but see no option to add the page-dependent properties in the code.
More general, is it possible to put generic configuration in a config file and add some customized properties in the controller method?
For me this seems logical but cannot figure out how...
I tried something like:
$this->load->config('pagination');
$this->config->set_item('next_link', 'blablabla');
but it seems that the Pagination is initialized immediately after reading the config file and this code has no effect.
Please any suggestions?
Since the initialize() only replaces the keys you provide, you can have your config/pagination.php hold the default values and call initialize() with the changing ones.
config/pagination.php
// put default values here
$config['next_link'] = 'Volgende';
$config['prev_link'] = 'Vorige';
// ...
controller
$this->pagination->initialize(array(
'base_url' => base_url().'categorie/'.$catName.'/',
'total_rows' => $totalRows,
// ...
)));
don't use base_url()... I always use site_url();
$config['base_url'] = site_url('');
Usually, I just set a $feedback var or array and then check for that to display in my views.
However, it occurred to me I should perhaps use flashdata instead.
The problem is sometimes - for say an edit record form, I may simply want to reload the form and display feedback - not redirect. when i use flashdata, it shows but then it shows on the next request as well.
What would be the best practice to use here?
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.
u use hidden field for that
I would use the validation errors from the Form validation class and load those directly to the view in its 2nd argument.
$this->form_validation->set_error_delimiters('<p>', '</p>');
$content_data = array();
if (!$this->form_validation->run()) {
$content_data['errors'] = validation_errors();
}
$this->load->view('output_page', $content_data);
Then check in your view whether $errors isset.
Controller:
$data['message'] = 'some message you want to see on the form';
$this->load->view('yourView', $data);
View:
if (isset ($message)) : echo $message; endif;
...
Ive been working with CI and I saw on the website of CI you can load a view as a variable part of the data you send to the "main" view, so, according the site (that says a lot of things, and many are not like they say ...ej pagination and others) i did something like this
$data['menu'] = $this->load->view('menu');
$this->load->view ('home',data);
the result of this is that I get an echo of the menu in the top of the site (before starts my body and all) and where should be its nothing, like if were printed before everything... I have no idea honestly of this problem, did anybody had the same problem before?
Two ways of doing this:
Load it in advance (like you're doing) and pass to the other view
<?php
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);
Load a view "from within" a view:
<?php
// put this in the controller
$this->load->view('home');
// put this in /application/views/home.php
$this->view('menu');
echo 'Other home content';
Create a helper function
function loadView($view,$data = null){
$CI = get_instance();
return $CI->load->view($view,$data);
}
Load the helper in the controller, then use the function in your view to load another one.
<?php
...
echo loadView('secondView',$data); // $data array
...
?>
I am trying to display information from mysql database based on user's query . I have a form and after a user submits data, it goes to the controller I have posted below and then to a view file where it shows the information the user wanted.
So far I am good with this, but the problem creates with the pagination. At present I have eight rows of data to display from my database, so to check if the pagination is working well I set the $config['per_page'] value to 5 (please check below). And now when I click on the next button, it doesn't show any data.
I think the problem lies in the fact that when I click on the "next" button, it goes to the controller again but this time the controller is not getting the values the user posted in the first place.
Would you please kindly help me with this?
Thank in Advance :)
My Controller:
function show(){
$batchid=$this->input->post('batchid');
$daterange1=$this->input->post('daterange1');
$daterange2=$this->input->post('daterange2');
$this->load->library('pagination');
$config['base_url'] = base_url().'markslist/show';
$this->db->select('*');
$this->db->from('marks');
$this->db->where('examdate', $daterange1);
$this->db->where('examdate', $daterange2);
$this->db->join('student', 'marks.studentid = student.studentid');
$this->db->where('batchid', $batchid);
$this->db->order_by('batchid','DESC');
$query = $this->db->get('');
$numrows=$query->num_rows();
$config['total_rows'] = $numrows;
$config['per_page'] = 5;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination" align="center">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
// To get the subject lists
$this->load->model('mod_markslist');
$data['records']= $this->mod_markslist->listmarks($batchid,$daterange1,$daterange2,$config['per_page'],$this->uri->segment(3));
$data['main_content']='view_markslist';
$this->load->view('includes/template',$data);
}
I would copy the initial posted data to session:
if($this->input->post()){
$this->session->set_userdata('batchid', $this->input->post('batchid'));
$batchid = $this->input->post('batchid');
/* similar for the other posted fields */
}
else{
$batchid = $this->session->userdata('batchid');
/* similar for other fields now in session */
}
/* load pagination library etc */
Best to also do a check if the information is available in session as well though.