PHP實現直接修改表內容DataGrid功能_PHP教程

來源:互聯網
上載者:User
最近想做一個通過PHP實現DataGrid功能的東西,這樣可以直接修改資料庫中表的內容,而不用開發【新增資料頁面】,【編輯頁面】,於是乎在網上找了找,類似的東西也有幾個,開源的、付費的都有,不過基本都是基於MySQL。由於需要串連Oracle所以從二次開發和頁面樣式來說個人覺得 phpMyDataGrid還是比較好上手。本篇首先介紹基於MySQL的使用方法,再簡單介紹對於Oracle串連(基於sqlrelay)的二次開發。

1. 建立測試資料庫和表

 
  1. create database `guru`;
  2. USE `guru`;
  3. CREATE TABLE `employees` (
  4. `id` int(6) NOT NULL auto_increment,
  5. `name` char(20) default NULL,
  6. `lastname` char(20) default NULL,
  7. `salary` float default NULL,
  8. `age` int(2) default NULL,
  9. `afiliation` date default NULL,
  10. `status` int(1) default NULL,
  11. `active` tinyint(1) default NULL,
  12. `workeddays` int(2) default NULL,
  13. `photo` char(30) default NULL,
  14. PRIMARY KEY (`id`)
  15. )
  16. insert into `employees`
  17. (`id`,`name`,`lastname`,`salary`,`age`,`afiliation`,`status`,`active`,`workeddays`,`photo`)
  18. values (1, 'Ana', 'Trujillo',2000,45, '2005-05-13',1,1,10, '1.jpg');
  19. insert into `employees`
  20. (`id`,`name`,`lastname`,`salary`,`age`,`afiliation`,`status`,`active`,`workeddays`,`photo`)
  21. values (2, 'Jennifer', 'Aniston',3500,23, '2004-10-22',1,0,0, '2.jpg');
  22. insert into `employees`
  23. (`id`,`name`,`lastname`,`salary`,`age`,`afiliation`,`status`,`active`,`workeddays`,`photo`)
  24. values (3, 'Michael', 'Norman',1200,19, '2007-01-10',1,1,5, '3.jpg');
  25. insert into `employees`
  26. (`id`,`name`,`lastname`,`salary`,`age`,`afiliation`,`status`,`active`,`workeddays`,`photo`)
  27. values (4, 'Vanessa', 'Black',6500,31, '2000-11-05',1,1,30, '4.jpg');
  28. insert into `employees`
  29. (`id`,`name`,`lastname`,`salary`,`age`,`afiliation`,`status`,`active`,`workeddays`,`photo`)
  30. values (5, 'Michael', 'Strauss',3200,45, '2006-10-21',2,0,22, '5.jpg');
  31. insert into `employees`
  32. (`id`,`name`,`lastname`,`salary`,`age`,`afiliation`,`status`,`active`,`workeddays`,`photo`)
  33. values (6, 'William', 'Brown',2300,21, '2001-03-10',3,1,10, '6.jpg');
  34. insert into `employees`
  35. (`id`,`name`,`lastname`,`salary`,`age`,`afiliation`,`status`,`active`,`workeddays`,`photo`)
  36. 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。先看一下頁面:

498)this.width=498;' onmousewheel = 'javascript:return big(this)' title="2009-8-11-19.22.06" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="270" alt="2009-8-11-19.22.06" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095417528.jpg" width="980" border="0" />

程式講解:

 
  1. include ("phpmydatagrid.class.php");
  2. $objGrid = new datagrid;
  3. $objGrid->closeTags(true);
  4. $objGrid->friendlyHTML();
  5. $objGrid->methodForm("get");
  6. //串連資料庫
  7. $objGrid->conectadb("127.0.0.1", "root", "root", "guru");//加密字串
  8. $objGrid->salt("Myc0defor5tr0ng3r-Pro3EctiOn");
  9. $objGrid->language("en");
  10. //最後一列顯示的功能鍵,從左向右功能為“新增鍵”、“編輯按鍵”、“刪除鍵”、“瀏覽鍵”。
  11. $objGrid->buttons(true,true,true,true);
  12. //修改數值時產生的Form名稱
  13. $objGrid->form('employee', true);
  14. //可檢索列名
  15. $objGrid->searchby("name,lastname");
  16. //需要讀取的表
  17. $objGrid->tabla("employees");
  18. //索引值用於修改資料
  19. $objGrid->keyfield("id");
  20. //分頁顯示行數
  21. $objGrid->datarows(20);
  22. //預設排序方式
  23. $objGrid->orderby("name", "ASC");
  24. //顯示列設定,相關設定可參考phpmydatagrid.class.php
  25. $objGrid->FormatColumn("id", "ID Employee", 5, 5, 1, "50", "center", "integer");
  26. $objGrid->FormatColumn("name", "Name", 30, 30, 0, "150", "left");
  27. $objGrid->FormatColumn("lastname", "Last name", 30, 30, 0, "150", "left");
  28. $objGrid->FormatColumn("age", "Age", 5, 5, 0, "50", "right");//自訂日期格式
  29. $objGrid->FormatColumn("afiliation", "Afiliation Date", 10, 10, 0, "100", "center", "date:dmy:/");//編輯時可以自訂為模式 $objGrid->FormatColumn("status", "Status", 5, 5, 0, "60", "left", "select:1_Single:2_Married:3_Divorced"); //編輯時可以自訂為模式 $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 ' PHPDataGrid "center">'; //產生DataGrid $objGrid->grid(); echo '';//關閉資料庫連接 $objGrid->desconectar(); ?> 3. 基於Oracle簡介對於Oracle的讀取主要是把phpmydatagrid.class.php中與MySQL串連的函數修改為Oracle,本篇是通過sqlrelay(可參考 http://www.bkjia.com/PHPjc/445682.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445682.htmlTechArticle最近想做一個通過PHP實現DataGrid功能的東西,這樣可以直接修改資料庫中表的內容,而不用開發【新增資料頁面】,【編輯頁面】,於是乎...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.