最近想做一個通過PHP實現DataGrid功能的東西,這樣可以直接修改數據庫中表的內容,而不用開發【新增數據頁面】,【編輯頁面】,於是乎在網上找了找,類似的東西也有幾個,開源的、付費的都有,不過基本都是基於MySQL。由於需要連接Oracle所以從二次開發和頁面樣式來說個人覺得 phpMyDataGrid還是比較好上手。本篇首先介紹基於MySQL的使用方法,再簡單介紹對於Oracle連接(基於sqlrelay)的二次開發。
1. 創建測試數據庫和表
- create database `guru`;
- USE `guru`;
- CREATE TABLE `employees` (
- `id` int(6) NOT NULL auto_increment,
- `name` char(20) default NULL,
- `lastname` char(20) default NULL,
- `salary` float default NULL,
- `age` int(2) default NULL,
- `afiliation` date default NULL,
- `status` int(1) default NULL,
- `active` tinyint(1) default NULL,
- `workeddays` int(2) default NULL,
- `photo` char(30) default NULL,
- PRIMARY KEY (`id`)
- )
- insert into `employees`
- (`id`,`name`,`lastname`,`salary`,`age`,`afiliation`,`status`,`active`,`workeddays`,`photo`)
- values (1, 'Ana', 'Trujillo',2000,45, '2005-05-13',1,1,10, '1.jpg');
- insert into `employees`
- (`id`,`name`,`lastname`,`salary`,`age`,`afiliation`,`status`,`active`,`workeddays`,`photo`)
- values (2, 'Jennifer', 'Aniston',3500,23, '2004-10-22',1,0,0, '2.jpg');
- insert into `employees`
- (`id`,`name`,`lastname`,`salary`,`age`,`afiliation`,`status`,`active`,`workeddays`,`photo`)
- values (3, 'Michael', 'Norman',1200,19, '2007-01-10',1,1,5, '3.jpg');
- insert into `employees`
- (`id`,`name`,`lastname`,`salary`,`age`,`afiliation`,`status`,`active`,`workeddays`,`photo`)
- values (4, 'Vanessa', 'Black',6500,31, '2000-11-05',1,1,30, '4.jpg');
- insert into `employees`
- (`id`,`name`,`lastname`,`salary`,`age`,`afiliation`,`status`,`active`,`workeddays`,`photo`)
- values (5, 'Michael', 'Strauss',3200,45, '2006-10-21',2,0,22, '5.jpg');
- insert into `employees`
- (`id`,`name`,`lastname`,`salary`,`age`,`afiliation`,`status`,`active`,`workeddays`,`photo`)
- values (6, 'William', 'Brown',2300,21, '2001-03-10',3,1,10, '6.jpg');
- insert into `employees`
- (`id`,`name`,`lastname`,`salary`,`age`,`afiliation`,`status`,`active`,`workeddays`,`photo`)
- values (7, 'Lucca', 'Normany',2800,36, '2006-10-02',3,1,20, '7.jpg');
2. PHP程序介紹
phpMyDataGrid主要是通過phpmydatagrid.class.php,dgscripts.js來實現的,總共加起來不到100kB,又是一個小巧的軟件。對於這兩個文件就不多講了,感興趣的同學可以“打包帶走”回去慢慢品。主要介紹該軟件的使用方法,即實例 datagrid_for_mysql.php。先看一下頁面示意圖:
程序講解:
- <?php
- include ("phpmydatagrid.class.php");
- $objGrid = new datagrid;
- $objGrid->closeTags(true);
- $objGrid->friendlyHTML();
- $objGrid->methodForm("get");
- //連接數據庫
- $objGrid->conectadb("127.0.0.1", "root", "root", "guru");//加密字符串
- $objGrid->salt("Myc0defor5tr0ng3r-Pro3EctiOn");
- $objGrid->language("en");
- //最後一列顯示的功能鍵,從左向右功能為“新增鍵”、“編輯鍵”、“刪除鍵”、“浏覽鍵”。
- $objGrid->buttons(true,true,true,true);
- //修改數值時產生的Form名稱
- $objGrid->form('employee', true);
- //可檢索列名
- $objGrid->searchby("name,lastname");
- //需要讀取的表
- $objGrid->tabla("employees");
- //索引值用於修改數據
- $objGrid->keyfield("id");
- //分頁顯示行數
- $objGrid->datarows(20);
- //默認排序方式
- $objGrid->orderby("name", "ASC");
- //顯示列設置,相關設置可參考phpmydatagrid.class.php
- $objGrid->FormatColumn("id", "ID Employee", 5, 5, 1, "50", "center", "integer");
- $objGrid->FormatColumn("name", "Name", 30, 30, 0, "150", "left");
- $objGrid->FormatColumn("lastname", "Last name", 30, 30, 0, "150", "left");
- $objGrid->FormatColumn("age", "Age", 5, 5, 0, "50", "right");//自定義日期格式
- $objGrid->FormatColumn("afiliation", "Afiliation Date", 10, 10, 0, "100", "center", "date:dmy:/");//編輯時可以自定義為<Select>模式
- $objGrid->FormatColumn("status", "Status", 5, 5, 0, "60", "left", "select:1_Single:2_Married:3_Divorced");
- //編輯時可以自定義為<CheckBox>模式
- $objGrid->FormatColumn("active", "Active", 2, 2, 0,"50", "center", "check:No:Yes");//自定義貨幣顯示形式
- $objGrid->FormatColumn("salary", "Salary", 10, 10, 0, "90", "right", "money:€");//將數據以柱狀圖顯示
- $objGrid->FormatColumn("workeddays", "Work days", 5, 2, 0, "50", "right", "chart:percent:val:31");
- $objGrid->checkable();
- $objGrid->setHeader();
- $objGrid->ajax('silent');
- echo '<html>
- <head><title>PHPDataGrid</title></head>
- <body><div align="center"><br />';
- //生成DataGrid
- $objGrid->grid();
- echo '</div></body></html>';//關閉數據庫連接
- $objGrid->desconectar();
- ?>
3. 基於Oracle簡介
對於Oracle的讀取主要是把phpmydatagrid.class.php中與MySQL連接的函數修改為Oracle,本篇是通過sqlrelay(可參考