当前位置:首页 > PHP教程 > PHP常见问题

PHP DataGrid 实现代码

由于需要连接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(可参考http://sqlrelay.sourceforge.net/)进行的oracle连接,当然也可以使用php自带的oci8模块(效率有些低),修改后另存为phporadatagrid.class.php即可在其他程序(datagrid_for_oracle.php)中调用。以上涉及的所有程序均可在压缩包中找到。
希望对大家有用!
4. 源程序下载



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!