淡水河边原创,转载请保留链接。两个tinybutstrong(小强)的Database plug-ins 。一个是配合adodb使用的,一个是在ms下无需adodb的(效率貌似比adodb好一些)。
淡水河边使用access测试,access能跑,插件示例的mysql就更不是问题了。
php代码如下:
PHP代码
- <?php
- include_once("tbs_class_php4.php");
- include_once("adodb/adodb.inc.php");
- include_once("tbsdb_jladodb.php"); // file that contain John Lim's ADOdb function
- $conn = NewADOConnection('ado');
- //$conn = NewADOConnection('ado_access');//you can use this
- //$conn = NewADOConnection('access');//you can use this
- $access = realpath("Employees.mdb");
- $myDSN = 'Driver={Microsoft Access Driver (*.mdb)};Dbq='. $access;
- $conn->Connect($myDSN);
- //$conn->debug = true;
- $tbs = new clsTinyButStrong;
- $tbs->LoadTemplate("list_employee.html");
- $query = "Select * from Employees";
- $tbs->MergeBlock("blk1","adodb",$query);
- //$tbs->MergeBlock("blk1",$conn,$query);//you can use this
- $tbs->Show();
- ?>
tbsdb_jladodb.php是tbs的插件。可以到官方下载
PHP代码
- <?php
- /*
- John Lim's ADOdb functions for TinyButStrong Template Engine
- Version 1.00, 2004-09-22, Skrol29
- http://www.tinybutstrong.com
- Example:
- $conn = NewADOConnection('mysql');
- $conn->Connect('host', 'uid', 'pdw', 'db');
- ...
- $TBS->MergeBlock('blk1','adodb','SELECT * FROM t_examples');
- Notes:
- - Before the merge, you have to open a connection using the global variable $conn (change it if you want into the custom function).
- - The keyword 'adodb' enables TBS to call the custom functions.
- */
- function tbsdb_adodb_open($source,$query) {
- global $conn ;
- $conn->SetFetchMode(ADODB_FETCH_ASSOC);
- $rs = $conn->Execute($query) ;
- return $rs ;
- }
- function tbsdb_adodb_fetch($rs) {
- return $rs->FetchRow() ;
- }
- function tbsdb_adodb_close($rs) {
- $rs->Close() ;
- }
- ?>
html模板:
XML/HTML代码
- <table border="1" cellpadding="2" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
- <tr>
- <td width="25%" bgcolor="#DDDDDD"><b><font size="1" face="Verdana">Employee No.</font></b></td>
- <td width="25%" bgcolor="#DDDDDD"><b><font size="1" face="Verdana">Employee Name</font></b></td>
- <td width="25%" bgcolor="#DDDDDD"><b><font size="1" face="Verdana">Designation</font></b></td>
- <td width="25%" bgcolor="#DDDDDD"><b><font size="1" face="Verdana">Department</font></b></td>
- </tr>
- <row>
- <tr>
- <td width="25%"><font size="1" face="Verdana">[blk1.EmployeeNO;block=row]</font></td>
- <td width="25%"><font size="1" face="Verdana">[blk1.FirstName]</font></td>
- <td width="25%"><font size="1" face="Verdana">[blk1.Positions]</font></td>
- <td width="25%"><font size="1" face="Verdana">[blk1.DepartmentDescription]</font></td>
- </tr>
- </row>
- <row>
- <tr>
- <td width="25%" bgcolor="#CCCCFF"><font size="1" face="Verdana">[blk1.EmployeeNO;block=row]</font></td>
- <td width="25%" bgcolor="#CCCCFF"><font size="1" face="Verdana">[blk1.FirstName]</font></td>
- <td width="25%" bgcolor="#CCCCFF"><font size="1" face="Verdana">[blk1.Positions]</font></td>
- <td width="25%" bgcolor="#CCCCFF"><font size="1" face="Verdana">[blk1.DepartmentDescription]</font></td>
- </tr>
- </row>
- </table>
如果是采用access或是mssql可以有另一种选择。大致相同。无需adpdb支持。
虽然也有adodb的字样,但是他说的是ms的adodb。
php文件如下:
PHP代码
- <?php
- include_once("tbs_class_php4.php");
- include_once("tbsdb_msadodb.php");
- $conn = new com('ADODB.Connection');
- $access = realpath("Employees.mdb");
- $myDSN = 'Driver={Microsoft Access Driver (*.mdb)};Dbq='. $access;
- $conn->Open($myDSN);
- $tbs = new clsTinyButStrong;
- $tbs->LoadTemplate("list_employee.html");
- $query = "Select * from Employees";
- $tbs->MergeBlock("blk1",$conn,$query);
- $tbs->Show();
- ?>
tbsdb_msadodb.php文件如下:
PHP代码
- <?php
- /*
- Ms ADODB functions for TinyButStrong Template Engine
- Version 1.00, 2004-09-22, Skrol29
- http://www.tinybutstrong.com
- Example:
- // SQL-Server
- $conn_str = 'PROVIDER=SQLOLEDB.1;PERSIST SECURITY INFO=FALSE;DATA SOURCE=server;INITIAL CATALOG=database;' ;
- // Ms Access
- $conn_str = 'DRIVER=Microsoft Access Driver (*.mdb);DBQ=E:\My Folder\My_Database.mdb';
- $conn = new com('ADODB.Connection');
- $conn->Open($conn_str,'uid','pwd');
- ...
- $TBS->MergeBlock('blk1',$conn,'SELECT * FROM t_examples');
- */
- function tbsdb_com_open($Source, $Query) {
- if (strlen(@$Source->ConnectionString())>0) { // Look if it's a Connection object
- if ($Source->State==1) {
- $Rs = @$Source->Execute($Query); // We use the Connection object reather than the Recordset object in order to manage errors
- if ($Source->Errors->Count>0) {
- echo 'TinyButStrong functions for ADODB: error message when opening the query: '.htmlentities($Source->Errors[0]->Description).' <br>';
- return false;
- } elseif ($Rs->State!=1) {
- echo 'TinyButStrong functions for ADODB: The ADODB query doesn\'t return a RecordSet or the ResordSet is not ready. <br>';
- return false;
- }
- } else {
- echo 'TinyButStrong functions for ADODB: Connection is not open or not ready. <br>';
- return false;
- }
- } elseif (strlen(@$Source->CursorType())>0) { // Look if it's a RecordSet object
- if ($Source->State==1) {
- $Rs = $Source;
- } else {
- echo 'TinyButStrong functions for ADODB: The specified ADODB Recordset is not open or not ready. <br>';
- return false;
- }
- } else {
- echo 'TinyButStrong functions for ADODB: The specified COM Object is not a Connection or a Recordset. <br>';
- return false;
- }
- if ($Rs===false) return false;
- $Fields = array();
- $iMax = $Rs->Fields->Count;
- for ($i=0;$i<$iMax;$i++) {
- $Fields[$i] = ''.$Rs->Fields[$i]->Name;
- }
- return array('recset'=>$Rs,'fields'=>$Fields);
- }
- function tbsdb_com_fetch(&$Rs) {
- if ($Rs['recset']->EOF()) {
- $Rec = false;
- } else {
- $Rec = array();
- foreach ($Rs['fields'] as $colid=>$colname) {
- $Rec[$colname] = $Rs['recset']->Fields[$colid]->Value;
- }
- $Rs['recset']->MoveNext(); // brackets () must be there
- }
- return $Rec;
- }
- function tbsdb_com_close(&$Rs) {
- $Rs['recset']->Close;
- }
- ?>
结果是一样的。如果是采用ms的平台,用这个效率应该会高一些。



