CodeIgniter is a popular, open source PHP framework loosely based on the MVC development pattern. It is used to build dynamic web sites. Out of box, phpGrid is a ready-to-use PHP datagrid solution. Integrating phpGrid with CodeIgniter couldn’t be easier. Here’s how.
Download CodeIgniter here. To install CodeIgniter, follow these four steps:
- Unzip the package.
- Upload the CodeIgniter folders and files to your server. Normally the index.php file will be at the root.
- Open the “application/config/config.php” file with a text editor and set your base URL. If you intend to use encryption or sessions, set your encryption key.
- If you intend to use a database, open the “application/config/database.php” file with a text editor and set your database settings.
Download phpGrid here. To install phpGrid, follow these steps:
- Unzip the phpGrid download file.
- Upload the unzipped phpGrid folder to the “application/libraries” folder.
- Complete the installation by configuring the conf.php file. For instructions on how to do this, see setup phpGrid configuration.
For the purpose of this tutorial, we will directly modify the default controller file “Welcome.php”. In practice, the changes can be made in any new or existing controller file. Notice that we did not initialize the class using the standard:
Where “someclass is” the file name, without the “.php” file extension. Instead we simply include the phpGrid configuration file. The “APPPATH” constant is the path to your application folder. Unlike with the Laravel framework, with CodeIgniter you can directly include PHP classes and working with them. phpGrid is a complex component and composed of multiple classes.
public function index()
require_once(APPPATH. 'libraries/phpGrid_Lite/conf.php'); // APPPATH is path to application folder
$data['phpgrid'] = new C_DataGrid("SELECT * FROM Orders", "orderNumber", "Orders"); //$this->ci_phpgrid->example_method(3);
Note the last line is our new view we are about to create. The view is called “show_grid”.
Creating a view is very simple. All you have to do is to put <?php $phpgrid->display(); ?> somewhere in the view. Learn more about creating a basic PHP grid here.
defined('BASEPATH') OR exit('No direct script access allowed');
<h1>Welcome to CodeIgniter! Show me the grid!</h1>
<?php $phpgrid->display(); ?>
That’s it! phpGrid handles all the CRUD operations. We don’t need to create a Model for our PHP datagrid to run in CodeIgniter.
CodeIgniter and PHP Session
We recommended that you stick to a native PHP session in CI. By default, CI stores session information in a cookie, which is neither secure nor efficient.
You can also use the “out of proc” session by storing the session info in a database. This method is more sophisticated but inevitably more complex. It is not recommended unless you need to develop a large eCommerce website needs a session persistent shopping cart. phpGrid also uses a native PHP session to store secure data such as database connections and SQL data.
If you are using a native PHP session, make sure set “save_path” value to php.ini in an existing folder with write permissions.