有了前面的基礎,後面就是將頁面展示出來。 預覽圖如下:1號和31號分別有活動,會一併顯示出來 這裡需要搞定幾個問題,一個就是資料庫的連接,我們用\sys\class\class.db_connect.inc.php db = $db; } else { // Constants are define... ...
有了前面的基礎,後面就是將頁面展示出來。
預覽圖如下:1號和31號分別有活動,會一併顯示出來
這裡需要搞定幾個問題,一個就是資料庫的連接,我們用\sys\class\class.db_connect.inc.php
<?php
/*
* 資料庫操作(資料庫訪問,認證等)
*/
class DB_Connect
{
/**
* Stores a database object
*
* @var object A database object
*/
protected $db;
/**
* Checks for a DB object or creates one if one isn't found
*
* @param object $dbo A database object
*/
protected function __construct($db = NULL)
{
if (is_object($db)) {
$this->db = $db;
} else {
// Constants are defined in /sys/config/db-cred.inc.php
$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
try {
$this->db = new PDO($dsn, DB_USER, DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . DB_ENCODE));
} catch (Exception $e) {
// If the DB connection fails, output the error
die ($e->getMessage());
}
}
}
}
?>
程式中需要引入DB_USER等的定義文件:db-cred.inc.php
<?php
/*
* Created on 2012-4-24 by xiongxuebing
*/
/*
* Create an empty array to store constants
*/
$C = array();
/*
* The database host URL
*/
$C['DB_HOST'] = 'localhost';
/*
* The database username
*/
$C['DB_USER'] = 'root';
/*
* The database password
*/
$C['DB_PASS'] = 'root';
/*
* The name of the database to work with
*/
$C['DB_NAME'] = 'php-jquery_example';
$C['DB_ENCODE'] = 'UTF8';
?>
需要註意的是,類似DB_HOST的常量並沒有直接定義,而是通過在/sys/core/init.inc.php中進行定義:
foreach ($C as $name => $val) {
define($name, $val);
}
原文件如下的示:
<?php
/*
* Created on 2016-6-19 by luhx
*/
session_start();
/*
* Generate an anti-CSRF token if one doesn't exist
*/
if (!isset($_SESSION['token'])) {
$_SESSION['token'] = sha1(uniqid(mt_rand(), TRUE));
}
/*
* Include the necessary configuration info
*/
include_once '../sys/config/db-cred.inc.php';
/*
* Define constants for configuration info
*/
foreach ($C as $name => $val) {
define($name, $val);
}
/*
* Create a PDO object
*/
$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
$dbo = new PDO($dsn, DB_USER, DB_PASS);
/*
* Define the auto-load function for classes
*/
function __autoload($class)
{
$filename = "../sys/class/class." . $class . ".inc.php";
if (file_exists($filename)) {
include_once $filename;
}
}
?>
接下來需顯示日曆:index.php
<?php
/*
* Created on 2012-4-24 by xiongxuebing
*/
/*
* 包含必須的文件
*/
include_once '../sys/core/init.inc.php';
/*
* 載入日曆
*/
$cal = new Calendar($dbo, "2010-01-01 12:00:00");
/**
* 初始化標題和樣式文件
*/
$page_title = "Events Calendar";
$css_files = array('style.css');
include_once 'assets/common/header.inc.php';
?>
<?php