把数据库操作函数集中到一个文件 table_gycp.php 中,然后在控制文件 gycp_publish.php 中调用这些函数。其中,在 gycp_publish.php 文件中使用了 C::t函数。
/gycp.php
/source/class/table/table_gycp.php
/source/module/gycp/gycp_publish.php
/template/default/gycp/gycp_publish.htm
运行结果:
/gycp.php 内容与前面一样:
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: gycp.php cuibq $
*/
define('APPTYPEID', 101);
define('CURSCRIPT', 'gycp');
require './source/class/class_core.php';
$discuz = C::app();
$discuz->reject_robot();
$modarray = array('gycp', 'gycp_publish');
$mod = getgpc('mod');
$mod = (empty($mod) || !in_array($mod, $modarray)) ? 'error' : $mod;
$discuz->init();
define('CURMODULE', $mod);
require DISCUZ_ROOT.'./source/module/gycp/gycp_publish.php';
?>
/source/class/table/table_gycp.php :
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
* 工业产品表
* $Id: table_gycp.php
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class table_gycp extends discuz_table
{
public function __construct() {
$this->_table = 'gycp';
$this->_pk = 'id';
parent::__construct();
}
public function fetch_all_by_displayorder() {
return DB::fetch_all('SELECT * FROM %t ORDER BY id DESC', array($this->_table), $this->_pk);
}
//public function fetch_by_id_username($id, $username, $adminid = 1) {
//return DB::fetch_first('SELECT * FROM %t WHERE id=%d', array($this->_table, $id, $adminid, $username));
//}
//public function delete_by_id_username($ids, $username, $adminid = 1) {
//if(($ids = dintval((array)$ids, true))) {
//DB::query('DELETE FROM %t WHERE id IN(%n)', array($this->_table, $ids, $adminid, $username), false, true);
//}
//}
//public function update_displayorder_by_id_username($id, $displayorder, $username, $adminid = 1) {
//if(($id = dintval((array)$id, true))) {
//DB::query('UPDATE %t SET displayorder=%d WHERE id IN(%n)', array($this->_table, $displayorder, $id, $adminid, $username), false, true);
//}
//}
//public function update_by_id_username($id, $data, $username, $adminid = 1) {
//if(($id = dintval($id, true)) && $data && is_array($data)) {
//$adminid = dintval($adminid);
//DB::update($this->_table, $data, DB::field($this->_pk, $id).')', true);
//}
//}
}
?>
/source/module/gycp/gycp_publish.php :
<?php
// mod文件只能被入口文件引用,不能直接访问
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
$test_arr = array('aaaa', 'bbbb', 'cccc', 'dddd', 'eeee');
require_once libfile('function/discuzcode');
$querydata = C::t('gycp')->fetch_all_by_displayorder();
if(!count($querydata)) {
showmessage('querydata_nonexistence');
}
$queryarr = array();
foreach ($querydata as $gycp) {
$queryarr[] = $gycp;
}
//显示发布表单
include_once template("gycp/gycp_publish");
?>
/template/default/gycp/gycp_publish.htm :
<!--//说明: 显示公共头部模板-->
<!--{template common/header}-->
<br/>
<table class="dt mtm">
<!--{loop $queryarr $rec_data}-->
<tr>
<td width="20">$rec_data[ZB]</td>
<td width="30">$rec_data[LJ]</td>
</tr>
<!--{/loop}-->
</table>
<br/>
<!--//说明: 显示公共尾部模板-->
<!--{template common/footer}-->