详情页

创建新表创建表类的方法

时间:2023年09月15日

编辑:佚名

在主题或者插件的include.php文件顶部引用文件
点击复制代码 PHP
include_once __DIR__.'/database/index.php';
在引入的php里面写入创建表类的代码
点击复制代码 PHP
$ytecn_database = array(
    'ytecn_table'   => array(
        'name'           => '%pre%ytecn_table',
        'info'           => array(
            'ID'           => array('ytecn_ID','integer','',0),
            'UID'        => array('ytecn_UID','integer','',0),
            'Name'        => array('ytecn_Name','string',255,''),
            'Content'        => array('ytecn_Content', 'string', '', '')
            'Status'      => array('ytecn_Status','integer','',0),
        ),
    ),
);
foreach ($ytecn_database as $k => $v) {
    $table[$k] = $v['name'];
    $datainfo[$k] = $v['info'];
}
function ytecn_table_CreateTable() {
    global $zbp, $ytecn_database;
    foreach ($ytecn_database as $k => $v) {
        if (!$zbp->db->ExistTable($v['name'])) {
            $s = $zbp->db->sql->CreateTable($v['name'],$v['info']);
            $zbp->db->QueryMulit($s);
        }
    }
}
class ytecntable extends Base {
    public function __construct() {
        global $zbp;
        parent::__construct($zbp->table['ytecn_table'], $zbp->datainfo['ytecn_table'], __CLASS__);
    }
    public function __get($name)
    {
        global $zbp;
        switch ($name) {
            case 'Url':
                return "详情的url访问地址";
                break;
            case 'Author':
                return $zbp->GetMemberByID($this->UID);
            default:
                return parent::__get($name);
                break;
        }
    }
    public static function GetList($select = null, $w = null, $order = null, $limit = null, $option = null) {
        global $zbp;
        if (empty($select)) {
            $select = array('*');
        }
        if (empty($w)) {
            $w = array();
        }
        $sql = $zbp->db->sql->Select(
            $zbp->table['ytecn_database'],
            $select,
            $w,
            $order,
            $limit,
            $option
        );
        $result = $zbp->GetListType('ytecntable', $sql);
        return $result;
    }
}
在启动主题或者插件的时候执行创建表的函数
点击复制代码 PHP
function InstallPlugin_****() {
    ytecn_table_CreateTable();
}
调用表列表
点击复制代码 PHP
$where = array();
$where[] = array('=', 'ytecn_Status', 0);
$array= ytecntable::GetList(null, $where, array("ytecn_ID" => "DESC"));
foreach ($arrayas $item) {
    echo $item->Name;
}
添加数据
点击复制代码 PHP
$table= new ytecntable;
$table->Name = '豫唐';
$table->Status= 0;
$table->Save();
修改数据
点击复制代码 PHP
$id = (int)GetVars("id", "POST");
$table= new ytecntable;
$table->LoadInfoByID($id);
$table->Name = '豫唐ytecn';
$table->Status= 1;
$table->Save();
删除数据
点击复制代码 PHP
$id = GetVars("id", "GET");
$table= new ytecntable;
if (!empty($id)) {
    $table->LoadInfoByID((int) $id);
}
$table->Del();
相关文章
猜你需要