攻克CakePHP系列二 表單數(shù)據(jù)顯示
Jun 13, 2016 pm 12:26 PM
首先建立數(shù)據(jù)庫cake_ext,并執(zhí)行如下sql文:
- CREATE?TABLE?`companies`?(
- ??`id`?int(11)?NOT?NULL?auto_increment,
- ??`company`?varchar(50)?NOT?NULL,
- ??`price`?decimal(8,2)?NOT?NULL,
- ??`change`?decimal(8,2)?NOT?NULL,
- ??`lastudp`?date?NOT?NULL,
- ??PRIMARY?KEY??(`id`)
- )?ENGINE=MyISAM?AUTO_INCREMENT=8?DEFAULT?CHARSET=utf8;
- --?----------------------------
- --?Records?
- --?----------------------------
- INSERT?INTO?`companies`?VALUES?('1',?'3m?Co',?'71.72',?'0.02',?'2008-10-21');
- INSERT?INTO?`companies`?VALUES?('2',?'Alcoa?Inc',?'29.01',?'0.42',?'2008-10-20');
- INSERT?INTO?`companies`?VALUES?('3',?'AT&T?Inc.',?'31.61',?'-0.48',?'2008-10-21');
- INSERT?INTO?`companies`?VALUES?('4',?'Boeing?Co.',?'75.43',?'0.53',?'2008-10-13');
- INSERT?INTO?`companies`?VALUES?('5',?'United?Technologies?Corporation',?'63.26',?'0.55',?'2008-10-09');
- INSERT?INTO?`companies`?VALUES?('6',?'Intel?Corporation',?'19.88',?'0.31',?'2008-10-15');
- INSERT?INTO?`companies`?VALUES?('7',?'Exxon?Mobil?Corp',?'68.10',?'-0.43',?'2008-10-17');
如下圖所示建立工程:
數(shù)據(jù)庫配置文件如下:
- class?DATABASE_CONFIG
- {
- ????var?$default?=?array('driver'?=>?'mysql',
- ????????????????????????????????'connect'?=>?'mysql_connect',
- ????????????????????????????????'host'?=>?'localhost',
- ????????????????????????????????'login'?=>?'root',
- ????????????????????????????????'password'?=>?'root',
- ????????????????????????????????'database'?=>?'cake_ext',
- ????????????????????????????????'prefix'?=>?'');
- ????var?$test?=?array('driver'?=>?'mysql',
- ????????????????????????????'connect'?=>?'mysql_connect',
- ????????????????????????????'host'?=>?'localhost',
- ????????????????????????????'login'?=>?'root',
- ????????????????????????????'password'?=>?'root',
- ????????????????????????????'database'?=>?'cake_ext',
- ????????????????????????????'prefix'?=>?'');
- }
companies_controller.php:
-
- class?CompaniesController?extends?AppController
- {
- ????var?$name?=?'Companies';
- ????
- ????function?index()
- ????{
- ????????$this->set('companies',?$this->Company->findAll());
- ????}
- ????
- ????function?view($id?=?null)
- ????{
- ????????$this->Company->id?=?$id;
- ????????$this->set('company',?$this->Company->read());
- ????}
- }
- ?>
company.php:
?
-
- class?Company?extends?AppModel
- {
- ????var?$name?=?'Company';
- }
- ?>
index.thtml:
-
Test?companies
Id company price change last?update - foreach?($companies?as?$company):??>
echo?$company['Company']['id'];??> - echo?$html->link($company['Company']['company'],?"/companies/view/".$company['Company']['id']);??>
echo?$company['Company']['price'];??> echo?$company['Company']['change'];??> echo?$company['Company']['lastudp'];??> - endforeach;??>??
view.thtml:
-
Company:?echo?$company['Company']['company']?>
-
Id:?echo?$company['Company']['id']?>
-
Price:?echo?$company['Company']['price']?>
-
Change:?echo?$company['Company']['change']?>
LastUpdate:?echo?$company['Company']['lastudp']?>
訪問http://localhost/cakephp/companies即可運(yùn)行測試程序。
?
本代碼參考自官方自帶例子:http://book.cakephp.org/view/326/The-Cake-Blog-Tutorial

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

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

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

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

MySQL is popular because of its excellent performance and ease of use and maintenance. 1. Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2. Insert and query data: operate data through INSERTINTO and SELECT statements. 3. Optimize query: Use indexes and EXPLAIN statements to improve performance.

MongoDB's future is full of possibilities: 1. The development of cloud-native databases, 2. The fields of artificial intelligence and big data are focused, 3. The improvement of security and compliance. MongoDB continues to advance and make breakthroughs in technological innovation, market position and future development direction.
