:::

4-6 admin.php

<?php
/*引入檔案(初始設定)*/
require_once 'header.php';
$page_title = '活動報名管理';

if ($group != "admin") {
    header("location: index.php");
    exit;
}

/*執行流程*/
try
{
    $action_id = isset($_REQUEST['action_id']) ? intval($_REQUEST['action_id']) : '';
    switch ($op) {

        case "insert_action":
            $action_id = insert_action();
            header("location: index.php?action_id=$action_id");
            exit;

        case "edit_action":
            $content = action_form($action_id);
            break;

        case "update_action":
            update_action($action_id);
            header("location: index.php?action_id=$action_id");
            exit;

        case "delete_action":
            delete_action($action_id);
            header("location: index.php");
            exit;

        //預設動作
        default:
            $content = action_form();
            break;
    }
} catch (exception $e) {
    $error = $e->getMessage();
}

/*輸出結果*/
require_once "footer.php";

/*自訂函數區*/

//活動表單
function action_form($action_id = '')
{
    global $db;

    require_once "class/php-bootstrap-form/PFBC/Form.php";

    if ($action_id) {
        $sql = "select * from `actions` where action_id='{$action_id}'";
        if (!$result = $db->query($sql)) {
            throw new Exception($db->error);
        }

        $values = $result->fetch_assoc();
        $op     = 'update_action';
    } else {
        $values = [
            'title'       => '活動',
            'action_date' => date("Y-m-d"),
            'end_date'    => date("Y-m-d H:i:00"),
            'enable'      => 1,
            'content'     => '活動內容~~~',
        ];
        $op = 'insert_action';
    }

    ob_start();
    echo '<script type="text/javascript" src="class/My97DatePicker/WdatePicker.js"></script>';
    Form::open("action", $values);
    Form::Hidden("op", $op);
    Form::Hidden("action_id", $action_id);
    Form::Textbox("活動名稱", "title", ['required' => 1]);
    Form::Textbox("活動日期", "action_date", ['required' => 1, 'onClick' => "WdatePicker()"]);
    Form::Textbox("截止日期", "end_date", ['required' => 1, 'onClick' => "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:00'})"]);
    Form::YesNo("使否啟用", "enable");
    Form::CKEditor("活動內容", "content");
    Form::Button('儲存', 'submit');
    Form::close(false);
    $form = ob_get_contents();
    ob_end_clean();
    return $form;
}

//新增活動
function insert_action()
{
    global $db;

    // 過濾變數
    $title       = clean_var('title', '活動名稱');
    $action_date = clean_var('action_date', '活動日期');
    $end_date    = clean_var('end_date', '截止日期');
    $enable      = clean_var('enable', '使否啟用');
    $content     = clean_var('content', '活動內容');
    $uid         = $_SESSION['uid'];

    // 連線資料庫
    $sql = "INSERT INTO `actions` ( `title`, `content`, `action_date`, `end_date`, `uid`, `enable`)
    VALUES ('{$title}', '{$content}', '{$action_date}', '{$end_date}', '{$uid}', '{$enable}')";
    if (!$db->query($sql)) {
        throw new Exception($db->error);
    }

    $action_id = $db->insert_id;
    return $action_id;
}

//更新活動
function update_action($action_id)
{
    global $db;

    // 過濾變數
    $title       = clean_var('title', '活動名稱');
    $action_date = clean_var('action_date', '活動日期');
    $end_date    = clean_var('end_date', '截止日期');
    $enable      = clean_var('enable', '使否啟用');
    $content     = clean_var('content', '活動內容');
    $uid         = $_SESSION['uid'];

    // 連線資料庫
    $sql = "UPDATE `actions` SET
    `title`='{$title}',
    `content`='{$content}',
    `action_date`='{$action_date}',
    `end_date`='{$end_date}',
    `uid`='{$uid}',
    `enable`='{$enable}'
    WHERE `action_id`= '$action_id'";
    if (!$db->query($sql)) {
        throw new Exception($db->error);
    }
}

//刪除活動
function delete_action($action_id)
{
    global $db;

    // 連線資料庫
    $sql = "DELETE FROM `actions` WHERE `action_id`= '$action_id'";
    if (!$db->query($sql)) {
        throw new Exception($db->error);
    }
}

 


:::

書籍目錄

展開 | 闔起

http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D30%26tbdsn%3D828

計數器

今天: 1752175217521752
昨天: 2395239523952395
總計: 7917697791769779176977917697791769779176977917697