I'm new in CodeIgniter and read user guide from website. Using pagination class, pagination can be made like :
<?php
class Test extends CI_Controller {
function page(){
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/CodeIgniter/index.php/test/page/';
$config['total_rows'] = 20;
$config['per_page'] = 1;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
}
}
?>
But how can I make this pagination with alphabetical order? Thanks...
Codeigniter pagination class is only for creating the navigation links. You have to write your script for populating these pages with data. So in your model query, you have to specify the order_by clause.
You can build new one as per your requirement. Already this topic is discussed in CI forum.
Please check the forum link:
http://ellislab.com/forums/viewthread/164849/
Related
CodeIgniter's pagination library is not loading in a particular controller of my project, the same piece of code works fine in other controllers of the same project.
Here is the code that loads correctly: (controller name : Purchases)
// load pagination library
$this->load->library('pagination');
//configure pagination
$page_config['base_url'] = base_url('purchases/list_all');
$page_config['total_rows'] = $total_no_of_purs;
$page_config['per_page'] = $no_of_purs_per_page;
$page_config['uri_segment'] = 3;
$page_config['use_page_numbers'] = TRUE;
//initialize pagination
$this->pagination->initialize($page_config);
and Here follows the code that fails to load pagination library (Controller Name: Employee)
//load pagination library
$this->load->library('pagination');
//configure pagination
$page_config['base_url'] = base_url('employee/list_all');
$page_config['total_rows'] = $total_no_of_emps;
$page_config['per_page'] = $no_of_emps_per_page;
$page_config['uri_segment'] = 3;
$page_config['use_page_numbers'] = TRUE;
//initialize pagination
$this->pagination->initialize($page_config);
I want to say that all the variables assigned to the config are valid. The error I am getting for the second piece of code is:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Employee::$pagination
Filename: controllers/employee.php
Line Number: 115
Line 115 is: $this->load->library('pagination');
In both the classes, the constructor was not loaded with the pagination library. Why does one work and the other not?
$this->pagination->create_links(); only shows pagination when your $total_no_of_emps is greater than $no_of_emps_per_page.
After seeing your coding related to pagination, I want to see your whole controller of employee as seems that extends in employee controller is missing and hence they are not able to load pagination class and hence please paste your complete employee controller here.
Thanks,
Shankar
this error because of
$page_config['base_url'] = base_url('employee/list_all');
try this $page_config['base_url'] = base_url()."employee/list_all"; If not
** you need to define base_url(); first
I want to change codeigniter pagination query string. Currently this is working like below example
http://example.com/1
I want to change this with my example like
http://example.com?page=1
Anyone can let me know how I can do this without changes in existing library? or Should I have to create my own pagination for this system?
Thanks
You have to set config for pagination
$config['page_query_string'] to TRUE
you also can configure your querystring
$config['query_string_segment'] = 'your_string';
Actually with CodeIgniter 3.0.0 there is a better solution now;
You should enable the reusage of the query string buy enabling this configuration:
$config['reuse_query_string'] = true;
only after that you should initialize the pagination:
$this->pagination->initialize($config);
You can do it by using 'Enabling Query Strings' in /application/config/config.php
$config['enable_query_strings'] = TRUE;
But, it will also add controller and method names as query stings not as clean urls like:
index.php?c=controller&m=method
Reference
I am new to Codeigniter, and I have trouble with the pagination class.
From CI document: (I do studied it carefully and practiced it.)
$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);
echo $this->pagination->create_links();
It works quite well, when I do practice at local host.
How can I pass more parameters for the base_url value?
Assumed I must pass mypara to page function as below.
$config['base_url'] = 'http://example.com/index.php/test/page/mypara/';
It doesn't work then. How can I joined my parameter (mypara) in the base_url?
It seems it doesn't support passing parameters to base_url.
Here's the answer on codeigniter forum. http://codeigniter.com/forums/viewthread/143280/P15T here are many questions that have been answered in there.
Here is an excellent tutorial from Nettuts on Pagination with COdeigniter
http://net.tutsplus.com/articles/news/codeigniter-from-scratch-day-7-pagination/
You might also change this
$config['uri_segment'] = '2';
to 3 or 4
I am developing an ecommerce website with CI that has product categories and products. I want to route the URL so that it will go to the products controller, then run the getCategoryByName function for the first segment, then run the getProductByName for the second segment. Here is what I have:
URL:
products/docupen/rc805
routes.php:
$route['products/([a-z]+)'] = "products/getCategoryByName/$1";
$route['products/([a-z]+)/([a-z0-9]+)'] = "products/$1/getProductByName/$2";
But its not working. "docupen" is the category, and "rc805" is the product.
Thanks in advance.
Thank you all for your help. This is what I ended up with for what I needed.
$route['products/:any/:num'] = "products/getProductByID";
$route['products/:any/:any'] = "products/getProductByName";
$route['products/:any'] = "products/getCategoryByName";
My answer builds a bit on Colin's answer.
When I played around with the routes in CodeIgniter I came to the conclusion that the order of the routes was important. When it finds the first valid route it won't do the other routes in the list. If it doesn't find any valid routes then it will handle the default route.
My routes that I played around with for my particular project worked as follows:
$route['default_controller'] = "main";
$route['main/:any'] = "main";
$route['events/:any'] = "main/events";
$route['search/:any'] = "main/search";
$route['events'] = "main/events";
$route['search'] = "main/search";
$route[':any'] = "main";
If I entered "http://localhost/index.php/search/1234/4321" It would be routed to main/search and I can then use $this->uri->segment(2); to retrieve the 1234.
In your scenario I would try (order is very important):
$route['products/:any/:any'] = "products/getProductByName";
$route['products/:any'] = "products/getCategoryByName";
I don't know enough to route the way you wanted (products/$1/getProductByName/$2), but I'm not sure how you would create a controller to handle this particular form of URI. Using the $this->uri->segment(n); statements as mentioned by Colin in your controller, you should be able to do what you want.
You should use the URI class to retrieve the "docupen" and "rc805" segments from your url. You can then use those values in whatever functions you need.
For example, if your url is www.yoursite.com/products/docupen/rc805, you would use the following in your products controller to retrieve the segments and assign them to variables:
$category = $this->uri->segment(2); //docupen
$product = $this->uri->segment(3); //rc805
Then you can use $category and $product however you need to.
CodeIgniter routes don't work well with regex. They are supported, not I can never get them to work. It would be much easier to catch them like this
$route['products/(:any)'] = "products/getCategoryByName/$1";
$route['products/(:any)/(:any)'] = "products/$1/getProductByName/$2";
How to use the mvc concept,
I need to add a php file to mvc concept.
Please explain
Before you start adding files, you need to understand what MVC is, i assume that you understand what a controller, model and view is, ok i will try to explain step-by-step on how to add a file. Let's say you want to create a page that grabs some product info from database and shows that on a page called products.php.
Step 1: You create a controller named products.php and put all the variables that will be passed to view as well as function to grab products info from db through model.
Step 2: You create a model named products.php and write a function it it that will grab the product info from db.
Step 3: You create a view named products.php and show all variables coming from controller as well as any html for the layout.
Here is the basic skeletion:
products.php controller
class products_controller extends controller
{
// set a variable to be shown on the view
$this->view->myvariable = 'Our Products';
// call model function to get info from db that will be shown on the view.
$this->load->model('products');
$this->view->db_products = $this->products->getProducts();
// now render the view
$this->view->render();
}
products.php model
class products_model extends model
{
function getProducts()
{
$result = mysql_query("select * from products_table");
$rows = mysql_fetch_assoc($result);
return $rows;
}
}
products.php view
<html>
........
<?php echo $myvariable; // this var comes from controller?>
<?php
// now show products coming from db
foreach ($db_products as $product)
{
echo $product['name'];
echo $product['price'];
echo $product['etc'];
}
?>
........
</html>
Note: This is just an example but depending on which MVC framework you are using, file names and class names or syntax might look different, so you will have to adjust that. However, i have put in the code from my own MVC framework named EZPHP, and as the name suggests, it is very easy to use MVC framework. If you need it just reply through a comment.
Thanks and hope that helps :)