Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

php - Integrate phpgrid with codeigniter

Im trying to integrate phpgrid with my codeigniter program.

I need a few clarifications on how to use the library function.

I have added the phpgrid files to the application/libraries path and I have loaded the libray using $this->load->library('phpGrid');

Below is the code in the phpgrid conf.php file.

<?php
// mysql example
define('DB_HOSTNAME','localhost'); // database host name
define('DB_USERNAME', 'admin');     // database user name
define('DB_PASSWORD', 'pop3'); // database password
define('DB_NAME', xtra); // database name     
define('DB_TYPE', 'mysql');  // database type
define('DB_CHARSET','utf8'); // ex: utf8(for mysql),AL32UTF8 (for oracle), leave blank to use the default charset


define('SERVER_ROOT', '/grid');

/******** DO NOT MODIFY ***********/
require_once('phpGrid.php');     
/**********************************/
?>

Can someone please help me how can I reference the define and require_once files to my library path?

Or is there any other way that I can include the phpgrid files to my CI project?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've just looked at the PHPgrid source files and it appears that they are encrypted so you are limited as to how much you can fully "integrate" with Codeigniters MVC framework.

To work with an external library like this, here's what I would generally do:

Store a separate config.php file (that looks like the PHPgrid one) that defines the db connection constants in the root directory.

Then require this in your Codeigniter config/database.php file and use the constants to set the Codeigniter settings as well. So your Codeigniter database.php would look like:

require_once('config.php');

$db['default']['hostname'] = DB_HOSTNAME;

$db['default']['username'] = DB_USERNAME;

$db['default']['password'] = DB_PASSWORD;

$db['default']['database'] = DB_NAME;

You don't want to be storing database connection details all over the place.

Then include config.php at the top of your phpgrid/conf.php file, and use the constants to fill the details the same way, obviously fill out the other phpgrid constants as well.

Put all the PHPgrid files in a subdir of application/libraries. Now create a new file in your application/libraries file that is called something like ci_phpgrid.php and in it create a new class like below:

<?php
require_once('phpgrid/conf.php');

class CI_phpgrid {

    public function example_method($val = '')
    {
        $dg = new C_DataGrid("SELECT * FROM Orders", $val, "Orders");
        return $dg;
    }
}

You can now use this to communicate with php grid, leaving the original files intact.

In you controller you would just do something like:

$this->load->library('ci_phpgrid');
$data['phpgrid'] = $this->ci_phpgrid->example_method(3)

$this->load->view('home_page',$data);

And then in the view you can display the table using:

$phpgrid->display()

As I mentioned, I've not used PHPgrid and you'll need to include all the relevant JS for sorting etc but this is generally the way you want to approach external libraries in CI.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...