search
Homephp教程php手册php学习,一个简单的Calendar(2) 一个简单的活动页面

有了前面的基础,后面就是将页面展示出来。

预览图如下:1号和31号分别有活动,会一并显示出来

image

 

这里需要搞定几个问题,一个就是数据库的连接,我们用\sys\class\class.db_connect.inc.php

<?php 
 
<span style="color: #008000">/*</span>
<span style="color: #008000"> * 数据库操作(数据库访问,认证等)</span>
<span style="color: #008000"> */</span>
 
<span style="color: #0000ff">class</span> DB_Connect
{
    <span style="color: #008000">/**</span>
<span style="color: #008000">     * Stores a database object</span>
<span style="color: #008000">     *</span>
<span style="color: #008000">     * @var object A database object</span>
<span style="color: #008000">     */</span>
    <span style="color: #0000ff">protected</span> $db;
 
    <span style="color: #008000">/**</span>
<span style="color: #008000">     * Checks for a DB object or creates one if one isn't found</span>
<span style="color: #008000">     *</span>
<span style="color: #008000">     * @param object $dbo A database object</span>
<span style="color: #008000">     */</span>
    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">function</span> __construct($db = NULL)
    {
        <span style="color: #0000ff">if</span> (is_object($db)) {
            $this->db = $db;
        } <span style="color: #0000ff">else</span> {
            <span style="color: #008000">// Constants are defined in /sys/config/db-cred.inc.php</span>
            $dsn = <span style="color: #006080">"mysql:host="</span> . DB_HOST . <span style="color: #006080">";dbname="</span> . DB_NAME;
            try {
                $this->db = <span style="color: #0000ff">new</span> PDO($dsn, DB_USER, DB_PASS, <span style="color: #0000ff">array</span>(PDO::MYSQL_ATTR_INIT_COMMAND => <span style="color: #006080">'SET NAMES '</span> . DB_ENCODE));
            } catch (Exception $e) {
                <span style="color: #008000">// If the DB connection fails, output the error</span>
                <span style="color: #0000ff">die</span> ($e->getMessage());
            }
        }
    }
}
 
?>

程序中需要引入DB_USER等的定义文件:db-cred.inc.php

<?php 
<span style="color: #008000">/*</span>
<span style="color: #008000"> * Created on 2012-4-24 by xiongxuebing</span>
<span style="color: #008000"> */</span>
 <span style="color: #008000">/*</span>
<span style="color: #008000">* Create an empty array to store constants</span>
<span style="color: #008000">*/</span>
$C = <span style="color: #0000ff">array</span>();
<span style="color: #008000">/*</span>
<span style="color: #008000">* The database host URL</span>
<span style="color: #008000">*/</span>
$C[<span style="color: #006080">'DB_HOST'</span>] = <span style="color: #006080">'localhost'</span>;
<span style="color: #008000">/*</span>
<span style="color: #008000">* The database username</span>
<span style="color: #008000">*/</span>
$C[<span style="color: #006080">'DB_USER'</span>] = <span style="color: #006080">'root'</span>;
<span style="color: #008000">/*</span>
<span style="color: #008000">* The database password</span>
<span style="color: #008000">*/</span>
$C[<span style="color: #006080">'DB_PASS'</span>] = <span style="color: #006080">'root'</span>;
<span style="color: #008000">/*</span>
<span style="color: #008000">* The name of the database to work with</span>
<span style="color: #008000">*/</span>
$C[<span style="color: #006080">'DB_NAME'</span>] = <span style="color: #006080">'php-jquery_example'</span>;
 
$C[<span style="color: #006080">'DB_ENCODE'</span>] = <span style="color: #006080">'UTF8'</span>;
 
?>

 

需要注意的是,类似DB_HOST的常量并没有直接定义,而是通过在/sys/core/init.inc.php中进行定义:

foreach ($C as $name => $val) {<br>    define($name, $val);<br>}
原文件如下的示:
 
<?php 
<span style="color: #008000">/*</span>
<span style="color: #008000"> * Created on 2016-6-19 by luhx</span>
<span style="color: #008000"> */</span>
 
session_start();
<span style="color: #008000">/*</span>
<span style="color: #008000">* Generate an anti-CSRF token if one doesn't exist</span>
<span style="color: #008000">*/</span>
<span style="color: #0000ff">if</span> (!<span style="color: #0000ff">isset</span>($_SESSION[<span style="color: #006080">'token'</span>])) {
    $_SESSION[<span style="color: #006080">'token'</span>] = sha1(uniqid(mt_rand(), TRUE));
}
 
<span style="color: #008000">/*</span>
<span style="color: #008000">* Include the necessary configuration info</span>
<span style="color: #008000">*/</span>
<span style="color: #0000ff">include_once</span> <span style="color: #006080">'../sys/config/db-cred.inc.php'</span>;
 
<span style="color: #008000">/*</span>
<span style="color: #008000">* Define constants for configuration info</span>
<span style="color: #008000">*/</span>
<span style="color: #0000ff">foreach</span> ($C <span style="color: #0000ff">as</span> $name => $val) {
    define($name, $val);
}
<span style="color: #008000">/*</span>
<span style="color: #008000">* Create a PDO object</span>
<span style="color: #008000">*/</span>
$dsn = <span style="color: #006080">"mysql:host="</span> . DB_HOST . <span style="color: #006080">";dbname="</span> . DB_NAME;
$dbo = <span style="color: #0000ff">new</span> PDO($dsn, DB_USER, DB_PASS);
<span style="color: #008000">/*</span>
<span style="color: #008000">* Define the auto-load function for classes</span>
<span style="color: #008000">*/</span>
<span style="color: #0000ff">function</span> __autoload($<span style="color: #0000ff">class</span>)
{
    $filename = <span style="color: #006080">"../sys/class/class."</span> . $<span style="color: #0000ff">class</span> . <span style="color: #006080">".inc.php"</span>;
    <span style="color: #0000ff">if</span> (file_exists($filename)) {
        <span style="color: #0000ff">include_once</span> $filename;
    }
}
 
?>

 

接下来需显示日历:index.php

<?php 
<span style="color: #008000">/*</span>
<span style="color: #008000"> * Created on 2012-4-24 by xiongxuebing</span>
<span style="color: #008000"> */</span>
<span style="color: #008000">/*</span>
<span style="color: #008000">* 包含必须的文件</span>
<span style="color: #008000">*/</span>
 
<span style="color: #0000ff">include_once</span> <span style="color: #006080">'../sys/core/init.inc.php'</span>;
<span style="color: #008000">/*</span>
<span style="color: #008000">* 载入日历</span>
<span style="color: #008000">*/</span>
$cal = <span style="color: #0000ff">new</span> Calendar($dbo, <span style="color: #006080">"2010-01-01 12:00:00"</span>);
 
<span style="color: #008000">/**</span>
<span style="color: #008000"> * 初始化标题和样式文件</span>
<span style="color: #008000"> */</span>
$page_title = <span style="color: #006080">"Events Calendar"</span>;
$css_files = <span style="color: #0000ff">array</span>(<span style="color: #006080">'style.css'</span>);
<span style="color: #0000ff">include_once</span> <span style="color: #006080">'assets/common/header.inc.php'</span>;
?>
 
<?php 
<span style="color: #008000">/*</span>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.