Click here to Skip to main content
15,892,298 members
Articles / All Topics

phpGrid, Twitter Bootstrap Integration

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
1 Jul 2015CPOL3 min read 11.9K  
  This is a step-by-step tutorial of how to integrate SmartAdmin, or any other Bootstrap them, with phpGrid. SmartAdmin is a popular Bootstrap admin theme. It has modern flat design and responsive layout.
Run Demo

 

This is a step-by-step tutorial of how to integrate SmartAdmin, or any other Bootstrap them, with phpGrid.

SmartAdmin is a popular Bootstrap admin theme. It has modern flat design and responsive layout. SmartAdmin comes with 5 different versions: Pure HTML, AJAX Version, PHP version, Modular AngularJS, and ASP.NET MVC5 versions. For this example, we use PHP version, and specifically, the PHP Ajax version.

 

What you will need

  1. Acquire a copy of phpGrid. You can use either the free, phpGrid Lite or get phpGrid full version here.
  2. Get Bootstrap SmartAdmin. Many Bootstrap themes are also available for free on the Internet.

Now that you’ve downloaded both, let’s get started.

 

1. Folder Structure

As mentioned earlier, we will use SmartAdmin’s PHP version. InsidePHP_version folder, you will find two subfolders, PHP_Ajax_version andPHP_HTML_version. Here, only PHP_Ajax_version folder is copied to web root directory and renamed as smartAdmin_AJAX (see below).

Secondly, save our phpGrid folder in smartAdmin_AJAX as shown. However, you can save it to any folder.

 

phpgrid bootstrap folder structure
phpgrid bootstrap folder structure

 

2. Update phpGrid conf.php

Before you begin coding using phpGrid, you must specify database information in conf.php, phpGrid configuration file in which we specify the following values:

  1. Database connection information,
  2. SEVER_ROOT,
  3. THEME

It’s important that we set THEME value as “NONE”. The reason is that we don’t need to provide a jQuery UI theme as Bootstrap already includes a theme. Using two jQuery themes will likely result in CSS conflicts and style inconsistency.

To learn more about phpGrid configuration, read the installation guide:http://phpgrid.com/documentation/installation/

For the purposes of this demo, we will use a MySQL database. In the figure below, you can see an example of the necessary database definitions as they appear in conf.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
define('PHPGRID_DB_HOSTNAME', 'localhost:3306');
define('PHPGRID_DB_USERNAME', 'root');    
define('PHPGRID_DB_PASSWORD', '');
define('PHPGRID_DB_NAME', 'sampledb');
define('PHPGRID_DB_TYPE', 'mysql');  
define('PHPGRID_DB_CHARSET','utf8');
define('SERVER_ROOT', '/smartAdmin_AJAX/phpGrid');
define('THEME', 'NONE');
// *** MUST SET TO FALSE WHEN DEPLOYED IN PRODUCTION ***
define('DEBUG', false);
/******** DO NOT MODIFY ***********/
require_once('phpGrid.php');    
/**********************************/
?>

3. Edit Bootstrap lib/config.php

phpGrid requires PHP SESSION. To ensure that the PHP session starts properly, open the file config.php on the SmartAdmin_AJAX/lib/ directory and copy and paste the following text at the very top of the file.

1
if (session_id() == ''){ session_start();}

phpgrid bootstrap session
phpgrid bootstrap session

 

The setup process is now complete.


4. Insert PHP Grid

In this demo, we will add a reference call to phpGrid directly in SmartAdminajax/dashboard.php. . This call can be in any file in SmartAdmin ajax folder.

1
2
3
4
5
6
7
8
9
10
<?php
 require_once(../phpGrid/conf.php”);
 $dg = new C_DataGrid(“SELECT * FROM orders”, “orderNumber”, “orders”);
 $dg->enable_search(true);
 $dg->enable_export(‘EXCEL’);
 $dg->enable_edit(‘INLINE’);
 $dg->set_col_hidden(‘comments’);
 $dg->set_col_edittype(“status”, “select”, “Shipped:Shipped;Canceled:Cancelled;Open:Open;Pending:Pending”); $dg->enable_autowidth(true);
 $dg->display();
 ?>

Note the first line that references phpGrid/conf.php.

“orders” is a database table name from our MySQL sample database. You can find the sample MySQL sample database inside phpGrid/examples/SampleDBfolder.

Now visit the following URL to play around with your newly created PHP grid by using functions such as CRUD, search, and export! You can find a complete list of phpGrid demo here.

1
http://<YOUR WEB SERVER>/smartAdmin_AJAX/


5. Add Custom CSS (Optional)

At this point, the installation is complete. However, I refined jqGrid CSS so the final result would be more aesthetically pleasing. Here’s the final version of the CSS. Feel free to use this code on your page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<style>
 /* phpGrid to re-expand 100%. Needed when page is loaded in Ajax and there’s sidebar */
 div.ui-jqgrid.ui-widget.ui-widget-content.ui-corner-all{
 width: 100% !important;
 overflow: auto;
 }
/* prevent Bootstrap CSS conflict by reseting phpGrid header CSS */
 .ui-jqgrid .ui-jqgrid-htable th div {
 position: inherit;
 height: initial;
 }
.ui-jqgrid .ui-jqgrid-view input, .ui-jqgrid .ui-jqgrid-view select, .ui-jqgrid .ui-jqgrid-view textarea, .ui-jqgrid .ui-jqgrid-view button {
 width: 100%;
 height: 100%;
 padding:0px;
 }
/* FORM edit */
 .ui-jqdialog-content table.EditTable input{
 width: 90%;
 }
 .ui-jqdialog-content table.EditTable select{
 width: 95%;
 }
 .ui-jqdialog-content table.EditTable textarea{
 width:90%;
 }
 </style>

That’s it. You now have a fully functional and great-looking, responsive PHP grid in your Bootstrap. And it also works great on mobile devices!

 

phpGrid Bootstrap is mobile friendly.
phpGrid Bootstrap is mobile friendly.

 

The post phpGrid, Twitter Bootstrap Integration appeared first on phpGrid.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
He likes programming and iPod.

Comments and Discussions

 
-- There are no messages in this forum --