目录
1. 创建测试数据库和表
2. PHP程序介绍
3. 基于Oracle简介
4. 源程序下载
首页 php教程 php手册 PHP实现DataGrid

PHP实现DataGrid

Jun 06, 2016 pm 07:52 PM
php 实现 通过

最近想做一个通过PHP实现DataGrid功能的东西,这样可以直接修改数据库中表的内容,而不用开发【新增数据页面】,【编辑页面】,于是乎在网上找了找,类似的东西也有几个,开源的、付费的都有,不过基本都是基于MySQL。由于需要连接Oracle所以从二次开发和页

       最近想做一个通过PHP实现DataGrid功能的东西,这样可以直接修改数据库中表的内容,而不用开发【新增数据页面】,【编辑页面】,于是乎在网上找了找,类似的东西也有几个,开源的、付费的都有,不过基本都是基于MySQL。由于需要连接Oracle所以从二次开发和页面样式来说个人觉得phpMyDataGrid还是比较好上手。本篇首先介绍基于MySQL的使用方法,再简单介绍对于Oracle连接(基于sqlrelay)的二次开发。

1. 创建测试数据库和表

<span>create database </span>`guru`<span>;</span>

<span>USE </span>`guru`<span>;</span>

<span>CREATE TABLE </span>`employees` <span>(
      </span>`id` <span>int</span><span>(</span>6<span>) NOT NULL </span>auto_increment<span>,
      </span>`name` <span>char</span><span>(</span>20<span>) </span><span>default </span><span>NULL,
      </span>`lastname` <span>char</span><span>(</span>20<span>) </span><span>default </span><span>NULL,
      </span>`salary` <span>float default </span><span>NULL,
      </span>`age` <span>int</span><span>(</span>2<span>) </span><span>default </span><span>NULL,
      </span>`afiliation` <span>date default </span><span>NULL,
      </span>`status` <span>int</span><span>(</span>1<span>) </span><span>default </span><span>NULL,
      </span>`active` <span>tinyint</span><span>(</span>1<span>) </span><span>default </span><span>NULL,
      </span>`workeddays` <span>int</span><span>(</span>2<span>) </span><span>default </span><span>NULL,
      </span>`photo` <span>char</span><span>(</span>30<span>) </span><span>default </span><span>NULL,
      </span><span>PRIMARY KEY  </span><span>(</span>`id`<span>)
)</span>

<span>insert into </span>`employees`
    <span>(</span>`id`<span>,</span>`name`<span>,</span>`lastname`<span>,</span>`salary`<span>,</span>`age`<span>,</span>`afiliation`<span>,</span>`status`<span>,</span>`active`<span>,</span>`workeddays`<span>,</span>`photo`<span>) 
    </span><span>values </span><span>(</span>1<span>, </span><span>'Ana'</span><span>, </span><span>'Trujillo'</span><span>,</span>2000<span>,</span>45<span>, </span><span>'2005-05-13'</span><span>,</span>1<span>,</span>1<span>,</span>10<span>, </span><span>'1.jpg'</span><span>);
</span><span>insert into </span>`employees` 
    <span>(</span>`id`<span>,</span>`name`<span>,</span>`lastname`<span>,</span>`salary`<span>,</span>`age`<span>,</span>`afiliation`<span>,</span>`status`<span>,</span>`active`<span>,</span>`workeddays`<span>,</span>`photo`<span>)
    </span><span>values </span><span>(</span>2<span>, </span><span>'Jennifer'</span><span>, </span><span>'Aniston'</span><span>,</span>3500<span>,</span>23<span>, </span><span>'2004-10-22'</span><span>,</span>1<span>,</span>0<span>,</span>0<span>, </span><span>'2.jpg'</span><span>);
</span><span>insert into </span>`employees` 
    <span>(</span>`id`<span>,</span>`name`<span>,</span>`lastname`<span>,</span>`salary`<span>,</span>`age`<span>,</span>`afiliation`<span>,</span>`status`<span>,</span>`active`<span>,</span>`workeddays`<span>,</span>`photo`<span>)
    </span><span>values </span><span>(</span>3<span>, </span><span>'Michael'</span><span>, </span><span>'Norman'</span><span>,</span>1200<span>,</span>19<span>, </span><span>'2007-01-10'</span><span>,</span>1<span>,</span>1<span>,</span>5<span>, </span><span>'3.jpg'</span><span>);
</span><span>insert into </span>`employees` 
    <span>(</span>`id`<span>,</span>`name`<span>,</span>`lastname`<span>,</span>`salary`<span>,</span>`age`<span>,</span>`afiliation`<span>,</span>`status`<span>,</span>`active`<span>,</span>`workeddays`<span>,</span>`photo`<span>)
    </span><span>values </span><span>(</span>4<span>, </span><span>'Vanessa'</span><span>, </span><span>'Black'</span><span>,</span>6500<span>,</span>31<span>, </span><span>'2000-11-05'</span><span>,</span>1<span>,</span>1<span>,</span>30<span>, </span><span>'4.jpg'</span><span>);
</span><span>insert into </span>`employees` 
    <span>(</span>`id`<span>,</span>`name`<span>,</span>`lastname`<span>,</span>`salary`<span>,</span>`age`<span>,</span>`afiliation`<span>,</span>`status`<span>,</span>`active`<span>,</span>`workeddays`<span>,</span>`photo`<span>)
    </span><span>values </span><span>(</span>5<span>, </span><span>'Michael'</span><span>, </span><span>'Strauss'</span><span>,</span>3200<span>,</span>45<span>, </span><span>'2006-10-21'</span><span>,</span>2<span>,</span>0<span>,</span>22<span>, </span><span>'5.jpg'</span><span>);
</span><span>insert into </span>`employees` 
    <span>(</span>`id`<span>,</span>`name`<span>,</span>`lastname`<span>,</span>`salary`<span>,</span>`age`<span>,</span>`afiliation`<span>,</span>`status`<span>,</span>`active`<span>,</span>`workeddays`<span>,</span>`photo`<span>)
    </span><span>values </span><span>(</span>6<span>, </span><span>'William'</span><span>, </span><span>'Brown'</span><span>,</span>2300<span>,</span>21<span>, </span><span>'2001-03-10'</span><span>,</span>3<span>,</span>1<span>,</span>10<span>, </span><span>'6.jpg'</span><span>);
</span><span>insert into </span>`employees` 
    <span>(</span>`id`<span>,</span>`name`<span>,</span>`lastname`<span>,</span>`salary`<span>,</span>`age`<span>,</span>`afiliation`<span>,</span>`status`<span>,</span>`active`<span>,</span>`workeddays`<span>,</span>`photo`<span>)
    </span><span>values </span><span>(</span>7<span>, </span><span>'Lucca'</span><span>, </span><span>'Normany'</span><span>,</span>2800<span>,</span>36<span>, </span><span>'2006-10-02'</span><span>,</span>3<span>,</span>1<span>,</span>20<span>, </span><span>'7.jpg'</span><span>);</span>
登录后复制

 

2. PHP程序介绍

       phpMyDataGrid主要是通过phpmydatagrid.class.php,dgscripts.js来实现的,总共加起来不到100kB,又是一个小巧的软件。对于这两个文件就不多讲了,感兴趣的同学可以“打包带走”回去慢慢品。主要介绍该软件的使用方法,即实例datagrid_for_mysql.php。先看一下页面示意图:

PHP实现DataGrid

程序讲解:

<span><?php include </span><span>(</span><span>"phpmydatagrid.class.php"</span><span>)</span>;
<span>$objGrid </span><span>= </span><span>new </span>datagrid;

<span>$objGrid</span><span>-></span><span>(</span><span>true</span><span>)</span>;  
<span>$objGrid</span><span>-></span>friendlyHTML<span>()</span>;  
<span>$objGrid</span><span>-></span>methodForm<span>(</span><span>"get"</span><span>)</span>; 
<span>//连接数据库
</span><span>$objGrid</span><span>-></span>conectadb<span>(</span><span>"127.0.0.1"</span><span>, </span><span>"root"</span><span>, </span><span>"root"</span><span>, </span><span>"guru"</span><span>)</span>;<br><span>//加密字符串
</span><span>$objGrid</span><span>-></span>salt<span>(</span><span>"Myc0defor5tr0ng3r-Pro3EctiOn"</span><span>)</span>;
<span>$objGrid</span><span>-></span>language<span>(</span><span>"en"</span><span>)</span>;
<span>//最后一列显示的功能键,从左向右功能为“新增键”、“编辑键”、“删除键”、“浏览键”。
</span><span>$objGrid</span><span>-></span>buttons<span>(</span><span>true</span><span>,</span><span>true</span><span>,</span><span>true</span><span>,</span><span>true</span><span>)</span>;
<span>//修改数值时产生的Form名称
</span><span>$objGrid</span><span>-></span>form<span>(</span><span>'employee'</span><span>, </span><span>true</span><span>)</span>;
<span>//可检索列名
</span><span>$objGrid</span><span>-></span>searchby<span>(</span><span>"name,lastname"</span><span>)</span>;
<span>//需要读取的表
</span><span>$objGrid</span><span>-></span>tabla<span>(</span><span>"employees"</span><span>)</span>;
<span>//索引值用于修改数据
</span><span>$objGrid</span><span>-></span>keyfield<span>(</span><span>"id"</span><span>)</span>;
<span>//分页显示行数
</span><span>$objGrid</span><span>-></span>datarows<span>(</span><span>20</span><span>)</span>;
<span>//默认排序方式
</span><span>$objGrid</span><span>-></span>orderby<span>(</span><span>"name"</span><span>, </span><span>"ASC"</span><span>)</span>;
<span>//显示列设置,相关设置可参考phpmydatagrid.class.php
</span><span>$objGrid</span><span>-></span>FormatColumn<span>(</span><span>"id"</span><span>, </span><span>"ID Employee"</span><span>, </span><span>5</span><span>, </span><span>5</span><span>, </span><span>1</span><span>, </span><span>"50"</span><span>, </span><span>"center"</span><span>, </span><span>"integer"</span><span>)</span>;
<span>$objGrid</span><span>-></span>FormatColumn<span>(</span><span>"name"</span><span>, </span><span>"Name"</span><span>, </span><span>30</span><span>, </span><span>30</span><span>, </span><span>0</span><span>, </span><span>"150"</span><span>, </span><span>"left"</span><span>)</span>;
<span>$objGrid</span><span>-></span>FormatColumn<span>(</span><span>"lastname"</span><span>, </span><span>"Last name"</span><span>, </span><span>30</span><span>, </span><span>30</span><span>, </span><span>0</span><span>, </span><span>"150"</span><span>, </span><span>"left"</span><span>)</span>;
<span>$objGrid</span><span>-></span>FormatColumn<span>(</span><span>"age"</span><span>, </span><span>"Age"</span><span>, </span><span>5</span><span>, </span><span>5</span><span>, </span><span>0</span><span>, </span><span>"50"</span><span>, </span><span>"right"</span><span>)</span>;<br><span>//自定义日期格式
</span><span>$objGrid</span><span>-></span>FormatColumn<span>(</span><span>"afiliation"</span><span>, </span><span>"Afiliation Date"</span><span>, </span><span>10</span><span>, </span><span>10</span><span>, </span><span>0</span><span>, </span><span>"100"</span><span>, </span><span>"center"</span><span>, </span><span>"date:dmy:/"</span><span>)</span>;<br><span>//编辑时可以自定义为<select>模式</select></span>
<span>$objGrid</span><span>-></span>FormatColumn<span>(</span><span>"status"</span><span>, </span><span>"Status"</span><span>, </span><span>5</span><span>, </span><span>5</span><span>, </span><span>0</span><span>, </span><span>"60"</span><span>, </span><span>"left"</span><span>, </span><span>"select:1_Single:2_Married:3_Divorced"</span><span>)</span>;
<span><span>//编辑时可以自定义为<checkbox>模式</checkbox></span>
</span><span>$objGrid</span><span>-></span>FormatColumn<span>(</span><span>"active"</span><span>, </span><span>"Active"</span><span>, </span><span>2</span><span>, </span><span>2</span><span>, </span><span>0</span><span>,</span><span>"50"</span><span>, </span><span>"center"</span><span>, </span><span>"check:No:Yes"</span><span>)</span>;<br><span>//自定义货币显示形式</span>
<span>$objGrid</span><span>-></span>FormatColumn<span>(</span><span>"salary"</span><span>, </span><span>"Salary"</span><span>, </span><span>10</span><span>, </span><span>10</span><span>, </span><span>0</span><span>, </span><span>"90"</span><span>, </span><span>"right"</span><span>, </span><span>"money:€"</span><span>)</span>;<br><span>//将数据以柱状图显示</span>
<span>$objGrid</span><span>-></span>FormatColumn<span>(</span><span>"workeddays"</span><span>, </span><span>"Work days"</span><span>, </span><span>5</span><span>, </span><span>2</span><span>, </span><span>0</span><span>, </span><span>"50"</span><span>, </span><span>"right"</span><span>, </span><span>"chart:percent:val:31"</span><span>)</span>;
<span>$objGrid</span><span>-></span>checkable<span>()</span>;
<span>$objGrid</span><span>-></span>setHeader<span>()</span>;
<span>$objGrid</span><span>-></span>ajax<span>(</span><span>'silent'</span><span>)</span>;
<span>echo </span><span>'
      <title>PHPDataGrid</title>
      <div align="center">
<br>';
<span>//生成DataGrid
</span><span>$objGrid</span><span>-></span>grid<span>()</span>;
<span>echo </span><span>'</span>
</div>'</span>;<br><span>//关闭数据库连接</span>
<span>$objGrid</span><span>-></span>desconectar<span>()</span>;
<span>?></span></span>
登录后复制

3. 基于Oracle简介

对于Oracle的读取主要是把phpmydatagrid.class.php中与MySQL连接的函数修改为Oracle,本篇是通过sqlrelay(可参考http://sqlrelay.sourceforge.net/)进行的Oracle连接,当然也可以使用PHP自带的OCI8模块(效率有些低),修改后另存为phporadatagrid.class.php即可在其他程序(datagrid_for_oracle.php)中调用。以上涉及的所有程序均可在压缩包中找到。
希望对大家有用!


4. 源程序下载

 

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
2 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

CakePHP 项目配置 CakePHP 项目配置 Sep 10, 2024 pm 05:25 PM

在本章中,我们将了解CakePHP中的环境变量、常规配置、数据库配置和电子邮件配置。

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

为了在 cakephp4 中处理日期和时间,我们将使用可用的 FrozenTime 类。

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

为了进行文件上传,我们将使用表单助手。这是文件上传的示例。

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

在本章中,我们将学习以下与路由相关的主题?

CakePHP 使用数据库 CakePHP 使用数据库 Sep 10, 2024 pm 05:25 PM

在 CakePHP 中使用数据库非常容易。本章我们将了解CRUD(创建、读取、更新、删除)操作。

CakePHP 创建验证器 CakePHP 创建验证器 Sep 10, 2024 pm 05:26 PM

可以通过在控制器中添加以下两行来创建验证器。

See all articles