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
如何学习PHP开发?如何学习PHP开发?Jun 12, 2023 am 08:09 AM

随着互联网的发展,动态网页的需求越来越大。而PHP作为一种主流的编程语言,被广泛应用于Web开发中。那么,对于初学者来说,如何学习PHP开发呢?一、了解PHP的基础知识PHP是一种脚本语言,可以直接嵌入HTML代码中,通过Web服务器进行解析运行。因此,在学习PHP之前,可以先了解HTML、CSS、JavaScript等前端技术基础,以便更好地理解PHP的作

PHP学习笔记:网络爬虫与数据采集PHP学习笔记:网络爬虫与数据采集Oct 08, 2023 pm 12:04 PM

PHP学习笔记:网络爬虫与数据采集引言:网络爬虫是一种自动从互联网上抓取数据的工具,它可以模拟人的行为,浏览网页并收集所需的数据。PHP作为一种流行的服务器端脚本语言,在网络爬虫和数据采集领域也发挥了重要的作用。本文将介绍如何使用PHP编写网络爬虫,并提供实际的代码示例。一、网络爬虫的基本原理网络爬虫的基本原理是通过发送HTTP请求,接收并解析服务器响应的H

PHP学习笔记:模块化开发与代码复用PHP学习笔记:模块化开发与代码复用Oct 10, 2023 pm 12:58 PM

PHP学习笔记:模块化开发与代码复用引言:在软件开发中,模块化开发与代码复用是相当重要的概念。模块化开发可以将复杂的系统分解成可管理的小模块,提高开发效率和代码可维护性;而代码复用则可以减少冗余代码,提高代码的重用性。在PHP开发中,我们可以通过一些技术手段来实现模块化开发和代码复用。本篇文章将介绍一些常用的技术和具体代码示例,帮助读者更好地理解和应用这些概

PHP学习笔记:性能分析与调优PHP学习笔记:性能分析与调优Oct 08, 2023 pm 03:21 PM

PHP学习笔记:性能分析与调优引言:在Web开发中,性能是一个非常关键的因素。一个高性能的网站能够提供更好的用户体验,提高用户留存率,增加业务收入。而在PHP开发中,性能的优化是一个常见且重要的问题。本文将介绍PHP中性能分析与调优的方法,并提供具体的代码示例,帮助读者更好地理解和运用这些技巧。一、性能分析的工具Xdebug扩展Xdebug是一款功能强大的P

2023年,学习PHP的最佳途径是什么?2023年,学习PHP的最佳途径是什么?Sep 10, 2023 pm 09:16 PM

2023年,学习PHP的最佳途径是什么?随着互联网的快速发展,计算机编程成为了一项具有极高就业前景的技能。而在众多的编程语言中,PHP是一门被广泛应用于网络开发的语言。想要学习PHP,了解最佳的学习途径是非常重要的。PHP是一种开源的、服务器端脚本语言,它被用于开发动态网站和应用程序。相比于其他语言,PHP具有较低的学习曲线和广泛的应用领域,使其成为初学者的

PHP学习笔记:表单处理与数据验证PHP学习笔记:表单处理与数据验证Oct 09, 2023 am 08:52 AM

PHP学习笔记:表单处理与数据验证在网页开发中,表单是用户与网站进行交互的重要组件之一。当用户在网站上填写表单并提交数据时,网站需要对提交的数据进行处理和验证,确保数据的准确性和安全性。本文将介绍如何使用PHP来处理表单和进行数据验证,并提供具体的代码示例。表单提交和数据预处理在HTML中,我们需要使用&lt;form&gt;标签来创建一个表单,并指定表单的

PHP学习笔记:前后端分离与API设计PHP学习笔记:前后端分离与API设计Oct 08, 2023 am 09:42 AM

PHP学习笔记:前后端分离与API设计概述:随着互联网的不断发展和用户需求的不断增加,前后端分离的开发模式越来越受到开发者的重视。前后端分离是指将前端和后端的开发分离开来,通过API进行数据交互,实现开发的高效性和灵活性。本文将介绍前后端分离的概念,以及如何设计API。前后端分离的概念:传统的Web开发模式是前后端耦合的,即前端和后端的开发是在同一个项目中进

PHP学习笔记:微信小程序与公众号开发PHP学习笔记:微信小程序与公众号开发Oct 09, 2023 am 11:05 AM

PHP学习笔记:微信小程序与公众号开发随着移动互联网的快速发展,微信成为了人们使用最广泛的社交媒体平台之一。为了满足用户的需求,微信提供了两种开发方式:小程序和公众号。本文将介绍如何使用PHP语言开发微信小程序和公众号,并提供一些具体的代码示例。一、微信小程序开发准备工作首先,我们需要在微信公众平台申请一个小程序账号,并获取到小程序的AppID和AppSec

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript 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.