Rumah  >  Artikel  >  php教程  >  php学习,一个简单的Calendar(2) 一个简单的活动页面

php学习,一个简单的Calendar(2) 一个简单的活动页面

WBOY
WBOYasal
2016-06-20 08:42:13896semak imbas

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

预览图如下: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>

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn