php学习,一个简单的Calendar(2) 一个简单的活动页面
有了前面的基础,后面就是将页面展示出来。
预览图如下:1号和31号分别有活动,会一并显示出来
这里需要搞定几个问题,一个就是数据库的连接,我们用\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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



With the development of the Internet, the demand for dynamic web pages is increasing. As a mainstream programming language, PHP is widely used in web development. So, for beginners, how to learn PHP development? 1. Understand the basic knowledge of PHP. PHP is a scripting language that can be directly embedded in HTML code and parsed and run through a web server. Therefore, before learning PHP, you can first understand the basics of front-end technologies such as HTML, CSS, and JavaScript to better understand the operation of PHP.

PHP study notes: Web crawler and data collection Introduction: A web crawler is a tool that automatically crawls data from the Internet. It can simulate human behavior, browse web pages and collect the required data. As a popular server-side scripting language, PHP also plays an important role in the field of web crawlers and data collection. This article will explain how to write a web crawler using PHP and provide practical code examples. 1. Basic principles of web crawlers The basic principles of web crawlers are to send HTTP requests, receive and parse the H response of the server.

PHP study notes: Modular development and code reuse Introduction: In software development, modular development and code reuse are very important concepts. Modular development can decompose complex systems into manageable small modules, improving development efficiency and code maintainability; while code reuse can reduce redundant code and improve code reusability. In PHP development, we can achieve modular development and code reuse through some technical means. This article will introduce some commonly used technologies and specific code examples to help readers better understand and apply these concepts.

PHP study notes: Performance analysis and tuning Introduction: In web development, performance is a very critical factor. A high-performance website can provide a better user experience, improve user retention, and increase business revenue. In PHP development, performance optimization is a common and important issue. This article will introduce performance analysis and tuning methods in PHP, and provide specific code examples to help readers better understand and apply these techniques. 1. Performance analysis tool Xdebug extension Xdebug is a powerful P

What is the best way to learn PHP in 2023? With the rapid development of the Internet, computer programming has become a skill with extremely high employment prospects. Among many programming languages, PHP is a language that is widely used in network development. If you want to learn PHP, it's important to know the best way to learn it. PHP is an open source, server-side scripting language used to develop dynamic websites and applications. Compared to other languages, PHP has a lower learning curve and a wide range of applications, making it an ideal choice for beginners.

PHP study notes: Form processing and data validation In web development, forms are one of the important components for users to interact with the website. When users fill out forms and submit data on the website, the website needs to process and verify the submitted data to ensure the accuracy and security of the data. This article will introduce how to use PHP to process forms and perform data validation, and provide specific code examples. Form submission and data preprocessing In HTML, we need to use the <form> tag to create a form and specify the form

Learn video special effects and filter processing function methods in PHP. PHP is a powerful programming language that is widely used in the field of web development. With the development of website design, video special effects and filter processing are becoming more and more popular. This article will introduce how to use PHP to implement video special effects and filter processing, as well as some commonly used function methods. 1. Install the ffmpeg extension. To process videos, we need to install the ffmpeg extension. Through this extension, we can directly call the ffmpeg command in PHP for video processing. Installation process

PHP study notes: WeChat applet and public account development With the rapid development of the mobile Internet, WeChat has become one of the most widely used social media platforms. In order to meet the needs of users, WeChat provides two development methods: mini programs and official accounts. This article will introduce how to use PHP language to develop WeChat mini programs and public accounts, and provide some specific code examples. 1. Preparations for WeChat Mini Program Development First, we need to apply for a Mini Program account on the WeChat public platform and obtain the AppID and AppSec of the Mini Program
